From 4928d513b4fe716c7ed958c294a10ef8517be25e Mon Sep 17 00:00:00 2001 From: Toru Nagashima Date: Sun, 1 Dec 2019 01:42:45 +0900 Subject: [PATCH] Fix: don't ignore the entry directory (fixes #12604) (#12607) --- lib/cli-engine/file-enumerator.js | 18 +++++++-- tests/lib/cli-engine/cli-engine.js | 51 ++++++++++++++++++++++++- tests/lib/cli-engine/file-enumerator.js | 2 +- 3 files changed, 65 insertions(+), 6 deletions(-) diff --git a/lib/cli-engine/file-enumerator.js b/lib/cli-engine/file-enumerator.js index 700f8009cf8..b5a082b71a6 100644 --- a/lib/cli-engine/file-enumerator.js +++ b/lib/cli-engine/file-enumerator.js @@ -375,9 +375,6 @@ class FileEnumerator { * @private */ *_iterateFilesRecursive(directoryPath, options) { - if (this._isIgnoredFile(directoryPath + path.sep, options)) { - return; - } debug(`Enter the directory: ${directoryPath}`); const { configArrayFactory, extensionRegExp } = internalSlotsMap.get(this); @@ -426,7 +423,20 @@ class FileEnumerator { // Dive into the sub directory. } else if (options.recursive && stat && stat.isDirectory()) { - yield* this._iterateFilesRecursive(filePath, options); + if (!config) { + config = configArrayFactory.getConfigArrayForFile( + filePath, + { ignoreNotFoundError: true } + ); + } + const ignored = this._isIgnoredFile( + filePath + path.sep, + { ...options, config } + ); + + if (!ignored) { + yield* this._iterateFilesRecursive(filePath, options); + } } } diff --git a/tests/lib/cli-engine/cli-engine.js b/tests/lib/cli-engine/cli-engine.js index 1d9bef0905f..1c2411c9776 100644 --- a/tests/lib/cli-engine/cli-engine.js +++ b/tests/lib/cli-engine/cli-engine.js @@ -1288,7 +1288,7 @@ describe("CLIEngine", () => { assert.throws(() => { engine.executeOnFiles(["./tests/fixtures/cli-engine/"]); - }, "No files matching './tests/fixtures/cli-engine/' were found."); + }, "All files matched by './tests/fixtures/cli-engine/' are ignored."); }); it("should throw an error when all given files are ignored via ignore-pattern", () => { @@ -3628,6 +3628,55 @@ describe("CLIEngine", () => { assert.strictEqual(messages[0].ruleId, "no-console"); }); }); + + describe("don't ignore the entry directory.", () => { + const root = getFixturePath("cli-engine/dont-ignore-entry-dir"); + + it("'executeOnFiles(\".\")' should not load config files from outside of \".\".", () => { + CLIEngine = defineCLIEngineWithInMemoryFileSystem({ + cwd: () => root, + files: { + "../.eslintrc.json": "BROKEN FILE", + ".eslintrc.json": JSON.stringify({ root: true }), + "index.js": "console.log(\"hello\")" + } + }).CLIEngine; + engine = new CLIEngine(); + + // Don't throw "failed to load config file" error. + engine.executeOnFiles("."); + }); + + it("'executeOnFiles(\".\")' should not ignore '.' even if 'ignorePatterns' contains it.", () => { + CLIEngine = defineCLIEngineWithInMemoryFileSystem({ + cwd: () => root, + files: { + "../.eslintrc.json": JSON.stringify({ ignorePatterns: ["/dont-ignore-entry-dir"] }), + ".eslintrc.json": JSON.stringify({ root: true }), + "index.js": "console.log(\"hello\")" + } + }).CLIEngine; + engine = new CLIEngine(); + + // Don't throw "file not found" error. + engine.executeOnFiles("."); + }); + + it("'executeOnFiles(\"subdir\")' should not ignore './subdir' even if 'ignorePatterns' contains it.", () => { + CLIEngine = defineCLIEngineWithInMemoryFileSystem({ + cwd: () => root, + files: { + ".eslintrc.json": JSON.stringify({ ignorePatterns: ["/subdir"] }), + "subdir/.eslintrc.json": JSON.stringify({ root: true }), + "subdir/index.js": "console.log(\"hello\")" + } + }).CLIEngine; + engine = new CLIEngine(); + + // Don't throw "file not found" error. + engine.executeOnFiles("subdir"); + }); + }); }); describe("getConfigForFile", () => { diff --git a/tests/lib/cli-engine/file-enumerator.js b/tests/lib/cli-engine/file-enumerator.js index 3d05f63ab07..c62578c9162 100644 --- a/tests/lib/cli-engine/file-enumerator.js +++ b/tests/lib/cli-engine/file-enumerator.js @@ -380,7 +380,7 @@ describe("FileEnumerator", () => { assert.throws(() => { listFiles(patterns); - }, `No files matching '${patterns[0]}' were found.`); + }, `All files matched by '${patterns[0]}' are ignored.`); }); it("should return an ignored file, if ignore option is turned off", () => {