Skip to content

Commit b951868

Browse files
committed
fix(linter): fix no-fallthrough rule, when the default condition is not last (#12793)
1 parent b9f200f commit b951868

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

crates/oxc_linter/src/rules/eslint/no_fallthrough.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,7 @@ fn get_switch_semantic_cases(
434434
/* default */ Option<BlockNodeId>,
435435
/* exit */ Option<BlockNodeId>,
436436
) {
437+
let nodes = ctx.nodes();
437438
let cfg = ctx.cfg();
438439
let graph = cfg.graph();
439440
let has_default = switch.cases.iter().any(SwitchCase::is_default_case);
@@ -459,7 +460,7 @@ fn get_switch_semantic_cases(
459460
.instructions()
460461
.first()
461462
.and_then(|it| it.node_id)
462-
.map(|id| ctx.nodes().parent_kind(id))
463+
.map(|id| nodes.parent_kind(id))
463464
.and_then(|it| match it {
464465
AstKind::SwitchCase(case) => Some(case),
465466
_ => None,
@@ -482,6 +483,7 @@ fn get_switch_semantic_cases(
482483
} else {
483484
(None, exit)
484485
};
486+
cfg_ids.sort_by_key(|cfg_id| nodes.iter().position(|it| it.cfg_id() == *cfg_id ));
485487
(cfg_ids, FxHashMap::from_iter(tests), default, exit)
486488
}
487489

@@ -535,6 +537,7 @@ fn test() {
535537
"switch (foo) { case 0: a(); \n// eslint-disable-next-line no-fallthrough\n case 1: }",
536538
None,
537539
),
540+
("switch(foo) { case 0: default: a(); break; case 1: b(); }", None),
538541
(
539542
"switch(foo) { case 0: a(); /* no break */ case 1: b(); }",
540543
Some(serde_json::json!([{

0 commit comments

Comments
 (0)