|
| 1 | +use lazy_regex::Regex; |
1 | 2 | use oxc_ast::{AstKind, ast::Statement};
|
2 | 3 | use oxc_diagnostics::OxcDiagnostic;
|
3 | 4 | use oxc_macros::declare_oxc_lint;
|
@@ -142,8 +143,10 @@ impl Rule for SwitchCaseBraces {
|
142 | 143 | };
|
143 | 144 |
|
144 | 145 | 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); |
147 | 150 | ctx.diagnostic_with_fix(
|
148 | 151 | switch_case_braces_diagnostic_missing_braces(span),
|
149 | 152 | |fixer| {
|
@@ -251,6 +254,21 @@ fn test() {
|
251 | 254 | None,
|
252 | 255 | ),
|
253 | 256 | ("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 | + ), |
254 | 272 | ];
|
255 | 273 |
|
256 | 274 | Tester::new(SwitchCaseBraces::NAME, SwitchCaseBraces::PLUGIN, pass, fail)
|
|
0 commit comments