-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
fix: Throw error when invalid flags passed #18705
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
Conversation
✅ Deploy Preview for docs-eslint canceled.
|
flags.forEach(flag => { | ||
if (inactiveFlags.has(flag)) { | ||
throw new Error(`The flag '${flag}' is inactive: ${inactiveFlags.get(flag)}`); | ||
} | ||
|
||
if (!activeFlags.has(flag)) { | ||
throw new Error(`Unknown flag '${flag}'.`); | ||
} | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We also already have these checks in cli.js, where unknown flags result in exit code 2 but inactive flags only log a warning:
Lines 492 to 508 in 53b1ff0
if (eslintOptions.flags) { | |
debug("Checking for inactive flags"); | |
for (const flag of eslintOptions.flags) { | |
if (inactiveFlags.has(flag)) { | |
log.warn(`InactiveFlag: The '${flag}' flag is no longer active: ${inactiveFlags.get(flag)}`); | |
continue; | |
} | |
if (activeFlags.has(flag)) { | |
continue; | |
} | |
log.error(`InvalidFlag: The '${flag}' flag is invalid.`); | |
return 2; | |
} | |
} |
Now, for inactive flags passed on the command line, ESLint would log both a warning (from cli.js) and an error (from linter):
$ node bin/eslint Makefile.js --flag test_only_old
InactiveFlag: The 'test_only_old' flag is no longer active: Used only for testing.
Oops! Something went wrong! :(
ESLint: 9.7.0
Error: The flag 'test_only_old' is inactive: Used only for testing.
at C:\projects\eslint\lib\linter\linter.js:1288:23
at Array.forEach (<anonymous>)
at new Linter (C:\projects\eslint\lib\linter\linter.js:1286:15)
at new ESLint (C:\projects\eslint\lib\eslint\eslint.js:596:24)
at Object.execute (C:\projects\eslint\lib\cli.js:510:24)
at async main (C:\projects\eslint\bin\eslint.js:153:22)
Perhaps we should sync the behavior to avoid duplicate logs, or maybe just remove the checks in cli.js (if we don't expect to have any cli-level-only flags).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, these two tests are failing (at least for me locally, CI is broken at the moment) because now they result in throwing an error:
Lines 1911 to 1923 in 53b1ff0
it("should emit a warning when an inactive flag is used", async () => { | |
const configPath = getFixturePath("eslint.config.js"); | |
const filePath = getFixturePath("passing.js"); | |
const input = `--flag test_only_old --config ${configPath} ${filePath}`; | |
const exitCode = await cli.execute(input, null, true); | |
sinon.assert.calledOnce(log.warn); | |
const formattedOutput = log.warn.firstCall.args[0]; | |
assert.include(formattedOutput, `InactiveFlag: The 'test_only_old' flag is no longer active: ${inactiveFlags.get("test_only_old")}`); | |
assert.strictEqual(exitCode, 0); | |
}); |
eslint/tests/lib/eslint/eslint.js
Lines 330 to 334 in 53b1ff0
it("should return false if the flag is present and inactive", () => { | |
eslint = new ESLint({ cwd: getFixturePath(), flags: ["test_only_old"] }); | |
assert.strictEqual(eslint.hasFlag("test_only_old"), false); | |
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, I'll update these.
This test still needs to be updated: eslint/tests/lib/eslint/eslint.js Lines 330 to 334 in 53b1ff0
|
Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>
Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks!
Prerequisites checklist
What is the purpose of this pull request? (put an "X" next to an item)
[x] Documentation update
[x] Bug fix (template)
[ ] New rule (template)
[ ] Changes an existing rule (template)
[ ] Add autofix to a rule
[ ] Add a CLI option
[ ] Add something to the core
[ ] Other, please explain:
What changes did you make? (Give an overview)
Linter
now validatesflags
and throws an error for any inactive or unknown flags. Previously, these flags were ignored, which made it difficult to track down problems.Is there anything you'd like reviewers to focus on?