Skip to content

Commit 1288958

Browse files
committed
refactor(linter/switch-case-braces): remove usage of regex to improve performance
1 parent f08261d commit 1288958

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use lazy_regex::Regex;
21
use oxc_ast::{AstKind, ast::Statement};
32
use oxc_diagnostics::OxcDiagnostic;
43
use oxc_macros::declare_oxc_lint;
@@ -104,9 +103,6 @@ impl Rule for SwitchCaseBraces {
104103
return;
105104
}
106105

107-
let switch_clause_regex =
108-
Regex::new(r#"(?:case|default)\s*(?:`[^`]+`|'[^']+'|"[^"]+"|[^:]*):"#).unwrap();
109-
110106
for case in &switch.cases {
111107
if case.consequent.is_empty() {
112108
continue;
@@ -146,11 +142,13 @@ impl Rule for SwitchCaseBraces {
146142
};
147143

148144
if self.always_braces && missing_braces {
149-
let colon_end = u32::try_from(
150-
switch_clause_regex.find(ctx.source_range(case.span)).unwrap().end(),
145+
let colon = u32::try_from(
146+
ctx.source_range(Span::new(case.span.start, case.consequent[0].span().start))
147+
.rfind(':')
148+
.unwrap(),
151149
)
152150
.unwrap();
153-
let span = Span::sized(case.span.start, colon_end);
151+
let span = Span::sized(case.span.start, colon + 1);
154152
ctx.diagnostic_with_fix(
155153
switch_case_braces_diagnostic_missing_braces(span),
156154
|fixer| {

0 commit comments

Comments
 (0)