Skip to content

Commit

Permalink
fix: Ensure globbing doesn't include subdirectories (#16272)
Browse files Browse the repository at this point in the history
* fix: Ensure globbing doesn't include subdirectories

Fixes #16260

* docs: copy & use main package version in docs on release (#16252)

* docs: update docs package ver from main on release

fixes #16212

* docs: read docs package ver instead of main

fixes #16212

* docs: add newline to updated docs package json

Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>

* docs: update docs package json version

Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>

* chore: enable linting `.eleventy.js` again (#16274)

* Fix filtering of globs

* Enable partial matching of globs

Co-authored-by: Jugal Thakkar <2640821+jugalthakkar@users.noreply.github.com>
Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>
  • Loading branch information
3 people committed Sep 12, 2022
1 parent e098b5f commit c6900f8
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 4 deletions.
30 changes: 26 additions & 4 deletions lib/eslint/eslint-helpers.js
Expand Up @@ -15,6 +15,7 @@ const fsp = fs.promises;
const isGlob = require("is-glob");
const globby = require("globby");
const hash = require("../cli-engine/hash");
const minimatch = require("minimatch");

//-----------------------------------------------------------------------------
// Errors
Expand Down Expand Up @@ -126,7 +127,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 @@ -157,6 +158,11 @@ async function findFiles({
return false;
}

// patterns starting with ** always apply
if (filePattern.startsWith("**")) {
return true;
}

// patterns ending with * are not used for file search
if (filePattern.endsWith("*")) {
return false;
Expand All @@ -167,11 +173,27 @@ async function findFiles({
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 matches
if (minimatch(filePath, path.dirname(fullFilePattern), { partial: true })) {
return true;
}

// 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/subdir/subsubdir/broken.js
@@ -0,0 +1 @@
function( {} // intentional syntax error
1 change: 1 addition & 0 deletions tests/fixtures/shallow-glob/subdir/subsubdir/plain.jsx
@@ -0,0 +1 @@
foo;
1 change: 1 addition & 0 deletions tests/fixtures/shallow-glob/target-dir/passing.js
@@ -0,0 +1 @@
module.exports = true;
66 changes: 66 additions & 0 deletions tests/lib/eslint/flat-eslint.js
Expand Up @@ -784,6 +784,72 @@ describe("FlatESLint", () => {
assert.strictEqual(results[0].suppressedMessages.length, 0);
});

// https://github.com/eslint/eslint/issues/16260
describe("Globbing based on configs", () => {
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 glob for .jsx file in a subdirectory of the passed-in directory and not glob for any other patterns", async () => {
eslint = new FlatESLint({
ignore: false,
overrideConfigFile: true,
overrideConfig: {
files: ["subdir/**/*.jsx", "target-dir/*.js"],
languageOptions: {
parserOptions: {
jsx: true
}
}
},
cwd: getFixturePath("shallow-glob")
});
const results = await eslint.lintFiles(["subdir/subsubdir"]);

assert.strictEqual(results.length, 2);
assert.strictEqual(results[0].messages.length, 1);
assert(results[0].messages[0].fatal, "Fatal error expected.");
assert.strictEqual(results[0].suppressedMessages.length, 0);
assert.strictEqual(results[1].messages.length, 0);
assert.strictEqual(results[1].suppressedMessages.length, 0);
});

it("should glob for all files in subdir when passed-in on the command line with a partial matching glob", async () => {
eslint = new FlatESLint({
ignore: false,
overrideConfigFile: true,
overrideConfig: {
files: ["s*/subsubdir/*.jsx", "target-dir/*.js"],
languageOptions: {
parserOptions: {
jsx: true
}
}
},
cwd: getFixturePath("shallow-glob")
});
const results = await eslint.lintFiles(["subdir"]);

assert.strictEqual(results.length, 3);
assert.strictEqual(results[0].messages.length, 1);
assert(results[0].messages[0].fatal, "Fatal error expected.");
assert.strictEqual(results[0].suppressedMessages.length, 0);
assert.strictEqual(results[1].messages.length, 1);
assert(results[0].messages[0].fatal, "Fatal error expected.");
assert.strictEqual(results[1].suppressedMessages.length, 0);
assert.strictEqual(results[2].messages.length, 0);
assert.strictEqual(results[2].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 c6900f8

Please sign in to comment.