Skip to content

Commit 04c7188

Browse files
authored
fix: Don't lint same file multiple times (#18899)
1 parent 87ec3c4 commit 04c7188

3 files changed

Lines changed: 62 additions & 25 deletions

File tree

lib/eslint/eslint-helpers.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ async function globMultiSearch({ searches, configs, errorOnUnmatchedPattern }) {
484484

485485
}
486486

487-
return [...new Set(filePaths)];
487+
return filePaths;
488488

489489
}
490490

@@ -533,10 +533,7 @@ async function findFiles({
533533

534534
// files are added directly to the list
535535
if (stat.isFile()) {
536-
results.push({
537-
filePath,
538-
ignored: configs.isFileIgnored(filePath)
539-
});
536+
results.push(filePath);
540537
}
541538

542539
// directories need extensions attached
@@ -594,11 +591,10 @@ async function findFiles({
594591
});
595592

596593
return [
597-
...results,
598-
...globbyResults.map(filePath => ({
599-
filePath: path.resolve(filePath),
600-
ignored: false
601-
}))
594+
...new Set([
595+
...results,
596+
...globbyResults.map(filePath => path.resolve(filePath))
597+
])
602598
];
603599
}
604600

lib/eslint/flat-eslint.js

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -790,31 +790,22 @@ class FlatESLint {
790790
*/
791791
const results = await Promise.all(
792792

793-
filePaths.map(({ filePath, ignored }) => {
793+
filePaths.map(filePath => {
794+
795+
const config = configs.getConfig(filePath);
794796

795797
/*
796-
* If a filename was entered that matches an ignore
797-
* pattern, then notify the user.
798+
* If a filename was entered that cannot be matched
799+
* to a config, then notify the user.
798800
*/
799-
if (ignored) {
801+
if (!config) {
800802
if (warnIgnored) {
801803
return createIgnoreResult(filePath, cwd);
802804
}
803805

804806
return void 0;
805807
}
806808

807-
const config = configs.getConfig(filePath);
808-
809-
/*
810-
* Sometimes a file found through a glob pattern will
811-
* be ignored. In this case, `config` will be undefined
812-
* and we just silently ignore the file.
813-
*/
814-
if (!config) {
815-
return void 0;
816-
}
817-
818809
// Skip if there is cached result.
819810
if (lintResultCache) {
820811
const cachedResult =

tests/lib/eslint/flat-eslint.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,6 +1037,56 @@ describe("FlatESLint", () => {
10371037
await assert.rejects(async () => await eslint.lintFiles(["lib/cli.js"]), /Expected object with parse\(\) or parseForESLint\(\) method/u);
10381038
});
10391039

1040+
describe("Overlapping searches", () => {
1041+
it("should not lint the same file multiple times when the file path was passed multiple times", async () => {
1042+
const cwd = getFixturePath();
1043+
1044+
eslint = new FlatESLint({
1045+
cwd,
1046+
overrideConfigFile: true
1047+
});
1048+
1049+
const results = await eslint.lintFiles(["files/foo.js", "files/../files/foo.js", "files/foo.js"]);
1050+
1051+
assert.strictEqual(results.length, 1);
1052+
assert.strictEqual(results[0].filePath, path.resolve(cwd, "files/foo.js"));
1053+
assert.strictEqual(results[0].messages.length, 0);
1054+
assert.strictEqual(results[0].suppressedMessages.length, 0);
1055+
});
1056+
1057+
it("should not lint the same file multiple times when the file path and a pattern that matches the file were passed", async () => {
1058+
const cwd = getFixturePath();
1059+
1060+
eslint = new FlatESLint({
1061+
cwd,
1062+
overrideConfigFile: true
1063+
});
1064+
1065+
const results = await eslint.lintFiles(["files/foo.js", "files/foo*"]);
1066+
1067+
assert.strictEqual(results.length, 1);
1068+
assert.strictEqual(results[0].filePath, path.resolve(cwd, "files/foo.js"));
1069+
assert.strictEqual(results[0].messages.length, 0);
1070+
assert.strictEqual(results[0].suppressedMessages.length, 0);
1071+
});
1072+
1073+
it("should not lint the same file multiple times when multiple patterns that match the file were passed", async () => {
1074+
const cwd = getFixturePath();
1075+
1076+
eslint = new FlatESLint({
1077+
cwd,
1078+
overrideConfigFile: true
1079+
});
1080+
1081+
const results = await eslint.lintFiles(["files/f*.js", "files/foo*"]);
1082+
1083+
assert.strictEqual(results.length, 1);
1084+
assert.strictEqual(results[0].filePath, path.resolve(cwd, "files/foo.js"));
1085+
assert.strictEqual(results[0].messages.length, 0);
1086+
assert.strictEqual(results[0].suppressedMessages.length, 0);
1087+
});
1088+
});
1089+
10401090
it("should report zero messages when given a directory with a .js2 file", async () => {
10411091
eslint = new FlatESLint({
10421092
cwd: path.join(fixtureDir, ".."),

0 commit comments

Comments
 (0)