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'); |
| 6 | const path = require('path'); |
| 7 | const slash = require('slash'); |
| 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. |
| 13 | * @param {import('stylelint').StylelintInternalApi} stylelint |
| 14 | * @param {string} [filePath] |
| 15 | * @return {Promise<boolean>} |
| 16 | */ |
| 17 | module.exports = function (stylelint, filePath) { |
| 18 | if (!filePath) { |
| 19 | return Promise.resolve(false); |
| 20 | } |
| 21 | |
| 22 | const cwd = process.cwd(); |
| 23 | const ignorer = getFileIgnorer(stylelint._options); |
| 24 | |
| 25 | return stylelint.getConfigForFile(filePath).then((result) => { |
| 26 | if (!result) { |
| 27 | return true; |
| 28 | } |
| 29 | |
| 30 | // Glob patterns for micromatch should be in POSIX-style |
| 31 | const ignoreFiles = /** @type {Array<string>} */ (result.config.ignoreFiles || []).map(slash); |
| 32 | |
| 33 | const absoluteFilePath = path.isAbsolute(filePath) |
| 34 | ? filePath |
| 35 | : path.resolve(process.cwd(), filePath); |
| 36 | |
| 37 | if (micromatch([absoluteFilePath], ignoreFiles).length) { |
| 38 | return true; |
| 39 | } |
| 40 | |
| 41 | // Check filePath with .stylelintignore file |
| 42 | if (filterFilePaths(ignorer, [path.relative(cwd, absoluteFilePath)]).length === 0) { |
| 43 | return true; |
| 44 | } |
| 45 | |
| 46 | return false; |
| 47 | }); |
| 48 | }; |