diff --git a/crates/oxc_linter/src/rules/unicorn/switch_case_braces.rs b/crates/oxc_linter/src/rules/unicorn/switch_case_braces.rs index b0a6b04314f7b..0b9f8e18ab9dc 100644 --- a/crates/oxc_linter/src/rules/unicorn/switch_case_braces.rs +++ b/crates/oxc_linter/src/rules/unicorn/switch_case_braces.rs @@ -142,7 +142,12 @@ impl Rule for SwitchCaseBraces { }; if self.always_braces && missing_braces { - let colon = u32::try_from(ctx.source_range(case.span).find(':').unwrap()).unwrap(); + let colon = u32::try_from( + ctx.source_range(Span::new(case.span.start, case.consequent[0].span().start)) + .rfind(':') + .unwrap(), + ) + .unwrap(); let span = Span::sized(case.span.start, colon + 1); ctx.diagnostic_with_fix( switch_case_braces_diagnostic_missing_braces(span), @@ -251,6 +256,21 @@ fn test() { None, ), ("switch(foo){ case 1: {}; break; }", "switch(foo){ case 1: { {}; break;} }", None), + ( + "switch(something) { case `scope:type`: doSomething();}", + "switch(something) { case `scope:type`: { doSomething();}}", + None, + ), + ( + "switch(something) { case \"scope:type\": doSomething();}", + "switch(something) { case \"scope:type\": { doSomething();}}", + None, + ), + ( + "switch(something) { case 'scope:type': doSomething();}", + "switch(something) { case 'scope:type': { doSomething();}}", + None, + ), ]; Tester::new(SwitchCaseBraces::NAME, SwitchCaseBraces::PLUGIN, pass, fail)