Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add --no-warn-ignored CLI option for flat config #17569

Merged
merged 13 commits into from Oct 1, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 override. Use \"--no-warn-ignored\" to suppress this warning.
domnantas marked this conversation as resolved.
Show resolved Hide resolved

✖ 1 problem (0 errors, 1 warning)
```
Expand Down
4 changes: 3 additions & 1 deletion 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
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 override. 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 override. Use \"--no-warn-ignored\" to suppress this warning.";
domnantas marked this conversation as resolved.
Show resolved Hide resolved
}

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.");
}
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved
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 warning 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")) {
domnantas marked this conversation as resolved.
Show resolved Hide resolved
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 warning 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 warning when the file list includes ignored files"
domnantas marked this conversation as resolved.
Show resolved Hide resolved
};
}

return optionator({
prepend: "eslint [options] file.js [file.js] [dir]",
defaults: {
Expand Down Expand Up @@ -257,6 +269,7 @@ module.exports = function(usingFlatConfig) {
default: "-1",
description: "Number of warnings to trigger nonzero exit code"
},
warnIgnoredFlag,
{
heading: "Output"
},
Expand Down
13 changes: 13 additions & 0 deletions tests/lib/cli.js
Expand Up @@ -801,6 +801,19 @@ 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);
});
});

describe("when given a pattern to ignore", () => {
Expand Down
59 changes: 48 additions & 11 deletions tests/lib/eslint/flat-eslint.js
domnantas marked this conversation as resolved.
Show resolved Hide resolved
Expand Up @@ -369,7 +369,31 @@ describe("FlatESLint", () => {
assert.strictEqual(results.length, 1);
assert.strictEqual(results[0].filePath, getFixturePath("passing.js"));
assert.strictEqual(results[0].messages[0].severity, 1);
assert.strictEqual(results[0].messages[0].message, "File ignored because of a matching ignore pattern. Use \"--no-ignore\" to override.");
assert.strictEqual(results[0].messages[0].message, "File ignored because of a matching ignore pattern. Use \"--no-ignore\" to override. Use \"--no-warn-ignored\" to suppress this warning.");
assert.strictEqual(results[0].messages[0].output, void 0);
assert.strictEqual(results[0].errorCount, 0);
assert.strictEqual(results[0].warningCount, 1);
assert.strictEqual(results[0].fatalErrorCount, 0);
assert.strictEqual(results[0].fixableErrorCount, 0);
assert.strictEqual(results[0].fixableWarningCount, 0);
assert.strictEqual(results[0].usedDeprecatedRules.length, 0);
assert.strictEqual(results[0].suppressedMessages.length, 0);
});

it("should return a warning when given a filename by --stdin-filename in excluded files list if constructor warnIgnored is false, but lintText warnIgnored is true", async () => {
eslint = new FlatESLint({
cwd: getFixturePath(".."),
overrideConfigFile: "fixtures/eslint.config_with_ignores.js",
warnIgnored: false
});

const options = { filePath: "fixtures/passing.js", warnIgnored: true };
const results = await eslint.lintText("var bar = foo;", options);

assert.strictEqual(results.length, 1);
assert.strictEqual(results[0].filePath, getFixturePath("passing.js"));
assert.strictEqual(results[0].messages[0].severity, 1);
assert.strictEqual(results[0].messages[0].message, "File ignored because of a matching ignore pattern. Use \"--no-ignore\" to override. Use \"--no-warn-ignored\" to suppress this warning.");
assert.strictEqual(results[0].messages[0].output, void 0);
assert.strictEqual(results[0].errorCount, 0);
assert.strictEqual(results[0].warningCount, 1);
Expand Down Expand Up @@ -397,18 +421,31 @@ describe("FlatESLint", () => {
assert.strictEqual(results.length, 0);
});

it("should suppress excluded file warnings by default", async () => {
it("should not return a warning when given a filename by --stdin-filename in excluded files list if constructor warnIgnored is false", async () => {
eslint = new FlatESLint({
cwd: getFixturePath(".."),
overrideConfigFile: "fixtures/eslint.config_with_ignores.js"
overrideConfigFile: "fixtures/eslint.config_with_ignores.js",
warnIgnored: false
});
const options = { filePath: "fixtures/passing.js" };
const results = await eslint.lintText("var bar = foo;", options);

// should not report anything because there are no errors
// should not report anything because the warning is suppressed
assert.strictEqual(results.length, 0);
});

it("should show excluded file warnings by default", async () => {
eslint = new FlatESLint({
cwd: getFixturePath(".."),
overrideConfigFile: "fixtures/eslint.config_with_ignores.js"
});
const options = { filePath: "fixtures/passing.js" };
const results = await eslint.lintText("var bar = foo;", options);

assert.strictEqual(results.length, 1);
assert.strictEqual(results[0].messages[0].message, "File ignored because of a matching ignore pattern. Use \"--no-ignore\" to override. Use \"--no-warn-ignored\" to suppress this warning.");
});

it("should return a message when given a filename by --stdin-filename in excluded files list and ignore is off", async () => {
eslint = new FlatESLint({
cwd: getFixturePath(".."),
Expand Down Expand Up @@ -685,7 +722,7 @@ describe("FlatESLint", () => {
ignore: false
});
const results = await eslint.lintText("var bar = foo;", { filePath: "node_modules/passing.js", warnIgnored: true });
const expectedMsg = "File ignored by default because it is located under the node_modules directory. Use ignore pattern \"!**/node_modules/\" to override.";
const expectedMsg = "File ignored by default because it is located under the node_modules directory. Use ignore pattern \"!**/node_modules/\" to override. Use \"--no-warn-ignored\" to suppress this warning.";

assert.strictEqual(results.length, 1);
assert.strictEqual(results[0].filePath, getFixturePath("node_modules/passing.js"));
Expand Down Expand Up @@ -1311,7 +1348,7 @@ describe("FlatESLint", () => {
cwd: getFixturePath("cli-engine")
});
const results = await eslint.lintFiles(["node_modules/foo.js"]);
const expectedMsg = "File ignored by default because it is located under the node_modules directory. Use ignore pattern \"!**/node_modules/\" to override.";
const expectedMsg = "File ignored by default because it is located under the node_modules directory. Use ignore pattern \"!**/node_modules/\" to override. Use \"--no-warn-ignored\" to suppress this warning.";

assert.strictEqual(results.length, 1);
assert.strictEqual(results[0].errorCount, 0);
Expand All @@ -1329,7 +1366,7 @@ describe("FlatESLint", () => {
cwd: getFixturePath("cli-engine")
});
const results = await eslint.lintFiles(["nested_node_modules/subdir/node_modules/text.js"]);
const expectedMsg = "File ignored by default because it is located under the node_modules directory. Use ignore pattern \"!**/node_modules/\" to override.";
const expectedMsg = "File ignored by default because it is located under the node_modules directory. Use ignore pattern \"!**/node_modules/\" to override. Use \"--no-warn-ignored\" to suppress this warning.";

assert.strictEqual(results.length, 1);
assert.strictEqual(results[0].errorCount, 0);
Expand All @@ -1348,7 +1385,7 @@ describe("FlatESLint", () => {
ignorePatterns: ["*.js"]
});
const results = await eslint.lintFiles(["node_modules_cleaner.js"]);
const expectedMsg = "File ignored because of a matching ignore pattern. Use \"--no-ignore\" to override.";
const expectedMsg = "File ignored because of a matching ignore pattern. Use \"--no-ignore\" to override. Use \"--no-warn-ignored\" to suppress this warning.";

assert.strictEqual(results.length, 1);
assert.strictEqual(results[0].errorCount, 0);
Expand Down Expand Up @@ -1483,7 +1520,7 @@ describe("FlatESLint", () => {
assert.strictEqual(results.length, 1);
assert.strictEqual(results[0].filePath, filePath);
assert.strictEqual(results[0].messages[0].severity, 1);
assert.strictEqual(results[0].messages[0].message, "File ignored because of a matching ignore pattern. Use \"--no-ignore\" to override.");
assert.strictEqual(results[0].messages[0].message, "File ignored because of a matching ignore pattern. Use \"--no-ignore\" to override. Use \"--no-warn-ignored\" to suppress this warning.");
assert.strictEqual(results[0].errorCount, 0);
assert.strictEqual(results[0].warningCount, 1);
assert.strictEqual(results[0].fatalErrorCount, 0);
Expand All @@ -1503,7 +1540,7 @@ describe("FlatESLint", () => {
assert.strictEqual(results.length, 1);
assert.strictEqual(results[0].filePath, filePath);
assert.strictEqual(results[0].messages[0].severity, 1);
assert.strictEqual(results[0].messages[0].message, "File ignored because of a matching ignore pattern. Use \"--no-ignore\" to override.");
assert.strictEqual(results[0].messages[0].message, "File ignored because of a matching ignore pattern. Use \"--no-ignore\" to override. Use \"--no-warn-ignored\" to suppress this warning.");
assert.strictEqual(results[0].errorCount, 0);
assert.strictEqual(results[0].warningCount, 1);
assert.strictEqual(results[0].fatalErrorCount, 0);
Expand Down Expand Up @@ -5400,7 +5437,7 @@ describe("FlatESLint", () => {
{
ruleId: null,
fatal: false,
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 override. Use \"--no-warn-ignored\" to suppress this warning.",
severity: 1,
nodeType: null
}
Expand Down
14 changes: 14 additions & 0 deletions tests/lib/options.js
Expand Up @@ -415,4 +415,18 @@ describe("options", () => {
});
});

describe("--no-warn-ignored", () => {
it("should return false when --no-warn-ignored is passed", () => {
const currentOptions = flatOptions.parse("--no-warn-ignored");

assert.isFalse(currentOptions.warnIgnored);
});

it("should return true when --warn-ignored is passed", () => {
const currentOptions = flatOptions.parse("--warn-ignored");

assert.isTrue(currentOptions.warnIgnored);
});
});

});