Skip to content

Commit

Permalink
fix: switch break
Browse files Browse the repository at this point in the history
  • Loading branch information
gurgunday committed Oct 5, 2024
1 parent b711753 commit 9090c28
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
5 changes: 1 addition & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down Expand Up @@ -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",

Expand Down
17 changes: 6 additions & 11 deletions src/rules/switch-break.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -26,7 +17,6 @@ export default {
return {
SwitchCase: (node) => {
if (!node.consequent.length) {
checkLastStatement(context, node, null);
return;
}

Expand All @@ -41,7 +31,12 @@ export default {
}
}

checkLastStatement(context, node, lastStatement);
if (lastStatement && !controlFlowTypes.has(lastStatement.type)) {
context.report({
node: lastStatement,
messageId: "missingBreakStatement",
});
}
},
};
},
Expand Down
14 changes: 14 additions & 0 deletions test/rules/switch-break.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,18 @@ ruleTester.run("switch-break", rule, {
}
`,
},
{
code: `
while (true) {
switch (value) {
case 1:
case 2:
console.log('123')
break;
}
}
`,
},
],
invalid: [
{
Expand All @@ -99,6 +111,8 @@ ruleTester.run("switch-break", rule, {
code: `
switch (value) {
case 1:
case 2:
console.log('two');
}
`,
errors: [
Expand Down

0 comments on commit 9090c28

Please sign in to comment.