Skip to content

Commit 9432b67

Browse files
mdjermanovicnzakas
andauthored
fix: throw error for first unmatched pattern (#16533)
* fix: throw error for first unmatched pattern * Update lib/eslint/eslint-helpers.js Co-authored-by: Nicholas C. Zakas <[email protected]> Co-authored-by: Nicholas C. Zakas <[email protected]>
1 parent 8385ecd commit 9432b67

2 files changed

Lines changed: 48 additions & 29 deletions

File tree

lib/eslint/eslint-helpers.js

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class UnmatchedSearchPatternsError extends Error {
7676
constructor({ basePath, unmatchedPatterns, patterns, rawPatterns }) {
7777
super(`No files matching '${rawPatterns}' in '${basePath}' were found.`);
7878
this.basePath = basePath;
79-
this.patternsToCheck = unmatchedPatterns;
79+
this.unmatchedPatterns = unmatchedPatterns;
8080
this.patterns = patterns;
8181
this.rawPatterns = rawPatterns;
8282
}
@@ -337,49 +337,43 @@ async function globSearch({
337337
}
338338

339339
/**
340-
* Checks to see if there are any ignored results for a given search. This
341-
* happens either when there are unmatched patterns during a search or if
342-
* a search returns no results.
340+
* Throws an error for unmatched patterns. The error will only contain information about the first one.
341+
* Checks to see if there are any ignored results for a given search.
343342
* @param {Object} options The options for this function.
344343
* @param {string} options.basePath The directory to search.
345344
* @param {Array<string>} options.patterns An array of glob patterns
346345
* that were used in the original search.
347346
* @param {Array<string>} options.rawPatterns An array of glob patterns
348347
* as the user inputted them. Used for errors.
349-
* @param {Array<string>} options.patternsToCheck An array of glob patterns
350-
* to use for this check.
351-
* @returns {void}
352-
* @throws {NoFilesFoundError} If there is a pattern that doesn't match
353-
* any files and `errorOnUnmatchedPattern` is true.
354-
* @throws {AllFilesIgnoredError} If there is a pattern that matches files
355-
* when there are no ignores.
348+
* @param {Array<string>} options.unmatchedPatterns A non-empty array of glob patterns
349+
* that were unmatched in the original search.
350+
* @returns {void} Always throws an error.
351+
* @throws {NoFilesFoundError} If the first unmatched pattern
352+
* doesn't match any files even when there are no ignores.
353+
* @throws {AllFilesIgnoredError} If the first unmatched pattern
354+
* matches some files when there are no ignores.
356355
*/
357-
async function checkForIgnoredResults({
356+
async function throwErrorForUnmatchedPatterns({
358357
basePath,
359358
patterns,
360359
rawPatterns,
361-
patternsToCheck = patterns
360+
unmatchedPatterns
362361
}) {
363362

364-
for (const pattern of patternsToCheck) {
363+
const pattern = unmatchedPatterns[0];
364+
const rawPattern = rawPatterns[patterns.indexOf(pattern)];
365365

366-
const patternHasMatch = await globMatch({
367-
basePath,
368-
pattern
369-
});
366+
const patternHasMatch = await globMatch({
367+
basePath,
368+
pattern
369+
});
370370

371-
if (patternHasMatch) {
372-
throw new AllFilesIgnoredError(
373-
rawPatterns[patterns.indexOf(pattern)]
374-
);
375-
}
371+
if (patternHasMatch) {
372+
throw new AllFilesIgnoredError(rawPattern);
376373
}
377374

378375
// if we get here there are truly no matches
379-
throw new NoFilesFoundError(
380-
rawPatterns[patterns.indexOf(patternsToCheck[0])],
381-
true
382-
);
376+
throw new NoFilesFoundError(rawPattern, true);
383377
}
384378

385379
/**
@@ -446,9 +440,9 @@ async function globMultiSearch({ searches, configs, errorOnUnmatchedPattern }) {
446440

447441
if (errorOnUnmatchedPattern) {
448442

449-
await checkForIgnoredResults({
443+
await throwErrorForUnmatchedPatterns({
450444
...currentSearch,
451-
patternsToCheck: error.patternsToCheck
445+
unmatchedPatterns: error.unmatchedPatterns
452446
});
453447

454448
}

tests/lib/eslint/flat-eslint.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -863,6 +863,31 @@ describe("FlatESLint", () => {
863863
}, /All files matched by 'subdir2\/\*\.js' are ignored/u);
864864
});
865865

866+
it("should always throw an error for the first unmatched file pattern", async () => {
867+
eslint = new FlatESLint({
868+
cwd: getFixturePath("example-app2"),
869+
overrideConfig: {
870+
ignores: ["subdir1/*.js", "subdir2/*.js"]
871+
}
872+
});
873+
874+
await assert.rejects(async () => {
875+
await eslint.lintFiles(["doesnotexist1/*.js", "doesnotexist2/*.js"]);
876+
}, /No files matching 'doesnotexist1\/\*\.js' were found/u);
877+
878+
await assert.rejects(async () => {
879+
await eslint.lintFiles(["doesnotexist1/*.js", "subdir1/*.js"]);
880+
}, /No files matching 'doesnotexist1\/\*\.js' were found/u);
881+
882+
await assert.rejects(async () => {
883+
await eslint.lintFiles(["subdir1/*.js", "doesnotexist1/*.js"]);
884+
}, /All files matched by 'subdir1\/\*\.js' are ignored/u);
885+
886+
await assert.rejects(async () => {
887+
await eslint.lintFiles(["subdir1/*.js", "subdir2/*.js"]);
888+
}, /All files matched by 'subdir1\/\*\.js' are ignored/u);
889+
});
890+
866891
it("should not throw an error for an ignored file pattern when errorOnUnmatchedPattern is false", async () => {
867892
eslint = new FlatESLint({
868893
cwd: getFixturePath("example-app2"),

0 commit comments

Comments
 (0)