Skip to content

Commit 6432707

Browse files
committedMar 24, 2025·
refactor(rust): use lazy-regex (#10004)
1 parent 707a776 commit 6432707

33 files changed

+174
-199
lines changed
 

Diff for: ‎Cargo.lock

+28-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: ‎Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ itoa = "1.0.14"
189189
javascript-globals = "1.0.0"
190190
json-strip-comments = "1.0.4"
191191
language-tags = "0.3.2"
192+
lazy-regex = "3.4.1"
192193
lazy_static = "1.5.0"
193194
log = "0.4.25"
194195
markdown = "1.0.0-alpha.22"
@@ -205,7 +206,6 @@ pico-args = "0.5.0"
205206
prettyplease = "0.2.29"
206207
project-root = "0.2.2"
207208
rayon = "1.10.0"
208-
regex = "1.11.1"
209209
ropey = "1.6.1"
210210
rust-lapper = "1.1.0"
211211
ryu-js = "1.0.1"

Diff for: ‎apps/oxlint/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ mimalloc-safe = { workspace = true, optional = true, features = ["skip_collect_o
4848

4949
[dev-dependencies]
5050
insta = { workspace = true }
51-
regex = { workspace = true }
51+
lazy-regex = { workspace = true }
5252

5353
[features]
5454
default = []

Diff for: ‎apps/oxlint/src/tester.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::runner::Runner;
55
#[cfg(test)]
66
use cow_utils::CowUtils;
77
#[cfg(test)]
8-
use regex::Regex;
8+
use lazy_regex::Regex;
99
#[cfg(test)]
1010
use std::{env, path::PathBuf};
1111
#[cfg(test)]

Diff for: ‎crates/oxc_linter/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ itertools = { workspace = true }
4848
javascript-globals = { workspace = true }
4949
json-strip-comments = { workspace = true }
5050
language-tags = { workspace = true }
51+
lazy-regex = { workspace = true }
5152
lazy_static = { workspace = true }
5253
memchr = { workspace = true }
5354
nonmax = { workspace = true }
5455
phf = { workspace = true, features = ["macros"] }
5556
rayon = { workspace = true }
56-
regex = { workspace = true }
5757
rust-lapper = { workspace = true }
5858
rustc-hash = { workspace = true }
5959
schemars = { workspace = true, features = ["indexmap2"] }

Diff for: ‎crates/oxc_linter/src/rules/eslint/default_case.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
use lazy_regex::{Regex, RegexBuilder};
12
use oxc_ast::AstKind;
23
use oxc_diagnostics::OxcDiagnostic;
34
use oxc_macros::declare_oxc_lint;
45
use oxc_span::Span;
5-
use regex::{Regex, RegexBuilder};
66

77
use crate::{AstNode, context::LintContext, rule::Rule};
88

Diff for: ‎crates/oxc_linter/src/rules/eslint/new_cap.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use crate::{AstNode, context::LintContext, rule::Rule};
2+
use lazy_regex::Regex;
23
use oxc_ast::{
34
AstKind,
45
ast::{ChainElement, ComputedMemberExpression, Expression},
56
};
67
use oxc_diagnostics::OxcDiagnostic;
78
use oxc_macros::declare_oxc_lint;
89
use oxc_span::{CompactStr, GetSpan, Span};
9-
use regex::Regex;
1010

1111
fn new_cap_diagnostic(span: Span, cap: &GetCapResult) -> OxcDiagnostic {
1212
let msg = if *cap == GetCapResult::Lower {

Diff for: ‎crates/oxc_linter/src/rules/eslint/no_fallthrough.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::ops::Range;
22

33
use cow_utils::CowUtils;
44
use itertools::Itertools;
5+
use lazy_regex::Regex;
56
use oxc_ast::{
67
AstKind,
78
ast::{Statement, SwitchCase, SwitchStatement},
@@ -16,7 +17,6 @@ use oxc_cfg::{
1617
use oxc_diagnostics::OxcDiagnostic;
1718
use oxc_macros::declare_oxc_lint;
1819
use oxc_span::{GetSpan, Span};
19-
use regex::Regex;
2020
use rustc_hash::{FxHashMap, FxHashSet};
2121

2222
use crate::{AstNode, context::LintContext, rule::Rule};

Diff for: ‎crates/oxc_linter/src/rules/eslint/no_nonoctal_decimal_escape.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
use lazy_static::lazy_static;
1+
use lazy_regex::{Captures, Lazy, Regex, lazy_regex};
22
use oxc_ast::AstKind;
33
use oxc_diagnostics::OxcDiagnostic;
44
use oxc_macros::declare_oxc_lint;
55
use oxc_span::Span;
6-
use regex::{Captures, Regex};
76

87
use crate::{AstNode, context::LintContext, rule::Rule};
98

@@ -93,13 +92,11 @@ fn quick_test(s: &str) -> bool {
9392
false
9493
}
9594

95+
static NONOCTAL_REGEX: Lazy<Regex> =
96+
lazy_regex!(r"(?:[^\\]|(?P<previousEscape>\\.))*?(?P<decimalEscape>\\[89])");
97+
9698
#[expect(clippy::cast_possible_truncation)]
9799
fn check_string(ctx: &LintContext<'_>, string: &str) {
98-
lazy_static! {
99-
static ref NONOCTAL_REGEX: Regex =
100-
Regex::new(r"(?:[^\\]|(?P<previousEscape>\\.))*?(?P<decimalEscape>\\[89])").unwrap();
101-
}
102-
103100
// Need at least 2 characters
104101
if string.len() <= 1 {
105102
return;

Diff for: ‎crates/oxc_linter/src/rules/eslint/no_restricted_imports.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use ignore::gitignore::GitignoreBuilder;
2+
use lazy_regex::Regex;
23
use oxc_ast::{
34
AstKind,
45
ast::{ImportOrExportKind, StringLiteral, TSImportEqualsDeclaration, TSModuleReference},
56
};
67
use oxc_diagnostics::OxcDiagnostic;
78
use oxc_macros::declare_oxc_lint;
89
use oxc_span::{CompactStr, Span};
9-
use regex::Regex;
1010
use rustc_hash::FxHashMap;
1111
use serde::{Deserialize, Deserializer, de::Error};
1212
use serde_json::Value;

Diff for: ‎crates/oxc_linter/src/rules/eslint/no_unused_vars/ignored.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use lazy_regex::Regex;
12
use oxc_ast::{
23
AstKind,
34
ast::{
@@ -6,7 +7,6 @@ use oxc_ast::{
67
ObjectAssignmentTarget,
78
},
89
};
9-
use regex::Regex;
1010

1111
use super::{NoUnusedVars, Symbol, options::IgnorePattern};
1212

Diff for: ‎crates/oxc_linter/src/rules/eslint/no_unused_vars/options.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::{borrow::Cow, ops::Deref};
22

3+
use lazy_regex::{Regex, RegexBuilder};
34
use oxc_diagnostics::OxcDiagnostic;
4-
use regex::Regex;
55
use serde_json::Value;
66

77
/// See [ESLint - no-unused-vars config schema](https://github.com/eslint/eslint/blob/53b1ff047948e36682fade502c949f4e371e53cd/lib/rules/no-unused-vars.js#L61)
@@ -290,15 +290,13 @@ where
290290
}
291291

292292
impl TryFrom<Option<&str>> for IgnorePattern<Regex> {
293-
type Error = regex::Error;
293+
type Error = lazy_regex::regex::Error;
294294

295295
fn try_from(value: Option<&str>) -> Result<Self, Self::Error> {
296296
match value {
297297
None => Ok(Self::None),
298298
Some("^_") => Ok(Self::Default),
299-
Some(pattern) => {
300-
regex::RegexBuilder::new(pattern).unicode(true).build().map(Self::Some)
301-
}
299+
Some(pattern) => RegexBuilder::new(pattern).build().map(Self::Some),
302300
}
303301
}
304302
}

Diff for: ‎crates/oxc_linter/src/rules/jest/expect_expect.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use cow_utils::CowUtils;
2+
use lazy_regex::Regex;
23
use oxc_ast::{
34
AstKind,
45
ast::{CallExpression, Expression, Statement},
56
};
67
use oxc_diagnostics::OxcDiagnostic;
78
use oxc_macros::declare_oxc_lint;
89
use oxc_span::{CompactStr, GetSpan, Span};
9-
use regex::Regex;
1010
use rustc_hash::FxHashSet;
1111

1212
use crate::{

Diff for: ‎crates/oxc_linter/src/rules/jest/no_commented_out_tests.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
use lazy_static::lazy_static;
1+
use lazy_regex::{Lazy, Regex, lazy_regex};
22
use oxc_diagnostics::OxcDiagnostic;
33
use oxc_macros::declare_oxc_lint;
44
use oxc_span::Span;
5-
use regex::Regex;
65

76
use crate::{context::LintContext, rule::Rule};
87

@@ -53,13 +52,12 @@ declare_oxc_lint!(
5352
suspicious
5453
);
5554

55+
// /^\s*[xf]?(test|it|describe)(\.\w+|\[['"]\w+['"]\])?\s*\(/mu
56+
static RE: Lazy<Regex> =
57+
lazy_regex!(r#"(?mu)^\s*[xf]?(test|it|describe)(\.\w+|\[['"]\w+['"]\])?\s*\("#);
58+
5659
impl Rule for NoCommentedOutTests {
5760
fn run_once(&self, ctx: &LintContext) {
58-
lazy_static! {
59-
// /^\s*[xf]?(test|it|describe)(\.\w+|\[['"]\w+['"]\])?\s*\(/mu
60-
static ref RE: Regex =
61-
Regex::new(r#"(?mu)^\s*[xf]?(test|it|describe)(\.\w+|\[['"]\w+['"]\])?\s*\("#).unwrap();
62-
}
6361
let comments = ctx.comments();
6462
let commented_tests = comments.iter().filter_map(|comment| {
6563
let text = ctx.source_range(comment.content_span());

Diff for: ‎crates/oxc_linter/src/rules/jest/no_large_snapshots.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use std::{ops::Deref, path::Path};
22

3+
use lazy_regex::Regex;
34
use oxc_ast::{
45
AstKind,
56
ast::{Expression, ExpressionStatement, MemberExpression},
67
};
78
use oxc_diagnostics::OxcDiagnostic;
89
use oxc_macros::declare_oxc_lint;
910
use oxc_span::{CompactStr, GetSpan, Span};
10-
use regex::Regex;
1111
use rustc_hash::FxHashMap;
1212

1313
use crate::{

Diff for: ‎crates/oxc_linter/src/rules/jest/valid_title.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
use std::hash::Hash;
22

33
use cow_utils::CowUtils;
4+
use lazy_regex::Regex;
45
use oxc_ast::{
56
AstKind,
67
ast::{Argument, BinaryExpression, Expression},
78
};
89
use oxc_diagnostics::OxcDiagnostic;
910
use oxc_macros::declare_oxc_lint;
1011
use oxc_span::{CompactStr, GetSpan, Span};
11-
use regex::Regex;
1212
use rustc_hash::FxHashMap;
1313

1414
use crate::{
@@ -309,7 +309,7 @@ fn validate_title(
309309
}
310310

311311
if !valid_title.disallowed_words.is_empty() {
312-
let Ok(disallowed_words_reg) = regex::Regex::new(&format!(
312+
let Ok(disallowed_words_reg) = Regex::new(&format!(
313313
r"(?iu)\b(?:{})\b",
314314
valid_title.disallowed_words.join("|").cow_replace('.', r"\.")
315315
)) else {

0 commit comments

Comments
 (0)
Please sign in to comment.