Skip to content

Commit 0a9c433

Browse files
authored
feat: Add --no-warn-ignored CLI option for flat config (#17569)
* Add warnIgnored CLI option for flat config * Update ignore result warning message about its suppression * Add test for lintText warnIgnored overriding constructor option * Respect constructor warnIgnored when linting via stdin * Move warnIgnored help text under Miscellaneous * Use void 0 instead of undefined * Add test cases for eslint.lintFiles with warnIgnored:false * Add assertion for error message when warnIgnored is not the right type * Change file ignore warning wording * Simplify error condition * Fix accidentally commented out test line * Add documentation for --no-warn-ignored CLI flag * Clarify warnIgnored descriptions
1 parent 1866da5 commit 0a9c433

File tree

9 files changed

+170
-25
lines changed

9 files changed

+170
-25
lines changed

docs/src/use/command-line-interface.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ Miscellaneous:
110110
--env-info Output execution environment information - default: false
111111
--no-error-on-unmatched-pattern Prevent errors when pattern is unmatched
112112
--exit-on-fatal-error Exit with exit code 2 in case of fatal error - default: false
113+
--no-warn-ignored Suppress warnings when the file list includes ignored files. *Flat Config Mode Only*
113114
--debug Output debugging information
114115
-h, --help Show help
115116
-v, --version Output the version number
@@ -703,6 +704,18 @@ This option causes ESLint to exit with exit code 2 if one or more fatal parsing
703704
npx eslint --exit-on-fatal-error file.js
704705
```
705706

707+
#### `--no-warn-ignored`
708+
709+
**Flat Config Mode Only.** This option suppresses both `File ignored by default` and `File ignored because of a matching ignore pattern` warnings when an ignored filename is passed explicitly. It is useful when paired with `--max-warnings 0` as it will prevent exit code 1 due to the aforementioned warning.
710+
711+
* **Argument Type**: No argument.
712+
713+
##### `--no-warn-ignored` example
714+
715+
```shell
716+
npx eslint --no-warn-ignored --max-warnings 0 ignored-file.js
717+
```
718+
706719
#### `--debug`
707720

708721
This option outputs debugging information to the console. Add this flag to an ESLint command line invocation in order to get extra debugging information while the command runs.

docs/src/use/configure/ignore.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ You'll see this warning:
149149

150150
```text
151151
foo.js
152-
0:0 warning File ignored because of a matching ignore pattern. Use "--no-ignore" to override.
152+
0:0 warning File ignored because of a matching ignore pattern. Use "--no-ignore" to disable file ignore settings or use "--no-warn-ignored" to suppress this warning.
153153
154154
✖ 1 problem (0 errors, 1 warning)
155155
```

lib/cli.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ async function translateOptions({
9191
reportUnusedDisableDirectives,
9292
resolvePluginsRelativeTo,
9393
rule,
94-
rulesdir
94+
rulesdir,
95+
warnIgnored
9596
}, configType) {
9697

9798
let overrideConfig, overrideConfigFile;
@@ -182,6 +183,7 @@ async function translateOptions({
182183

183184
if (configType === "flat") {
184185
options.ignorePatterns = ignorePattern;
186+
options.warnIgnored = warnIgnored;
185187
} else {
186188
options.resolvePluginsRelativeTo = resolvePluginsRelativeTo;
187189
options.rulePaths = rulesdir;
@@ -385,7 +387,9 @@ const cli = {
385387
if (useStdin) {
386388
results = await engine.lintText(text, {
387389
filePath: options.stdinFilename,
388-
warnIgnored: true
390+
391+
// flatConfig respects CLI flag and constructor warnIgnored, eslintrc forces true for backwards compatibility
392+
warnIgnored: usingFlatConfig ? void 0 : true
389393
});
390394
} else {
391395
results = await engine.lintFiles(files);

lib/eslint/eslint-helpers.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -594,9 +594,9 @@ function createIgnoreResult(filePath, baseDir) {
594594
const isInNodeModules = baseDir && path.dirname(path.relative(baseDir, filePath)).split(path.sep).includes("node_modules");
595595

596596
if (isInNodeModules) {
597-
message = "File ignored by default because it is located under the node_modules directory. Use ignore pattern \"!**/node_modules/\" to override.";
597+
message = "File ignored by default because it is located under the node_modules directory. Use ignore pattern \"!**/node_modules/\" to disable file ignore settings or use \"--no-warn-ignored\" to suppress this warning.";
598598
} else {
599-
message = "File ignored because of a matching ignore pattern. Use \"--no-ignore\" to override.";
599+
message = "File ignored because of a matching ignore pattern. Use \"--no-ignore\" to disable file ignore settings or use \"--no-warn-ignored\" to suppress this warning.";
600600
}
601601

602602
return {
@@ -676,6 +676,7 @@ function processOptions({
676676
overrideConfigFile = null,
677677
plugins = {},
678678
reportUnusedDisableDirectives = null, // ← should be null by default because if it's a string then it overrides the 'reportUnusedDisableDirectives' setting in config files. And we cannot use `overrideConfig.reportUnusedDisableDirectives` instead because we cannot configure the `error` severity with that.
679+
warnIgnored = true,
679680
...unknownOptions
680681
}) {
681682
const errors = [];
@@ -781,6 +782,9 @@ function processOptions({
781782
) {
782783
errors.push("'reportUnusedDisableDirectives' must be any of \"error\", \"warn\", \"off\", and null.");
783784
}
785+
if (typeof warnIgnored !== "boolean") {
786+
errors.push("'warnIgnored' must be a boolean.");
787+
}
784788
if (errors.length > 0) {
785789
throw new ESLintInvalidOptionsError(errors);
786790
}
@@ -802,7 +806,8 @@ function processOptions({
802806
globInputPaths,
803807
ignore,
804808
ignorePatterns,
805-
reportUnusedDisableDirectives
809+
reportUnusedDisableDirectives,
810+
warnIgnored
806811
};
807812
}
808813

lib/eslint/flat-eslint.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ const LintResultCache = require("../cli-engine/lint-result-cache");
8484
* when a string.
8585
* @property {Record<string,Plugin>} [plugins] An array of plugin implementations.
8686
* @property {"error" | "warn" | "off"} [reportUnusedDisableDirectives] the severity to report unused eslint-disable directives.
87+
* @property {boolean} warnIgnored Show warnings when the file list includes ignored files
8788
*/
8889

8990
//------------------------------------------------------------------------------
@@ -749,7 +750,8 @@ class FlatESLint {
749750
fixTypes,
750751
reportUnusedDisableDirectives,
751752
globInputPaths,
752-
errorOnUnmatchedPattern
753+
errorOnUnmatchedPattern,
754+
warnIgnored
753755
} = eslintOptions;
754756
const startTime = Date.now();
755757
const fixTypesSet = fixTypes ? new Set(fixTypes) : null;
@@ -795,7 +797,11 @@ class FlatESLint {
795797
* pattern, then notify the user.
796798
*/
797799
if (ignored) {
798-
return createIgnoreResult(filePath, cwd);
800+
if (warnIgnored) {
801+
return createIgnoreResult(filePath, cwd);
802+
}
803+
804+
return void 0;
799805
}
800806

801807
const config = configs.getConfig(filePath);
@@ -908,7 +914,7 @@ class FlatESLint {
908914

909915
const {
910916
filePath,
911-
warnIgnored = false,
917+
warnIgnored,
912918
...unknownOptions
913919
} = options || {};
914920

@@ -922,7 +928,7 @@ class FlatESLint {
922928
throw new Error("'options.filePath' must be a non-empty string or undefined");
923929
}
924930

925-
if (typeof warnIgnored !== "boolean") {
931+
if (typeof warnIgnored !== "boolean" && typeof warnIgnored !== "undefined") {
926932
throw new Error("'options.warnIgnored' must be a boolean or undefined");
927933
}
928934

@@ -937,15 +943,18 @@ class FlatESLint {
937943
allowInlineConfig,
938944
cwd,
939945
fix,
940-
reportUnusedDisableDirectives
946+
reportUnusedDisableDirectives,
947+
warnIgnored: constructorWarnIgnored
941948
} = eslintOptions;
942949
const results = [];
943950
const startTime = Date.now();
944951
const resolvedFilename = path.resolve(cwd, filePath || "__placeholder__.js");
945952

946953
// Clear the last used config arrays.
947954
if (resolvedFilename && await this.isPathIgnored(resolvedFilename)) {
948-
if (warnIgnored) {
955+
const shouldWarnIgnored = typeof warnIgnored === "boolean" ? warnIgnored : constructorWarnIgnored;
956+
957+
if (shouldWarnIgnored) {
949958
results.push(createIgnoreResult(resolvedFilename, cwd));
950959
}
951960
} else {

lib/options.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ const optionator = require("optionator");
5555
* @property {string} [stdinFilename] Specify filename to process STDIN as
5656
* @property {boolean} quiet Report errors only
5757
* @property {boolean} [version] Output the version number
58+
* @property {boolean} warnIgnored Show warnings when the file list includes ignored files
5859
* @property {string[]} _ Positional filenames or patterns
5960
*/
6061

@@ -139,6 +140,17 @@ module.exports = function(usingFlatConfig) {
139140
};
140141
}
141142

143+
let warnIgnoredFlag;
144+
145+
if (usingFlatConfig) {
146+
warnIgnoredFlag = {
147+
option: "warn-ignored",
148+
type: "Boolean",
149+
default: "true",
150+
description: "Suppress warnings when the file list includes ignored files"
151+
};
152+
}
153+
142154
return optionator({
143155
prepend: "eslint [options] file.js [file.js] [dir]",
144156
defaults: {
@@ -349,6 +361,7 @@ module.exports = function(usingFlatConfig) {
349361
default: "false",
350362
description: "Exit with exit code 2 in case of fatal error"
351363
},
364+
warnIgnoredFlag,
352365
{
353366
option: "debug",
354367
type: "Boolean",

tests/lib/cli.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,32 @@ describe("cli", () => {
801801
assert.isFalse(log.info.called);
802802
assert.strictEqual(exit, 0);
803803
});
804+
805+
it(`should suppress the warning if --no-warn-ignored is passed with configType:${configType}`, async () => {
806+
const options = useFlatConfig
807+
? `--config ${getFixturePath("eslint.config_with_ignores.js")}`
808+
: `--ignore-path ${getFixturePath(".eslintignore")}`;
809+
const filePath = getFixturePath("passing.js");
810+
const exit = await cli.execute(`${options} --no-warn-ignored ${filePath}`, null, useFlatConfig);
811+
812+
assert.isFalse(log.info.called);
813+
814+
// When eslintrc is used, we get an exit code of 2 because the --no-warn-ignored option is unrecognized.
815+
assert.strictEqual(exit, useFlatConfig ? 0 : 2);
816+
});
817+
818+
it(`should suppress the warning if --no-warn-ignored is passed and an ignored file is passed via stdin with configType:${configType}`, async () => {
819+
const options = useFlatConfig
820+
? `--config ${getFixturePath("eslint.config_with_ignores.js")}`
821+
: `--ignore-path ${getFixturePath(".eslintignore")}`;
822+
const filePath = getFixturePath("passing.js");
823+
const exit = await cli.execute(`${options} --no-warn-ignored --stdin --stdin-filename ${filePath}`, "foo", useFlatConfig);
824+
825+
assert.isFalse(log.info.called);
826+
827+
// When eslintrc is used, we get an exit code of 2 because the --no-warn-ignored option is unrecognized.
828+
assert.strictEqual(exit, useFlatConfig ? 0 : 2);
829+
});
804830
});
805831

806832
describe("when given a pattern to ignore", () => {

0 commit comments

Comments
 (0)