Skip to content

Commit 969b77b

Browse files
committedJul 11, 2024··
fix(no-conditional-statements): allow continue and break statements with labels to be considered "returning" (#846)
Handle labeled break statements and continue statements in switch cases in the no-conditional-statements rule.
1 parent b83aa68 commit 969b77b

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed
 

‎src/rules/no-conditional-statements.ts

+19-12
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
isContinueStatement,
1818
isExpressionStatement,
1919
isIfStatement,
20+
isLabeledStatement,
2021
isReturnStatement,
2122
isSwitchStatement,
2223
isThrowStatement,
@@ -195,18 +196,6 @@ function getIfBranchViolations(
195196
return violations.flatMap(incompleteBranchViolation);
196197
}
197198

198-
/**
199-
* Is the given statement, when inside a switch statement, a returning branch?
200-
*/
201-
function isSwitchReturningBranch(statement: TSESTree.Statement) {
202-
return (
203-
// Another instance of this rule will check nested switch statements.
204-
isSwitchStatement(statement) ||
205-
isReturnStatement(statement) ||
206-
isThrowStatement(statement)
207-
);
208-
}
209-
210199
/**
211200
* Get all of the violations in the given switch statement assuming switch
212201
* statements are allowed.
@@ -217,6 +206,8 @@ function getSwitchViolations(
217206
): RuleResult<keyof typeof errorMessages, Options>["descriptors"] {
218207
const isNeverExpressions = getIsNeverExpressions(context);
219208

209+
const label = isLabeledStatement(node.parent) ? node.parent.label.name : null;
210+
220211
const violations = node.cases.filter((branch) => {
221212
if (branch.consequent.length === 0) {
222213
return false;
@@ -241,6 +232,22 @@ function getSwitchViolations(
241232
});
242233

243234
return violations.flatMap(incompleteBranchViolation);
235+
236+
/**
237+
* Is the given statement, when inside a switch statement, a returning branch?
238+
*/
239+
function isSwitchReturningBranch(statement: TSESTree.Statement) {
240+
return (
241+
// Another instance of this rule will check nested switch statements.
242+
isSwitchStatement(statement) ||
243+
isReturnStatement(statement) ||
244+
isThrowStatement(statement) ||
245+
(isBreakStatement(statement) &&
246+
statement.label !== null &&
247+
statement.label.name !== label) ||
248+
isContinueStatement(statement)
249+
);
250+
}
244251
}
245252

246253
/**

‎src/utils/type-guards.ts

+6
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,12 @@ export function isIfStatement(
157157
return node.type === AST_NODE_TYPES.IfStatement;
158158
}
159159

160+
export function isLabeledStatement(
161+
node: TSESTree.Node,
162+
): node is TSESTree.LabeledStatement {
163+
return node.type === AST_NODE_TYPES.LabeledStatement;
164+
}
165+
160166
export function isMemberExpression(
161167
node: TSESTree.Node,
162168
): node is TSESTree.MemberExpression {

0 commit comments

Comments
 (0)
Please sign in to comment.