Skip to content

Commit

Permalink
perf: reduce string clone in HastVisitor
Browse files Browse the repository at this point in the history
  • Loading branch information
SyMind committed Sep 21, 2024
1 parent 15f263f commit 3c150e6
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions crates/core/src/hast_to_swc_ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,16 @@ fn text(n: &swc_xml::ast::Text) -> Option<JSXElementChild> {
static ref SPACE_REGEX: Regex = Regex::new(r"^\s+$").unwrap();
}

let value = n.data.to_string();
if SPACE_REGEX.is_match(&value) {
let value = n.data.as_str();
if SPACE_REGEX.is_match(value) {
return None;
}

Some(JSXElementChild::JSXExprContainer(JSXExprContainer {
span: DUMMY_SP,
expr: JSXExpr::Expr(Box::new(Expr::Lit(Lit::Str(Str {
span: DUMMY_SP,
value: decode_xml(&value).into(),
value: decode_xml(value).into(),
raw: None,
})))),
}))
Expand Down Expand Up @@ -112,7 +112,7 @@ impl HastVisitor {
.attributes
.iter()
.map(|attr| {
let value = attr.value.clone().map(|v| get_value(&attr.name, &v));
let value = attr.value.as_ref().map(|v| get_value(&attr.name, v));
JSXAttrOrSpread::JSXAttr(JSXAttr {
span: DUMMY_SP,
name: JSXAttrName::Ident(self.get_key(&attr.name, &n.tag_name).into()),
Expand All @@ -128,23 +128,23 @@ impl HastVisitor {
));
let children = self.all(&n.children);

let opening = JSXOpeningElement {
span: DUMMY_SP,
name: name.clone(),
attrs,
self_closing: children.is_empty(),
type_args: None,
};

let closing = if !children.is_empty() {
Some(JSXClosingElement {
span: DUMMY_SP,
name,
name: name.clone(),
})
} else {
None
};

let opening = JSXOpeningElement {
span: DUMMY_SP,
name: name,
attrs,
self_closing: children.is_empty(),
type_args: None,
};

JSXElement {
span: DUMMY_SP,
opening,
Expand Down

0 comments on commit 3c150e6

Please sign in to comment.