Skip to content

Commit 9dcfaee

Browse files
committed
fix(linter/switch-case-braces): add support for string including colon on case expression
The following case was wrongly formatted. // Input case "scope:type": // ... break; // Output case "scope: {type": // ... break; } // Expected case "scope:type": { // ... break; }
1 parent 265d6a6 commit 9dcfaee

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

crates/oxc_linter/src/rules/unicorn/switch_case_braces.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use lazy_regex::Regex;
12
use oxc_ast::{AstKind, ast::Statement};
23
use oxc_diagnostics::OxcDiagnostic;
34
use oxc_macros::declare_oxc_lint;
@@ -142,8 +143,10 @@ impl Rule for SwitchCaseBraces {
142143
};
143144

144145
if self.always_braces && missing_braces {
145-
let colon = u32::try_from(ctx.source_range(case.span).find(':').unwrap()).unwrap();
146-
let span = Span::sized(case.span.start, colon + 1);
146+
let regex = Regex::new(r#"(case|default)\s*(`.+`|'.+'|".+"|[^:]*):"#).unwrap();
147+
let colon_end =
148+
u32::try_from(regex.find(ctx.source_range(case.span)).unwrap().end()).unwrap();
149+
let span = Span::sized(case.span.start, colon_end);
147150
ctx.diagnostic_with_fix(
148151
switch_case_braces_diagnostic_missing_braces(span),
149152
|fixer| {
@@ -251,6 +254,21 @@ fn test() {
251254
None,
252255
),
253256
("switch(foo){ case 1: {}; break; }", "switch(foo){ case 1: { {}; break;} }", None),
257+
(
258+
"switch(something) { case `scope:type`: doSomething();}",
259+
"switch(something) { case `scope:type`: { doSomething();}}",
260+
None,
261+
),
262+
(
263+
"switch(something) { case \"scope:type\": doSomething();}",
264+
"switch(something) { case \"scope:type\": { doSomething();}}",
265+
None,
266+
),
267+
(
268+
"switch(something) { case 'scope:type': doSomething();}",
269+
"switch(something) { case 'scope:type': { doSomething();}}",
270+
None,
271+
),
254272
];
255273

256274
Tester::new(SwitchCaseBraces::NAME, SwitchCaseBraces::PLUGIN, pass, fail)

0 commit comments

Comments
 (0)