Skip to content

Commit 618ee87

Browse files
authored
fix(linter/array-callback-return): fix handling of default case in switch statements for array-callback-return rule (#13081)
fix handling of default case in switch statements for array-callback-return rule - fixes(#13075) - Consider cases following the default case in switch statements
1 parent 8c141e5 commit 618ee87

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

crates/oxc_linter/src/rules/eslint/array_callback_return/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ fn test() {
311311
None,
312312
),
313313
("foo.every(function() { try { bar(); } finally { return true; } })", None),
314+
("foo.every(function() { switch (a) { default: case0: return true; } })", None),
314315
(
315316
"Array.from(x, function() { return; })",
316317
Some(serde_json::json!([{"allowImplicit": true}])),

crates/oxc_linter/src/rules/eslint/array_callback_return/return_checker.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,17 +156,25 @@ pub fn check_statement(statement: &Statement) -> StatementReturnStatus {
156156
// 2. There is a default case that returns
157157
Statement::SwitchStatement(stmt) => {
158158
let mut case_statuses = vec![];
159+
// The default case maybe is not the last case and fallthrough
160+
let mut default_case_fallthrough_continue = false;
159161
let mut default_case_status = StatementReturnStatus::NotReturn;
160162

161163
let mut current_case_status = StatementReturnStatus::NotReturn;
162164
for case in &stmt.cases {
163165
let branch_terminated = check_switch_case(case, &mut current_case_status);
164-
165166
if case.is_default_case() {
166-
default_case_status = current_case_status;
167-
// Cases below the default case are not considered.
168-
break;
167+
if branch_terminated {
168+
default_case_status = current_case_status;
169+
// Cases below the default case are not considered.
170+
break;
171+
}
172+
default_case_fallthrough_continue = true;
169173
} else if branch_terminated {
174+
if default_case_fallthrough_continue {
175+
default_case_status = current_case_status;
176+
break;
177+
}
170178
case_statuses.push(current_case_status);
171179
current_case_status = StatementReturnStatus::NotReturn;
172180
} // Falls through to next case, accumulating lattice

0 commit comments

Comments
 (0)