From 46017f1f44e62353db1da0a120c4e1388a230f3c Mon Sep 17 00:00:00 2001 From: "Nicholas C. Zakas" Date: Thu, 1 Sep 2022 13:47:34 -0700 Subject: [PATCH] fix: Ensure globbing doesn't include subdirectories Fixes #16260 --- lib/eslint/eslint-helpers.js | 24 +++++++++++++++---- tests/fixtures/shallow-glob/eslint.config.js | 5 ++++ tests/fixtures/shallow-glob/subdir/broken.js | 1 + .../shallow-glob/target-dir/passing.js | 1 + tests/lib/eslint/flat-eslint.js | 13 ++++++++++ 5 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 tests/fixtures/shallow-glob/eslint.config.js create mode 100644 tests/fixtures/shallow-glob/subdir/broken.js create mode 100644 tests/fixtures/shallow-glob/target-dir/passing.js diff --git a/lib/eslint/eslint-helpers.js b/lib/eslint/eslint-helpers.js index 442686e56bf..d23af3e6ea9 100644 --- a/lib/eslint/eslint-helpers.js +++ b/lib/eslint/eslint-helpers.js @@ -126,7 +126,7 @@ async function findFiles({ const filePaths = patterns.map(filePath => path.resolve(cwd, filePath)); const stats = await Promise.all( filePaths.map( - filePath => fsp.stat(filePath).catch(() => {}) + filePath => fsp.stat(filePath).catch(() => { }) ) ); @@ -162,16 +162,32 @@ async function findFiles({ return false; } + // patterns starting with ** always apply + if (filePattern.startsWith("**")) { + return true; + } + // not sure how to handle negated patterns yet if (filePattern.startsWith("!")) { return false; } - // check if the pattern would be inside the cwd or not + // check if the pattern would be inside the config base path or not const fullFilePattern = path.join(cwd, filePattern); - const relativeFilePattern = path.relative(configs.basePath, fullFilePattern); + const patternRelativeToConfigBasePath = path.relative(configs.basePath, fullFilePattern); + + if (patternRelativeToConfigBasePath.startsWith("..")) { + return false; + } + + // check if the pattern is inside the directory or not + const patternRelativeToFilePath = path.relative(filePath, fullFilePattern); + + if (patternRelativeToFilePath.startsWith("..")) { + return false; + } - return !relativeFilePattern.startsWith(".."); + return true; }) .map(filePattern => { if (filePattern.startsWith("**")) { diff --git a/tests/fixtures/shallow-glob/eslint.config.js b/tests/fixtures/shallow-glob/eslint.config.js new file mode 100644 index 00000000000..211719ddb90 --- /dev/null +++ b/tests/fixtures/shallow-glob/eslint.config.js @@ -0,0 +1,5 @@ +module.exports = [ + { + files: ["subdir/*.js"] + } +]; diff --git a/tests/fixtures/shallow-glob/subdir/broken.js b/tests/fixtures/shallow-glob/subdir/broken.js new file mode 100644 index 00000000000..d280fee2fdc --- /dev/null +++ b/tests/fixtures/shallow-glob/subdir/broken.js @@ -0,0 +1 @@ +module.exports = /* intentional syntax error */ diff --git a/tests/fixtures/shallow-glob/target-dir/passing.js b/tests/fixtures/shallow-glob/target-dir/passing.js new file mode 100644 index 00000000000..ec01c2c1416 --- /dev/null +++ b/tests/fixtures/shallow-glob/target-dir/passing.js @@ -0,0 +1 @@ +module.exports = true; diff --git a/tests/lib/eslint/flat-eslint.js b/tests/lib/eslint/flat-eslint.js index 1af6c2e1d8b..4c6f9df162a 100644 --- a/tests/lib/eslint/flat-eslint.js +++ b/tests/lib/eslint/flat-eslint.js @@ -784,6 +784,19 @@ describe("FlatESLint", () => { assert.strictEqual(results[0].suppressedMessages.length, 0); }); + // https://github.com/eslint/eslint/issues/16260 + it("should report zero messages when given a directory with a .js and config file specifying a subdirectory", async () => { + eslint = new FlatESLint({ + ignore: false, + cwd: getFixturePath("shallow-glob") + }); + const results = await eslint.lintFiles(["target-dir"]); + + assert.strictEqual(results.length, 1); + assert.strictEqual(results[0].messages.length, 0); + assert.strictEqual(results[0].suppressedMessages.length, 0); + }); + it("should report zero messages when given a '**' pattern with a .js and a .js2 file", async () => { eslint = new FlatESLint({ ignore: false,