Skip to content

Commit

Permalink
fix: make rule severity case-sensitive in flat config (#17619)
Browse files Browse the repository at this point in the history
Fixes #17570
  • Loading branch information
mdjermanovic authored Oct 6, 2023
1 parent 0edfe36 commit f976b2f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
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 @@ -1932,6 +1932,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 @@ -12353,6 +12369,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

0 comments on commit f976b2f

Please sign in to comment.