Skip to content

Commit

Permalink
fix: Ensure globbing doesn't include subdirectories
Browse files Browse the repository at this point in the history
Fixes #16260
  • Loading branch information
nzakas committed Sep 1, 2022
1 parent 42bfbd7 commit 46017f1
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 4 deletions.
24 changes: 20 additions & 4 deletions lib/eslint/eslint-helpers.js
Expand Up @@ -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(() => { })
)
);

Expand Down Expand Up @@ -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("**")) {
Expand Down
5 changes: 5 additions & 0 deletions tests/fixtures/shallow-glob/eslint.config.js
@@ -0,0 +1,5 @@
module.exports = [
{
files: ["subdir/*.js"]
}
];
1 change: 1 addition & 0 deletions tests/fixtures/shallow-glob/subdir/broken.js
@@ -0,0 +1 @@
module.exports = /* intentional syntax error */
1 change: 1 addition & 0 deletions tests/fixtures/shallow-glob/target-dir/passing.js
@@ -0,0 +1 @@
module.exports = true;
13 changes: 13 additions & 0 deletions tests/lib/eslint/flat-eslint.js
Expand Up @@ -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,
Expand Down

0 comments on commit 46017f1

Please sign in to comment.