Skip to content

Commit

Permalink
feat: Add --no-warn-ignored CLI option for flat config (#17569)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
domnantas committed Oct 1, 2023
1 parent 1866da5 commit 0a9c433
Show file tree
Hide file tree
Showing 9 changed files with 170 additions and 25 deletions.
13 changes: 13 additions & 0 deletions docs/src/use/command-line-interface.md
Expand Up @@ -110,6 +110,7 @@ Miscellaneous:
--env-info Output execution environment information - default: false
--no-error-on-unmatched-pattern Prevent errors when pattern is unmatched
--exit-on-fatal-error Exit with exit code 2 in case of fatal error - default: false
--no-warn-ignored Suppress warnings when the file list includes ignored files. *Flat Config Mode Only*
--debug Output debugging information
-h, --help Show help
-v, --version Output the version number
Expand Down Expand Up @@ -703,6 +704,18 @@ This option causes ESLint to exit with exit code 2 if one or more fatal parsing
npx eslint --exit-on-fatal-error file.js
```

#### `--no-warn-ignored`

**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.

* **Argument Type**: No argument.

##### `--no-warn-ignored` example

```shell
npx eslint --no-warn-ignored --max-warnings 0 ignored-file.js
```

#### `--debug`

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.
Expand Down
2 changes: 1 addition & 1 deletion docs/src/use/configure/ignore.md
Expand Up @@ -149,7 +149,7 @@ You'll see this warning:

```text
foo.js
0:0 warning File ignored because of a matching ignore pattern. Use "--no-ignore" to override.
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.
✖ 1 problem (0 errors, 1 warning)
```
Expand Down
8 changes: 6 additions & 2 deletions lib/cli.js
Expand Up @@ -91,7 +91,8 @@ async function translateOptions({
reportUnusedDisableDirectives,
resolvePluginsRelativeTo,
rule,
rulesdir
rulesdir,
warnIgnored
}, configType) {

let overrideConfig, overrideConfigFile;
Expand Down Expand Up @@ -182,6 +183,7 @@ async function translateOptions({

if (configType === "flat") {
options.ignorePatterns = ignorePattern;
options.warnIgnored = warnIgnored;
} else {
options.resolvePluginsRelativeTo = resolvePluginsRelativeTo;
options.rulePaths = rulesdir;
Expand Down Expand Up @@ -385,7 +387,9 @@ const cli = {
if (useStdin) {
results = await engine.lintText(text, {
filePath: options.stdinFilename,
warnIgnored: true

// flatConfig respects CLI flag and constructor warnIgnored, eslintrc forces true for backwards compatibility
warnIgnored: usingFlatConfig ? void 0 : true
});
} else {
results = await engine.lintFiles(files);
Expand Down
11 changes: 8 additions & 3 deletions lib/eslint/eslint-helpers.js
Expand Up @@ -594,9 +594,9 @@ function createIgnoreResult(filePath, baseDir) {
const isInNodeModules = baseDir && path.dirname(path.relative(baseDir, filePath)).split(path.sep).includes("node_modules");

if (isInNodeModules) {
message = "File ignored by default because it is located under the node_modules directory. Use ignore pattern \"!**/node_modules/\" to override.";
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.";
} else {
message = "File ignored because of a matching ignore pattern. Use \"--no-ignore\" to override.";
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.";
}

return {
Expand Down Expand Up @@ -676,6 +676,7 @@ function processOptions({
overrideConfigFile = null,
plugins = {},
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.
warnIgnored = true,
...unknownOptions
}) {
const errors = [];
Expand Down Expand Up @@ -781,6 +782,9 @@ function processOptions({
) {
errors.push("'reportUnusedDisableDirectives' must be any of \"error\", \"warn\", \"off\", and null.");
}
if (typeof warnIgnored !== "boolean") {
errors.push("'warnIgnored' must be a boolean.");
}
if (errors.length > 0) {
throw new ESLintInvalidOptionsError(errors);
}
Expand All @@ -802,7 +806,8 @@ function processOptions({
globInputPaths,
ignore,
ignorePatterns,
reportUnusedDisableDirectives
reportUnusedDisableDirectives,
warnIgnored
};
}

Expand Down
21 changes: 15 additions & 6 deletions lib/eslint/flat-eslint.js
Expand Up @@ -84,6 +84,7 @@ const LintResultCache = require("../cli-engine/lint-result-cache");
* when a string.
* @property {Record<string,Plugin>} [plugins] An array of plugin implementations.
* @property {"error" | "warn" | "off"} [reportUnusedDisableDirectives] the severity to report unused eslint-disable directives.
* @property {boolean} warnIgnored Show warnings when the file list includes ignored files
*/

//------------------------------------------------------------------------------
Expand Down Expand Up @@ -749,7 +750,8 @@ class FlatESLint {
fixTypes,
reportUnusedDisableDirectives,
globInputPaths,
errorOnUnmatchedPattern
errorOnUnmatchedPattern,
warnIgnored
} = eslintOptions;
const startTime = Date.now();
const fixTypesSet = fixTypes ? new Set(fixTypes) : null;
Expand Down Expand Up @@ -795,7 +797,11 @@ class FlatESLint {
* pattern, then notify the user.
*/
if (ignored) {
return createIgnoreResult(filePath, cwd);
if (warnIgnored) {
return createIgnoreResult(filePath, cwd);
}

return void 0;
}

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

const {
filePath,
warnIgnored = false,
warnIgnored,
...unknownOptions
} = options || {};

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

if (typeof warnIgnored !== "boolean") {
if (typeof warnIgnored !== "boolean" && typeof warnIgnored !== "undefined") {
throw new Error("'options.warnIgnored' must be a boolean or undefined");
}

Expand All @@ -937,15 +943,18 @@ class FlatESLint {
allowInlineConfig,
cwd,
fix,
reportUnusedDisableDirectives
reportUnusedDisableDirectives,
warnIgnored: constructorWarnIgnored
} = eslintOptions;
const results = [];
const startTime = Date.now();
const resolvedFilename = path.resolve(cwd, filePath || "__placeholder__.js");

// Clear the last used config arrays.
if (resolvedFilename && await this.isPathIgnored(resolvedFilename)) {
if (warnIgnored) {
const shouldWarnIgnored = typeof warnIgnored === "boolean" ? warnIgnored : constructorWarnIgnored;

if (shouldWarnIgnored) {
results.push(createIgnoreResult(resolvedFilename, cwd));
}
} else {
Expand Down
13 changes: 13 additions & 0 deletions lib/options.js
Expand Up @@ -55,6 +55,7 @@ const optionator = require("optionator");
* @property {string} [stdinFilename] Specify filename to process STDIN as
* @property {boolean} quiet Report errors only
* @property {boolean} [version] Output the version number
* @property {boolean} warnIgnored Show warnings when the file list includes ignored files
* @property {string[]} _ Positional filenames or patterns
*/

Expand Down Expand Up @@ -139,6 +140,17 @@ module.exports = function(usingFlatConfig) {
};
}

let warnIgnoredFlag;

if (usingFlatConfig) {
warnIgnoredFlag = {
option: "warn-ignored",
type: "Boolean",
default: "true",
description: "Suppress warnings when the file list includes ignored files"
};
}

return optionator({
prepend: "eslint [options] file.js [file.js] [dir]",
defaults: {
Expand Down Expand Up @@ -349,6 +361,7 @@ module.exports = function(usingFlatConfig) {
default: "false",
description: "Exit with exit code 2 in case of fatal error"
},
warnIgnoredFlag,
{
option: "debug",
type: "Boolean",
Expand Down
26 changes: 26 additions & 0 deletions tests/lib/cli.js
Expand Up @@ -801,6 +801,32 @@ describe("cli", () => {
assert.isFalse(log.info.called);
assert.strictEqual(exit, 0);
});

it(`should suppress the warning if --no-warn-ignored is passed with configType:${configType}`, async () => {
const options = useFlatConfig
? `--config ${getFixturePath("eslint.config_with_ignores.js")}`
: `--ignore-path ${getFixturePath(".eslintignore")}`;
const filePath = getFixturePath("passing.js");
const exit = await cli.execute(`${options} --no-warn-ignored ${filePath}`, null, useFlatConfig);

assert.isFalse(log.info.called);

// When eslintrc is used, we get an exit code of 2 because the --no-warn-ignored option is unrecognized.
assert.strictEqual(exit, useFlatConfig ? 0 : 2);
});

it(`should suppress the warning if --no-warn-ignored is passed and an ignored file is passed via stdin with configType:${configType}`, async () => {
const options = useFlatConfig
? `--config ${getFixturePath("eslint.config_with_ignores.js")}`
: `--ignore-path ${getFixturePath(".eslintignore")}`;
const filePath = getFixturePath("passing.js");
const exit = await cli.execute(`${options} --no-warn-ignored --stdin --stdin-filename ${filePath}`, "foo", useFlatConfig);

assert.isFalse(log.info.called);

// When eslintrc is used, we get an exit code of 2 because the --no-warn-ignored option is unrecognized.
assert.strictEqual(exit, useFlatConfig ? 0 : 2);
});
});

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

0 comments on commit 0a9c433

Please sign in to comment.