From 9090c286ede9973a4c820213fb4d008ceb6fb516 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCrg=C3=BCn=20Day=C4=B1o=C4=9Flu?= Date: Sat, 5 Oct 2024 09:49:11 +0200 Subject: [PATCH] fix: switch break --- src/index.js | 5 +---- src/rules/switch-break.js | 17 ++++++----------- test/rules/switch-break.test.js | 14 ++++++++++++++ 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/src/index.js b/src/index.js index 06fc2fe..5882c6f 100644 --- a/src/index.js +++ b/src/index.js @@ -89,10 +89,6 @@ export default [ "UnaryExpression[operator='+']", "SequenceExpression", "LabeledStatement", - { - selector: 'SwitchCase > *.consequent[type!="BlockStatement"]', - message: "Switch cases without blocks are disallowed.", - }, ], "no-self-compare": "error", "one-var": ["error", "never"], @@ -285,6 +281,7 @@ export default [ "unicorn/prefer-ternary": "error", "unicorn/prefer-type-error": "error", "unicorn/require-array-join-separator": "error", + "unicorn/switch-case-braces": "error", "unicorn/text-encoding-identifier-case": "error", "unicorn/throw-new-error": "error", diff --git a/src/rules/switch-break.js b/src/rules/switch-break.js index a47e407..d24e55c 100644 --- a/src/rules/switch-break.js +++ b/src/rules/switch-break.js @@ -5,15 +5,6 @@ const controlFlowTypes = new Set([ "ThrowStatement", ]); -const checkLastStatement = (context, node, statement) => { - if (!statement || !controlFlowTypes.has(statement.type)) { - context.report({ - node: statement || node, - messageId: "missingBreakStatement", - }); - } -}; - export default { meta: { type: "problem", @@ -26,7 +17,6 @@ export default { return { SwitchCase: (node) => { if (!node.consequent.length) { - checkLastStatement(context, node, null); return; } @@ -41,7 +31,12 @@ export default { } } - checkLastStatement(context, node, lastStatement); + if (lastStatement && !controlFlowTypes.has(lastStatement.type)) { + context.report({ + node: lastStatement, + messageId: "missingBreakStatement", + }); + } }, }; }, diff --git a/test/rules/switch-break.test.js b/test/rules/switch-break.test.js index 6dc0536..1c04345 100644 --- a/test/rules/switch-break.test.js +++ b/test/rules/switch-break.test.js @@ -73,6 +73,18 @@ ruleTester.run("switch-break", rule, { } `, }, + { + code: ` + while (true) { + switch (value) { + case 1: + case 2: + console.log('123') + break; + } + } + `, + }, ], invalid: [ { @@ -99,6 +111,8 @@ ruleTester.run("switch-break", rule, { code: ` switch (value) { case 1: + case 2: + console.log('two'); } `, errors: [