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

fix: make rule severity case-sensitive in flat config #17619

Merged
merged 1 commit into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 1 addition & 3 deletions lib/config/flat-config-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,7 @@ class InvalidRuleSeverityError extends Error {
* @throws {InvalidRuleSeverityError} If the value isn't a valid rule severity.
*/
function assertIsRuleSeverity(ruleId, value) {
const severity = typeof value === "string"
? ruleSeverities.get(value.toLowerCase())
: ruleSeverities.get(value);
const severity = ruleSeverities.get(value);

if (typeof severity === "undefined") {
throw new InvalidRuleSeverityError(ruleId, value);
Expand Down
11 changes: 11 additions & 0 deletions tests/lib/config/flat-config-array.js
Original file line number Diff line number Diff line change
Expand Up @@ -1726,6 +1726,17 @@ describe("FlatConfigArray", () => {
], "Key \"rules\": Key \"foo\": Expected severity of \"off\", 0, \"warn\", 1, \"error\", or 2.");
});

it("should error when a string rule severity is not in lowercase", async () => {

await assertInvalidConfig([
{
rules: {
foo: "Error"
}
}
], "Key \"rules\": Key \"foo\": Expected severity of \"off\", 0, \"warn\", 1, \"error\", or 2.");
});

it("should error when an invalid rule severity is set in an array", async () => {

await assertInvalidConfig([
Expand Down
39 changes: 39 additions & 0 deletions tests/lib/linter/linter.js
Original file line number Diff line number Diff line change
Expand Up @@ -1906,6 +1906,22 @@ describe("Linter", () => {
assert.strictEqual(suppressedMessages.length, 0);
});

it("should enable rule configured using a string severity that contains uppercase letters", () => {
const code = "/*eslint no-alert: \"Error\"*/ alert('test');";
const config = { rules: {} };

const messages = linter.verify(code, config, filename);
const suppressedMessages = linter.getSuppressedMessages();

assert.strictEqual(messages.length, 1);
assert.strictEqual(messages[0].ruleId, "no-alert");
assert.strictEqual(messages[0].severity, 2);
assert.strictEqual(messages[0].message, "Unexpected alert.");
assert.include(messages[0].nodeType, "CallExpression");

assert.strictEqual(suppressedMessages.length, 0);
});

it("rules should not change initial config", () => {
const config = { rules: { strict: 2 } };
const codeA = "/*eslint strict: 0*/ function bar() { return 2; }";
Expand Down Expand Up @@ -12206,6 +12222,29 @@ describe("Linter with FlatConfigArray", () => {
assert.strictEqual(suppressedMessages.length, 0);
});

it("should report a violation when a rule is configured using a string severity that contains uppercase letters", () => {
const messages = linter.verify("/*eslint no-alert: \"Error\"*/ alert('test');", {});
const suppressedMessages = linter.getSuppressedMessages();

assert.deepStrictEqual(
messages,
[
{
severity: 2,
ruleId: "no-alert",
message: "Inline configuration for rule \"no-alert\" is invalid:\n\tExpected severity of \"off\", 0, \"warn\", 1, \"error\", or 2. You passed \"Error\".\n",
line: 1,
column: 1,
endLine: 1,
endColumn: 29,
nodeType: null
}
]
);

assert.strictEqual(suppressedMessages.length, 0);
});

it("should report a violation when the config violates a rule's schema", () => {
const messages = linter.verify("/* eslint no-alert: [error, {nonExistentPropertyName: true}]*/", {});
const suppressedMessages = linter.getSuppressedMessages();
Expand Down