Skip to content

Commit be62d38

Browse files
committed
refactor(rust): remove usages of lazy_static (#10007)
1 parent 6432707 commit be62d38

File tree

7 files changed

+34
-35
lines changed

7 files changed

+34
-35
lines changed

Cargo.lock

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

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ ureq = { version = "3.0.5", default-features = false }
226226
walkdir = "2.5.0"
227227

228228
[workspace.metadata.cargo-shear]
229-
ignored = ["napi", "oxc_transform_napi", "oxc_parser_napi", "prettyplease"]
229+
ignored = ["napi", "oxc_transform_napi", "oxc_parser_napi", "prettyplease", "lazy_static"]
230230

231231
[profile.dev]
232232
# Disabling debug info speeds up local and CI builds,

crates/oxc_linter/src/rules/jsdoc/require_param.rs

+11-9
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
use std::sync::Mutex;
1+
use std::sync::{
2+
OnceLock, {RwLock, RwLockWriteGuard},
3+
};
24

35
use lazy_regex::Regex;
4-
use lazy_static::lazy_static;
6+
use rustc_hash::{FxHashMap, FxHashSet};
7+
use serde::Deserialize;
8+
59
use oxc_ast::{AstKind, ast::MethodDefinitionKind};
610
use oxc_diagnostics::{LabeledSpan, OxcDiagnostic};
711
use oxc_macros::declare_oxc_lint;
812
use oxc_semantic::{AstNode, JSDoc};
9-
use rustc_hash::{FxHashMap, FxHashSet};
10-
use serde::Deserialize;
1113

1214
use crate::{
1315
context::LintContext,
@@ -91,9 +93,9 @@ fn default_check_types_pattern() -> String {
9193
"^(?:[oO]bject|[aA]rray|PlainObject|Generic(?:Object|Array))$".to_string() // spellchecker:disable-line
9294
}
9395

94-
// For perf, cache regex is needed
95-
lazy_static! {
96-
static ref REGEX_CACHE: Mutex<FxHashMap<String, Regex>> = Mutex::new(FxHashMap::default());
96+
fn regex_cache() -> RwLockWriteGuard<'static, FxHashMap<String, Regex>> {
97+
static REGEX_CACHE: OnceLock<RwLock<FxHashMap<String, Regex>>> = OnceLock::new();
98+
REGEX_CACHE.get_or_init(|| RwLock::new(FxHashMap::default())).write().unwrap()
9799
}
98100

99101
impl Rule for RequireParam {
@@ -165,9 +167,9 @@ impl Rule for RequireParam {
165167
let shallow_tags =
166168
tags_to_check.iter().filter(|(name, _)| !name.contains('.')).collect::<Vec<_>>();
167169

168-
let mut regex_cache = REGEX_CACHE.lock().unwrap();
170+
let mut cache = regex_cache();
169171
let check_types_regex =
170-
regex_cache.entry(config.check_types_pattern.clone()).or_insert_with(|| {
172+
cache.entry(config.check_types_pattern.clone()).or_insert_with(|| {
171173
Regex::new(config.check_types_pattern.as_str())
172174
.expect("`config.checkTypesPattern` should be a valid regex pattern")
173175
});

crates/oxc_linter/src/rules/jsx_a11y/prefer_tag_over_role.rs

+8-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use lazy_static::lazy_static;
21
use phf::phf_map;
32

43
use oxc_ast::{
@@ -80,16 +79,14 @@ impl PreferTagOverRole {
8079
}
8180
}
8281

83-
lazy_static! {
84-
static ref ROLE_TO_TAG_MAP: phf::Map<&'static str, &'static str> = phf_map! {
85-
"checkbox" => "input",
86-
"button" => "button",
87-
"heading" => "h1,h2,h3,h4,h5,h6",
88-
"link" => "a,area",
89-
"rowgroup" => "tbody,tfoot,thead",
90-
"banner" => "header",
91-
};
92-
}
82+
const ROLE_TO_TAG_MAP: phf::Map<&'static str, &'static str> = phf_map! {
83+
"checkbox" => "input",
84+
"button" => "button",
85+
"heading" => "h1,h2,h3,h4,h5,h6",
86+
"link" => "a,area",
87+
"rowgroup" => "tbody,tfoot,thead",
88+
"banner" => "header",
89+
};
9390

9491
impl Rule for PreferTagOverRole {
9592
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {

crates/oxc_linter/src/rules/react/no_unknown_property.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
use std::borrow::Cow;
1+
use std::{borrow::Cow, sync::OnceLock};
22

33
use cow_utils::CowUtils;
44
use itertools::Itertools;
5-
use lazy_static::lazy_static;
65
use oxc_ast::{
76
AstKind,
87
ast::{JSXAttributeItem, JSXAttributeName, JSXElementName},
@@ -426,12 +425,15 @@ const DOM_PROPERTIES_IGNORE_CASE: [&str; 5] = [
426425
"webkitDirectory",
427426
];
428427

429-
lazy_static! {
430-
static ref DOM_PROPERTIES_LOWER_MAP: FxHashMap<Cow<'static, str>, &'static str> =
428+
fn dom_properties_lower_map() -> &'static FxHashMap<Cow<'static, str>, &'static str> {
429+
static DOM_PROPERTIES_LOWER_MAP: OnceLock<FxHashMap<Cow<'static, str>, &'static str>> =
430+
OnceLock::new();
431+
DOM_PROPERTIES_LOWER_MAP.get_or_init(|| {
431432
DOM_PROPERTIES_NAMES
432433
.iter()
433434
.map(|it| (it.cow_to_ascii_lowercase(), *it))
434-
.collect::<FxHashMap<_, _>>();
435+
.collect::<FxHashMap<_, _>>()
436+
})
435437
}
436438

437439
/// Checks if an attribute name is a valid `data-*` attribute:
@@ -544,7 +546,7 @@ impl Rule for NoUnknownProperty {
544546
return;
545547
}
546548

547-
DOM_PROPERTIES_LOWER_MAP
549+
dom_properties_lower_map()
548550
.get(&name.cow_to_ascii_lowercase())
549551
.or_else(|| DOM_ATTRIBUTES_TO_CAMEL.get(name))
550552
.map_or_else(

tasks/coverage/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ encoding_rs = { workspace = true }
3333
encoding_rs_io = { workspace = true }
3434
futures = { workspace = true }
3535
lazy-regex = { workspace = true }
36-
lazy_static = { workspace = true }
3736
pico-args = { workspace = true }
3837
rayon = { workspace = true }
3938
rustc-hash = { workspace = true }

tasks/coverage/src/typescript/meta.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use std::{fs, path::Path, sync::Arc};
44

5-
use lazy_regex::Regex;
5+
use lazy_regex::{Lazy, Regex, lazy_regex};
66
use rustc_hash::FxHashMap;
77

88
use oxc::{
@@ -15,11 +15,11 @@ use oxc::{
1515

1616
use crate::workspace_root;
1717

18-
lazy_static::lazy_static! {
19-
// Returns a match for a test option. Test options have the form `// @name: value`
20-
static ref META_OPTIONS: Regex = Regex::new(r"(?m)^/{2}[[:space:]]*@(?P<name>[[:word:]]+)[[:space:]]*:[[:space:]]*(?P<value>[^\r\n]*)").unwrap();
21-
static ref TEST_BRACES: Regex = Regex::new(r"^[[:space:]]*[{|}][[:space:]]*$").unwrap();
22-
}
18+
// Returns a match for a test option. Test options have the form `// @name: value`
19+
static META_OPTIONS: Lazy<Regex> = lazy_regex!(
20+
r"(?m)^/{2}[[:space:]]*@(?P<name>[[:word:]]+)[[:space:]]*:[[:space:]]*(?P<value>[^\r\n]*)"
21+
);
22+
// static TEST_BRACES: Lazy<Regex> = lazy_regex!(r"^[[:space:]]*[{|}][[:space:]]*$");
2323

2424
#[expect(unused)]
2525
#[derive(Debug)]

0 commit comments

Comments
 (0)