Skip to content

Commit

Permalink
perf: prevent duplicate create regex
Browse files Browse the repository at this point in the history
  • Loading branch information
SyMind committed Sep 20, 2024
1 parent fd2be5b commit 0dd1c77
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 28 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ swc_core = { version = "0.100.1", features = [
thiserror = "1.0.56"
linked-hash-map = { version = "0.5.6", features = ["serde_impl"] }
linked_hash_set = "0.1.4"
lazy_static = "1.5.0"

[dev-dependencies]
codspeed-criterion-compat = "2.7.2"
Expand Down
22 changes: 15 additions & 7 deletions crates/core/src/core/state.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::path::Path;

use lazy_static::lazy_static;
use regex::Regex;

#[cfg(feature = "node")]
Expand Down Expand Up @@ -80,16 +81,20 @@ const IDENTIFIER: &str = r"([\p{Alpha}\p{N}_]|$)";
const SEPARATORS: &str = r"[_.\- ]+";

fn pascal_case(input: &str) -> String {
let separators_and_identifier =
Regex::new(format!("{}{}", SEPARATORS, IDENTIFIER).as_str()).unwrap();
let numbers_and_identifier = Regex::new(format!("(\\d+){}", IDENTIFIER).as_str()).unwrap();
let result = separators_and_identifier
lazy_static! {
static ref SEPARATORS_AND_IDENTIFIER_REGEX: Regex =
Regex::new(&format!("{}{}", SEPARATORS, IDENTIFIER)).unwrap();
static ref NUMBERS_AND_IDENTIFIER_REGEX: Regex =
Regex::new(&format!("(\\d+){}", IDENTIFIER)).unwrap();
}

let result = SEPARATORS_AND_IDENTIFIER_REGEX
.replace_all(input, |caps: &regex::Captures| {
let identifier = caps.get(1).unwrap().as_str();
identifier.to_uppercase()
})
.to_string();
let result = numbers_and_identifier
let result = NUMBERS_AND_IDENTIFIER_REGEX
.replace_all(&result, |caps: &regex::Captures| {
let num = caps.get(1).unwrap().as_str();
let identifier = caps.get(2).unwrap().as_str();
Expand All @@ -100,8 +105,11 @@ fn pascal_case(input: &str) -> String {
}

fn get_component_name(file_path: &str) -> String {
let valid_char_regex = Regex::new(r"[^a-zA-Z0-9 _-]").unwrap();
let file_name = valid_char_regex
lazy_static! {
static ref VALID_CHAR_REGEX_REGEX: Regex = Regex::new(r"[^a-zA-Z0-9 _-]").unwrap();
}

let file_name = VALID_CHAR_REGEX_REGEX
.replace_all(
Path::new(file_path)
.file_prefix()
Expand Down
21 changes: 14 additions & 7 deletions crates/core/src/hast_to_swc_ast/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::collections::HashMap;

use lazy_static::lazy_static;
use regex::{Captures, Regex};
use swc_core::common::SyntaxContext;
use swc_core::{
Expand All @@ -19,8 +20,10 @@ use self::string_to_object_style::*;
use self::util::*;

fn kebab_case(str: &str) -> String {
let kebab_regex = Regex::new(r"[A-Z\u00C0-\u00D6\u00D8-\u00DE]").unwrap();
kebab_regex
lazy_static! {
static ref KEBAB_REGEX: Regex = Regex::new(r"[A-Z\u00C0-\u00D6\u00D8-\u00DE]").unwrap();
}
KEBAB_REGEX
.replace_all(str, |caps: &Captures| {
format!("-{}", &caps[0].to_lowercase())
})
Expand All @@ -35,8 +38,10 @@ fn convert_aria_attribute(kebab_key: &str) -> String {
}

fn replace_spaces(s: &str) -> String {
let spaces_regex = Regex::new(r"[\t\r\n\u0085\u2028\u2029]+").unwrap();
spaces_regex.replace_all(s, |_: &Captures| " ").to_string()
lazy_static! {
static ref SPACES_REGEX: Regex = Regex::new(r"[\t\r\n\u0085\u2028\u2029]+").unwrap();
}
SPACES_REGEX.replace_all(s, |_: &Captures| " ").to_string()
}

fn get_value(attr_name: &str, value: &JsWord) -> JSXAttrValue {
Expand Down Expand Up @@ -68,10 +73,12 @@ fn get_value(attr_name: &str, value: &JsWord) -> JSXAttrValue {
}

fn text(n: &swc_xml::ast::Text) -> Option<JSXElementChild> {
let value = n.data.to_string();
lazy_static! {
static ref SPACE_REGEX: Regex = Regex::new(r"^\s+$").unwrap();
}

let space_regex = Regex::new(r"^\s+$").unwrap();
if space_regex.is_match(&value) {
let value = n.data.to_string();
if SPACE_REGEX.is_match(&value) {
return None;
}

Expand Down
28 changes: 16 additions & 12 deletions crates/core/src/hast_to_swc_ast/string_to_object_style.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
use lazy_static::lazy_static;
use regex::{Captures, Regex};
use swc_core::{common::DUMMY_SP, ecma::ast::*};

use super::util::*;

const PX_REGEX: &str = r#"^\d+px$"#;
const MS_REGEX: &str = r#"^-ms-"#;
const VAR_REGEX: &str = r#"^--"#;

pub fn hyphen_to_camel_case(s: &str) -> String {
let regex = Regex::new(r#"-(.)"#).unwrap();
regex
lazy_static! {
static ref HYPHEN_REGEX: Regex = Regex::new(r#"-(.)"#).unwrap();
}
HYPHEN_REGEX
.replace_all(s, |caps: &Captures| caps[1].to_uppercase())
.into()
}

// Format style key into JSX style object key.
pub fn format_key(key: &str) -> PropName {
let var_regex = Regex::new(VAR_REGEX).unwrap();
if var_regex.is_match(key) {
lazy_static! {
static ref VAR_REGEX: Regex = Regex::new(r#"^--"#).unwrap();
static ref MS_REGEX: Regex = Regex::new(r#"^-ms-"#).unwrap();
}

if VAR_REGEX.is_match(key) {
return PropName::Str(Str {
span: DUMMY_SP,
value: key.into(),
Expand All @@ -26,17 +29,18 @@ pub fn format_key(key: &str) -> PropName {
}

let mut key = key.to_lowercase();
let ms_regex = Regex::new(MS_REGEX).unwrap();
if ms_regex.is_match(&key) {
if MS_REGEX.is_match(&key) {
key = key[1..].into();
}

PropName::Ident(IdentName::new(hyphen_to_camel_case(&key).into(), DUMMY_SP))
}

fn is_convertible_pixel_value(s: &str) -> bool {
let px_regex = Regex::new(PX_REGEX).unwrap();
px_regex.is_match(s)
lazy_static! {
static ref PX_REGEX: Regex = Regex::new(r#"^\d+px$"#).unwrap();
}
PX_REGEX.is_match(s)
}

// Format style value into JSX style object value.
Expand Down
7 changes: 5 additions & 2 deletions crates/core/src/hast_to_swc_ast/util.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use lazy_static::lazy_static;
use regex::Regex;

pub fn is_numeric(s: &str) -> bool {
let regex = Regex::new(r#"^(\-|\+)?\d+(\.\d+)?$"#).unwrap();
regex.is_match(s)
lazy_static! {
static ref NUMERIC_REGEX: Regex = Regex::new(r#"^(\-|\+)?\d+(\.\d+)?$"#).unwrap();
}
NUMERIC_REGEX.is_match(s)
}

0 comments on commit 0dd1c77

Please sign in to comment.