Skip to content

Commit

Permalink
feat: no-case-declarations add suggestions (#18388)
Browse files Browse the repository at this point in the history
* feat: `no-case-declarations` add suggestions

* Add extra test case for declared identifiers
  • Loading branch information
JoshuaKGoldberg authored Apr 26, 2024
1 parent a498f35 commit 8485d76
Show file tree
Hide file tree
Showing 2 changed files with 260 additions and 19 deletions.
14 changes: 13 additions & 1 deletion lib/rules/no-case-declarations.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ module.exports = {
url: "https://eslint.org/docs/latest/rules/no-case-declarations"
},

hasSuggestions: true,

schema: [],

messages: {
addBrackets: "Add {} brackets around the case block.",
unexpected: "Unexpected lexical declaration in case block."
}
},
Expand Down Expand Up @@ -53,7 +56,16 @@ module.exports = {
if (isLexicalDeclaration(statement)) {
context.report({
node: statement,
messageId: "unexpected"
messageId: "unexpected",
suggest: [
{
messageId: "addBrackets",
fix: fixer => [
fixer.insertTextBefore(node.consequent[0], "{ "),
fixer.insertTextAfter(node.consequent.at(-1), " }")
]
}
]
});
}
}
Expand Down
265 changes: 247 additions & 18 deletions tests/lib/rules/no-case-declarations.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,79 +37,308 @@ ruleTester.run("no-case-declarations", rule, {
languageOptions: { ecmaVersion: 6 }
},
`
switch (a) {
case 1:
case 2: {}
}
`,
`
switch (a) {
case 1: var x;
}
`
],
invalid: [
{
code: `
switch (a) {
case 1:
case 2: {}
{}
function f() {}
break;
}
`,
`
errors: [{
messageId: "unexpected",
type: "FunctionDeclaration",
suggestions: [
{
messageId: "addBrackets",
output: `
switch (a) {
case 1: var x;
case 1:
{ {}
function f() {}
break; }
}
`
],
invalid: [
}
]
}]
},
{
code: `
switch (a) {
case 1:
{}
function f() {}
break;
case 2:
let x;
}
`,
errors: [{ messageId: "unexpected", type: "FunctionDeclaration" }]
languageOptions: { ecmaVersion: 6 },
errors: [{
messageId: "unexpected",
type: "VariableDeclaration",
suggestions: [
{
messageId: "addBrackets",
output: `
switch (a) {
case 1:
case 2:
{ let x; }
}
`
}
]
}]
},
{
code: `
switch (a) {
case 1:
let x;
case 2:
let y;
}
`,
languageOptions: { ecmaVersion: 6 },
errors: [
{
messageId: "unexpected",
type: "VariableDeclaration",
suggestions: [
{
messageId: "addBrackets",
output: `
switch (a) {
case 1:
{ let x; }
case 2:
let y;
}
`
}
]
},
{
messageId: "unexpected",
type: "VariableDeclaration",
suggestions: [
{
messageId: "addBrackets",
output: `
switch (a) {
case 1:
let x;
case 2:
{ let y; }
}
`
}
]
}
]
},
{
code: `
switch (a) {
case 1:
let x;
default:
let y;
}
`,
languageOptions: { ecmaVersion: 6 },
errors: [{ messageId: "unexpected", type: "VariableDeclaration" }]
errors: [
{
messageId: "unexpected",
type: "VariableDeclaration",
suggestions: [
{
messageId: "addBrackets",
output: `
switch (a) {
case 1:
{ let x; }
default:
let y;
}
`
}
]
},
{
messageId: "unexpected",
type: "VariableDeclaration",
suggestions: [
{
messageId: "addBrackets",
output: `
switch (a) {
case 1:
let x;
default:
{ let y; }
}
`
}
]
}
]
},
{
code: "switch (a) { case 1: let x = 1; break; }",
languageOptions: { ecmaVersion: 6 },
errors: [{ messageId: "unexpected", type: "VariableDeclaration" }]
errors: [{
messageId: "unexpected",
type: "VariableDeclaration",
suggestions: [
{
messageId: "addBrackets",
output: "switch (a) { case 1: { let x = 1; break; } }"
}
]
}]
},
{
code: "switch (a) { default: let x = 2; break; }",
languageOptions: { ecmaVersion: 6 },
errors: [{ messageId: "unexpected", type: "VariableDeclaration" }]
errors: [{
messageId: "unexpected",
type: "VariableDeclaration",
suggestions: [
{
messageId: "addBrackets",
output: "switch (a) { default: { let x = 2; break; } }"
}
]
}]
},
{
code: "switch (a) { case 1: const x = 1; break; }",
languageOptions: { ecmaVersion: 6 },
errors: [{ messageId: "unexpected", type: "VariableDeclaration" }]
errors: [{
messageId: "unexpected",
type: "VariableDeclaration",
suggestions: [
{
messageId: "addBrackets",
output: "switch (a) { case 1: { const x = 1; break; } }"
}
]
}]
},
{
code: "switch (a) { default: const x = 2; break; }",
languageOptions: { ecmaVersion: 6 },
errors: [{ messageId: "unexpected", type: "VariableDeclaration" }]
errors: [{
messageId: "unexpected",
type: "VariableDeclaration",
suggestions: [
{
messageId: "addBrackets",
output: "switch (a) { default: { const x = 2; break; } }"
}
]
}]
},
{
code: "switch (a) { case 1: function f() {} break; }",
languageOptions: { ecmaVersion: 6 },
errors: [{ messageId: "unexpected", type: "FunctionDeclaration" }]
errors: [{
messageId: "unexpected",
type: "FunctionDeclaration",
suggestions: [
{
messageId: "addBrackets",
output: "switch (a) { case 1: { function f() {} break; } }"
}
]
}]
},
{
code: "switch (a) { default: function f() {} break; }",
languageOptions: { ecmaVersion: 6 },
errors: [{ messageId: "unexpected", type: "FunctionDeclaration" }]
errors: [{
messageId: "unexpected",
type: "FunctionDeclaration",
suggestions: [
{
messageId: "addBrackets",
output: "switch (a) { default: { function f() {} break; } }"
}
]
}]
},
{
code: "switch (a) { case 1: class C {} break; }",
languageOptions: { ecmaVersion: 6 },
errors: [{ messageId: "unexpected", type: "ClassDeclaration" }]
errors: [{
messageId: "unexpected",
type: "ClassDeclaration",
suggestions: [
{
messageId: "addBrackets",
output: "switch (a) { case 1: { class C {} break; } }"
}
]
}]
},
{
code: "switch (a) { default: class C {} break; }",
languageOptions: { ecmaVersion: 6 },
errors: [{ messageId: "unexpected", type: "ClassDeclaration" }]
errors: [{
messageId: "unexpected",
type: "ClassDeclaration",
suggestions: [
{
messageId: "addBrackets",
output: "switch (a) { default: { class C {} break; } }"
}
]
}]
},

// https://github.com/eslint/eslint/pull/18388#issuecomment-2075356456
{
code: `
switch ("foo") {
case "bar":
function baz() { }
break;
default:
baz();
}
`,
languageOptions: { ecmaVersion: "latest" },
errors: [{
messageId: "unexpected",
type: "FunctionDeclaration",
suggestions: [
{
messageId: "addBrackets",
output: `
switch ("foo") {
case "bar":
{ function baz() { }
break; }
default:
baz();
}
`
}
]
}]
}
]
});

0 comments on commit 8485d76

Please sign in to comment.