blob: b3c05d3e27ef8028369f4e58307326765346d828 [file] [log] [blame]
Mathias Bynens79e2cf02020-05-29 16:46:17 +02001'use strict';
2
3const filterFilePaths = require('./utils/filterFilePaths');
4const getFileIgnorer = require('./utils/getFileIgnorer');
5const micromatch = require('micromatch');
Tim van der Lippe16b82282021-11-08 13:50:26 +00006const normalizePath = require('normalize-path');
Mathias Bynens79e2cf02020-05-29 16:46:17 +02007const path = require('path');
Mathias Bynens79e2cf02020-05-29 16:46:17 +02008
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 Lippe16b82282021-11-08 13:50:26 +000013 * @param {import('stylelint').InternalApi} stylelint
Mathias Bynens79e2cf02020-05-29 16:46:17 +020014 * @param {string} [filePath]
15 * @return {Promise<boolean>}
16 */
Tim van der Lippe16b82282021-11-08 13:50:26 +000017module.exports = async function isPathIgnored(stylelint, filePath) {
Mathias Bynens79e2cf02020-05-29 16:46:17 +020018 if (!filePath) {
Tim van der Lippe16b82282021-11-08 13:50:26 +000019 return false;
Mathias Bynens79e2cf02020-05-29 16:46:17 +020020 }
21
22 const cwd = process.cwd();
23 const ignorer = getFileIgnorer(stylelint._options);
24
Tim van der Lippe16b82282021-11-08 13:50:26 +000025 const result = await stylelint.getConfigForFile(filePath, filePath);
Mathias Bynens79e2cf02020-05-29 16:46:17 +020026
Tim van der Lippe16b82282021-11-08 13:50:26 +000027 if (!result) {
28 return true;
29 }
Mathias Bynens79e2cf02020-05-29 16:46:17 +020030
Tim van der Lippe16b82282021-11-08 13:50:26 +000031 // 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 Bynens79e2cf02020-05-29 16:46:17 +020035
Tim van der Lippe16b82282021-11-08 13:50:26 +000036 const absoluteFilePath = path.isAbsolute(filePath)
37 ? filePath
38 : path.resolve(process.cwd(), filePath);
Mathias Bynens79e2cf02020-05-29 16:46:17 +020039
Tim van der Lippe16b82282021-11-08 13:50:26 +000040 if (micromatch([absoluteFilePath], ignoreFiles).length) {
41 return true;
42 }
Mathias Bynens79e2cf02020-05-29 16:46:17 +020043
Tim van der Lippe16b82282021-11-08 13:50:26 +000044 // Check filePath with .stylelintignore file
45 if (filterFilePaths(ignorer, [path.relative(cwd, absoluteFilePath)]).length === 0) {
46 return true;
47 }
48
49 return false;
Mathias Bynens79e2cf02020-05-29 16:46:17 +020050};