Skip to content

Commit

Permalink
perf: lazy static attr mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
SyMind committed Sep 21, 2024
1 parent 3c150e6 commit a36e9dd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
6 changes: 4 additions & 2 deletions crates/core/src/hast_to_swc_ast/mappings.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::collections::HashMap;
use lazy_static::lazy_static;

// From https://raw.githubusercontent.com/facebook/react/master/packages/react-dom/src/shared/possibleStandardNames.js
pub fn create_attr_mappings() -> HashMap<&'static str, &'static str> {
lazy_static! {
pub static ref ATTR_MAPPINGS: HashMap<&'static str, &'static str> =
HashMap::from([
// HTML
("accept", "accept"),
Expand Down Expand Up @@ -488,5 +490,5 @@ pub fn create_attr_mappings() -> HashMap<&'static str, &'static str> {
("ychannelselector", "yChannelSelector"),
("z", "z"),
("zoomandpan", "zoomAndPan"),
])
]);
}
10 changes: 5 additions & 5 deletions crates/core/src/hast_to_swc_ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ mod string_to_object_style;
mod util;

use self::decode_xml::*;
use self::mappings::*;
use self::mappings::ATTR_MAPPINGS;
use self::string_to_object_style::*;
use self::util::*;

Expand Down Expand Up @@ -92,14 +92,14 @@ fn text(n: &swc_xml::ast::Text) -> Option<JSXElementChild> {

pub struct HastVisitor {
jsx: Option<JSXElement>,
attr_mappings: HashMap<&'static str, &'static str>,
attr_mappings: &'static HashMap<&'static str, &'static str>,
}

impl HastVisitor {
fn new() -> Self {
Self {
jsx: None,
attr_mappings: create_attr_mappings(),
attr_mappings: &ATTR_MAPPINGS,
}
}

Expand Down Expand Up @@ -139,7 +139,7 @@ impl HastVisitor {

let opening = JSXOpeningElement {
span: DUMMY_SP,
name: name,
name,
attrs,
self_closing: children.is_empty(),
type_args: None,
Expand Down Expand Up @@ -283,7 +283,7 @@ mod tests {
})))
.unwrap();

String::from_utf8_lossy(&buf).to_string()
unsafe { String::from_utf8_unchecked(buf) }
}

fn document_test(input: PathBuf) {
Expand Down

0 comments on commit a36e9dd

Please sign in to comment.