From d597cb19101bdd4f14619ccefcfc4c1c150d8ce9 Mon Sep 17 00:00:00 2001 From: hezaichang Date: Sun, 4 Aug 2019 20:42:18 +0800 Subject: [PATCH 1/4] Fix PostCSS plugin ignoring .stylelintignore --- .gitignore | 1 + lib/isPathIgnored.js | 13 +++++++++++++ lib/utils/getFileIgnorer.js | 30 ++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 lib/utils/getFileIgnorer.js diff --git a/.gitignore b/.gitignore index 1cc5b58824..d398dc45d5 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ node_modules .eslintcache package-lock.json yarn.lock +.idea 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/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; +}; From 6f8e5fba883e05e9a908a586d5d690fb7dc9fb7f Mon Sep 17 00:00:00 2001 From: hezaichang Date: Mon, 5 Aug 2019 21:53:15 +0800 Subject: [PATCH 2/4] Changes: Input ignorePath parameter in standalone --- .gitignore | 1 - lib/standalone.js | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index d398dc45d5..1cc5b58824 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,3 @@ node_modules .eslintcache package-lock.json yarn.lock -.idea 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, From ab74156db648a5bc0c31dbd19777d7a3391d8bb3 Mon Sep 17 00:00:00 2001 From: hezaichang Date: Sat, 31 Aug 2019 21:53:58 +0800 Subject: [PATCH 3/4] Add postcssPlugin ignorePath unit test --- lib/__tests__/postcssPlugin.test.js | 21 +++++++++++++++++++ .../stylelintignore-test/.postcssPluginignore | 1 + lib/postcssPlugin.js | 1 + 3 files changed, 23 insertions(+) create mode 100644 lib/__tests__/stylelintignore-test/.postcssPluginignore diff --git a/lib/__tests__/postcssPlugin.test.js b/lib/__tests__/postcssPlugin.test.js index 6207e4dfc1..f3087ab7d6 100644 --- a/lib/__tests__/postcssPlugin.test.js +++ b/lib/__tests__/postcssPlugin.test.js @@ -107,3 +107,24 @@ it("`ignoreFiles` options is not empty and file not ignored", () => { expect(postcssResult.stylelint.ignored).toBeFalsy(); }); }); + +it("postcssPlugin with .stylelintignore 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/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; From 8f024a7c398e99083f3672aa5351ae4b61117dbb Mon Sep 17 00:00:00 2001 From: hezaichang Date: Sun, 1 Sep 2019 21:58:40 +0800 Subject: [PATCH 4/4] Changes: postcssPlugin stylelintignore unit test --- lib/__tests__/.stylelintignore | 1 + lib/__tests__/postcssPlugin.test.js | 68 +++++++++++++++++++++-------- 2 files changed, 50 insertions(+), 19 deletions(-) create mode 100644 lib/__tests__/.stylelintignore 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 f3087ab7d6..a1355f009d 100644 --- a/lib/__tests__/postcssPlugin.test.js +++ b/lib/__tests__/postcssPlugin.test.js @@ -108,23 +108,53 @@ it("`ignoreFiles` options is not empty and file not ignored", () => { }); }); -it("postcssPlugin with .stylelintignore 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(); - }); +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(); + }); + }); });