diff --git a/lib/__tests__/.stylelintignore b/lib/__tests__/.stylelintignore new file mode 100644 index 0000000000..b1d30e5a73 --- /dev/null +++ b/lib/__tests__/.stylelintignore @@ -0,0 +1 @@ +postcssstylelintignore.css diff --git a/lib/__tests__/postcssPlugin.test.js b/lib/__tests__/postcssPlugin.test.js index 6207e4dfc1..a1355f009d 100644 --- a/lib/__tests__/postcssPlugin.test.js +++ b/lib/__tests__/postcssPlugin.test.js @@ -107,3 +107,54 @@ it("`ignoreFiles` options is not empty and file not ignored", () => { expect(postcssResult.stylelint.ignored).toBeFalsy(); }); }); + +describe("stylelintignore", () => { + let actualCwd; + + beforeEach(() => { + actualCwd = process.cwd(); + process.chdir(__dirname); + }); + + afterEach(() => { + process.chdir(actualCwd); + }); + + it("postcssPlugin with .stylelintignore and file is ignored", () => { + const options = { + config: { + rules: { + "block-no-empty": true + } + }, + from: "postcssstylelintignore.css" + }; + + return postcssPlugin + .process("a {}", { from: undefined }, options) + .then(postcssResult => { + expect(postcssResult.stylelint.ignored).toBeTruthy(); + }); + }); + + it("postcssPlugin with ignorePath and file is ignored", () => { + const options = { + config: { + rules: { + "block-no-empty": true + } + }, + from: "foo.css", + ignorePath: path.join( + __dirname, + "./stylelintignore-test/.postcssPluginignore" + ) + }; + + return postcssPlugin + .process("a {}", { from: undefined }, options) + .then(postcssResult => { + expect(postcssResult.stylelint.ignored).toBeTruthy(); + }); + }); +}); diff --git a/lib/__tests__/stylelintignore-test/.postcssPluginignore b/lib/__tests__/stylelintignore-test/.postcssPluginignore new file mode 100644 index 0000000000..4799acf495 --- /dev/null +++ b/lib/__tests__/stylelintignore-test/.postcssPluginignore @@ -0,0 +1 @@ +foo.css diff --git a/lib/isPathIgnored.js b/lib/isPathIgnored.js index 7aa35ac393..44b04498d5 100644 --- a/lib/isPathIgnored.js +++ b/lib/isPathIgnored.js @@ -1,6 +1,8 @@ /* @flow */ "use strict"; +const filterFilePaths = require("./utils/filterFilePaths"); +const getFileIgnorer = require("./utils/getFileIgnorer"); const micromatch = require("micromatch"); const path = require("path"); const slash = require("slash"); @@ -18,6 +20,9 @@ module.exports = function( return Promise.resolve(false); } + const cwd = process.cwd(); + const ignorer = getFileIgnorer(stylelint._options); + return stylelint.getConfigForFile(filePath).then(result => { // Glob patterns for micromatch should be in POSIX-style const ignoreFiles = (result.config.ignoreFiles || []).map(slash); @@ -30,6 +35,14 @@ module.exports = function( return true; } + // Check filePath with .stylelintignore file + if ( + filterFilePaths(ignorer, [path.relative(cwd, absoluteFilePath)]) + .length === 0 + ) { + return true; + } + return false; }); }; diff --git a/lib/postcssPlugin.js b/lib/postcssPlugin.js index dee9764ca9..0ad48bf6aa 100644 --- a/lib/postcssPlugin.js +++ b/lib/postcssPlugin.js @@ -30,6 +30,7 @@ const postcss /*: postcssType*/ = require("postcss"); from?: string; ignoreDisables?: boolean; ignoreFiles?: string; + ignorePath?: string; pluginFunctions?: Object; plugins?: Array; rules?: Object; diff --git a/lib/standalone.js b/lib/standalone.js index 13b1605981..97483980a9 100644 --- a/lib/standalone.js +++ b/lib/standalone.js @@ -99,6 +99,7 @@ module.exports = function( configBasedir, configOverrides, ignoreDisables, + ignorePath: ignoreFilePath, reportNeedlessDisables, syntax, customSyntax, diff --git a/lib/utils/getFileIgnorer.js b/lib/utils/getFileIgnorer.js new file mode 100644 index 0000000000..003fc8bc01 --- /dev/null +++ b/lib/utils/getFileIgnorer.js @@ -0,0 +1,30 @@ +"use strict"; +// Try to get file ignorer from '.stylelintignore' + +const fs = require("fs"); +const ignore = require("ignore"); +const path = require("path"); + +const DEFAULT_IGNORE_FILENAME = ".stylelintignore"; +const FILE_NOT_FOUND_ERROR_CODE = "ENOENT"; + +module.exports = function(options /*: stylelint$standaloneOptions */) { + const ignoreFilePath = options.ignorePath || DEFAULT_IGNORE_FILENAME; + const absoluteIgnoreFilePath = path.isAbsolute(ignoreFilePath) + ? ignoreFilePath + : path.resolve(process.cwd(), ignoreFilePath); + let ignoreText = ""; + + try { + ignoreText = fs.readFileSync(absoluteIgnoreFilePath, "utf8"); + } catch (readError) { + if (readError.code !== FILE_NOT_FOUND_ERROR_CODE) throw readError; + } + + const ignorePattern = options.ignorePattern || []; + const ignorer = ignore() + .add(ignoreText) + .add(ignorePattern); + + return ignorer; +};