UNPKG

1.35 kBJavaScriptView Raw
1'use strict';
2
3const filterFilePaths = require('./utils/filterFilePaths');
4const getFileIgnorer = require('./utils/getFileIgnorer');
5const micromatch = require('micromatch');
6const normalizePath = require('normalize-path');
7const path = require('path');
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').InternalApi} stylelint
14 * @param {string} [filePath]
15 * @return {Promise<boolean>}
16 */
17module.exports = async function isPathIgnored(stylelint, filePath) {
18 if (!filePath) {
19 return false;
20 }
21
22 const cwd = stylelint._options.cwd;
23 const ignorer = getFileIgnorer(stylelint._options);
24
25 const result = await stylelint.getConfigForFile(filePath, filePath);
26
27 if (!result) {
28 return true;
29 }
30
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 );
35
36 const absoluteFilePath = path.isAbsolute(filePath) ? filePath : path.resolve(cwd, filePath);
37
38 if (micromatch([absoluteFilePath], ignoreFiles).length) {
39 return true;
40 }
41
42 // Check filePath with .stylelintignore file
43 if (filterFilePaths(ignorer, [path.relative(cwd, absoluteFilePath)]).length === 0) {
44 return true;
45 }
46
47 return false;
48};