Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 1 | 'use strict'; |
| 2 | |
| 3 | const filterFilePaths = require('./utils/filterFilePaths'); |
| 4 | const getFileIgnorer = require('./utils/getFileIgnorer'); |
| 5 | const micromatch = require('micromatch'); |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 6 | const normalizePath = require('normalize-path'); |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 7 | const path = require('path'); |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 8 | |
| 9 | /** |
| 10 | * To find out if a path is ignored, we need to load the config, |
| 11 | * which may have an ignoreFiles property. We then check the path |
| 12 | * against these. |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 13 | * @param {import('stylelint').InternalApi} stylelint |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 14 | * @param {string} [filePath] |
| 15 | * @return {Promise<boolean>} |
| 16 | */ |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 17 | module.exports = async function isPathIgnored(stylelint, filePath) { |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 18 | if (!filePath) { |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 19 | return false; |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 20 | } |
| 21 | |
| 22 | const cwd = process.cwd(); |
| 23 | const ignorer = getFileIgnorer(stylelint._options); |
| 24 | |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 25 | const result = await stylelint.getConfigForFile(filePath, filePath); |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 26 | |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 27 | if (!result) { |
| 28 | return true; |
| 29 | } |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 30 | |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 31 | // Glob patterns for micromatch should be in POSIX-style |
| 32 | const ignoreFiles = /** @type {Array<string>} */ (result.config.ignoreFiles || []).map((s) => |
| 33 | normalizePath(s), |
| 34 | ); |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 35 | |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 36 | const absoluteFilePath = path.isAbsolute(filePath) |
| 37 | ? filePath |
| 38 | : path.resolve(process.cwd(), filePath); |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 39 | |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 40 | if (micromatch([absoluteFilePath], ignoreFiles).length) { |
| 41 | return true; |
| 42 | } |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 43 | |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 44 | // Check filePath with .stylelintignore file |
| 45 | if (filterFilePaths(ignorer, [path.relative(cwd, absoluteFilePath)]).length === 0) { |
| 46 | return true; |
| 47 | } |
| 48 | |
| 49 | return false; |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 50 | }; |