UNPKG

1.34 kBJavaScriptView Raw
1'use strict';
2
3const filterFilePaths = require('./utils/filterFilePaths');
4const getFileIgnorer = require('./utils/getFileIgnorer');
5const micromatch = require('micromatch');
6const path = require('path');
7const 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 */
17module.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};