Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix: Don't lint same file multiple times
  • Loading branch information
fasttime committed Sep 14, 2024
commit 0df52c12a6605a2723294f7da87223f417b8b1e9
16 changes: 6 additions & 10 deletions lib/eslint/eslint-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ async function globMultiSearch({ searches, configs, errorOnUnmatchedPattern }) {

}

return [...new Set(filePaths)];
return filePaths;

}

Expand Down Expand Up @@ -533,10 +533,7 @@ async function findFiles({

// files are added directly to the list
if (stat.isFile()) {
results.push({
filePath,
ignored: configs.isFileIgnored(filePath)
});
results.push(filePath);
}

// directories need extensions attached
Expand Down Expand Up @@ -594,11 +591,10 @@ async function findFiles({
});

return [
...results,
...globbyResults.map(filePath => ({
filePath: path.resolve(filePath),
ignored: false
}))
...new Set([
...results,
...globbyResults.map(filePath => path.resolve(filePath))
])
];
}

Expand Down
21 changes: 6 additions & 15 deletions lib/eslint/flat-eslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -790,31 +790,22 @@ class FlatESLint {
*/
const results = await Promise.all(

filePaths.map(({ filePath, ignored }) => {
filePaths.map(filePath => {

const config = configs.getConfig(filePath);

/*
* If a filename was entered that matches an ignore
* pattern, then notify the user.
* If a filename was entered that cannot be matched
* to a config, then notify the user.
*/
if (ignored) {
if (!config) {
if (warnIgnored) {
return createIgnoreResult(filePath, cwd);
}

return void 0;
}

const config = configs.getConfig(filePath);

/*
* Sometimes a file found through a glob pattern will
* be ignored. In this case, `config` will be undefined
* and we just silently ignore the file.
*/
if (!config) {
return void 0;
}

// Skip if there is cached result.
if (lintResultCache) {
const cachedResult =
Expand Down
50 changes: 50 additions & 0 deletions tests/lib/eslint/flat-eslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -1037,6 +1037,56 @@ describe("FlatESLint", () => {
await assert.rejects(async () => await eslint.lintFiles(["lib/cli.js"]), /Expected object with parse\(\) or parseForESLint\(\) method/u);
});

describe("Overlapping searches", () => {
it("should not lint the same file multiple times when the file path was passed multiple times", async () => {
const cwd = getFixturePath();

eslint = new FlatESLint({
cwd,
overrideConfigFile: true
});

const results = await eslint.lintFiles(["files/foo.js", "files/../files/foo.js", "files/foo.js"]);

assert.strictEqual(results.length, 1);
assert.strictEqual(results[0].filePath, path.resolve(cwd, "files/foo.js"));
assert.strictEqual(results[0].messages.length, 0);
assert.strictEqual(results[0].suppressedMessages.length, 0);
});

it("should not lint the same file multiple times when the file path and a pattern that matches the file were passed", async () => {
const cwd = getFixturePath();

eslint = new FlatESLint({
cwd,
overrideConfigFile: true
});

const results = await eslint.lintFiles(["files/foo.js", "files/foo*"]);

assert.strictEqual(results.length, 1);
assert.strictEqual(results[0].filePath, path.resolve(cwd, "files/foo.js"));
assert.strictEqual(results[0].messages.length, 0);
assert.strictEqual(results[0].suppressedMessages.length, 0);
});

it("should not lint the same file multiple times when multiple patterns that match the file were passed", async () => {
const cwd = getFixturePath();

eslint = new FlatESLint({
cwd,
overrideConfigFile: true
});

const results = await eslint.lintFiles(["files/f*.js", "files/foo*"]);

assert.strictEqual(results.length, 1);
assert.strictEqual(results[0].filePath, path.resolve(cwd, "files/foo.js"));
assert.strictEqual(results[0].messages.length, 0);
assert.strictEqual(results[0].suppressedMessages.length, 0);
});
});

it("should report zero messages when given a directory with a .js2 file", async () => {
eslint = new FlatESLint({
cwd: path.join(fixtureDir, ".."),
Expand Down