Skip to content

Commit 2e25dc8

Browse files
committed
fix(linter/switch-case-braces): move regex out of the for loop
1 parent 9dcfaee commit 2e25dc8

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ impl Rule for SwitchCaseBraces {
9696
}
9797

9898
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
99+
let switch_clause_regex =
100+
Regex::new(r#"(case|default)\s*(`.+`|'.+'|".+"|[^:]*):"#).unwrap();
101+
99102
let AstKind::SwitchStatement(switch) = node.kind() else {
100103
return;
101104
};
@@ -143,9 +146,10 @@ impl Rule for SwitchCaseBraces {
143146
};
144147

145148
if self.always_braces && missing_braces {
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 colon_end = u32::try_from(
150+
switch_clause_regex.find(ctx.source_range(case.span)).unwrap().end(),
151+
)
152+
.unwrap();
149153
let span = Span::sized(case.span.start, colon_end);
150154
ctx.diagnostic_with_fix(
151155
switch_case_braces_diagnostic_missing_braces(span),

0 commit comments

Comments
 (0)