Skip to content

Commit 242e721

Browse files
committed
perf(transformer/styled-components): pass Atom instead of Option<Atom> (#12218)
Small optimization. There's only one path leading to `create_object_property` where `value` can be `None`. Move `value.unwrap_or(Atom::from(""))` from `create_object_property` to the one place where `None` was previously created. This avoids creating an `Option::Some` on all the other paths, only to unwrap it again later. This removes a branch from `create_object_property`.
1 parent 836bf9d commit 242e721

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

crates/oxc_transformer/src/plugins/styled_components.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ impl<'a> StyledComponents<'a, '_> {
571571
properties.push(Self::create_object_property("displayName", value, ctx));
572572
}
573573
if self.options.ssr {
574-
let value = Some(self.get_component_id(ctx));
574+
let value = self.get_component_id(ctx);
575575
properties.push(Self::create_object_property("componentId", value, ctx));
576576
}
577577
}
@@ -725,21 +725,22 @@ impl<'a> StyledComponents<'a, '_> {
725725
}
726726

727727
/// Returns the display name which infers the component name or gets from the file name.
728-
fn get_display_name(&mut self, ctx: &TraverseCtx<'a>) -> Option<Atom<'a>> {
728+
fn get_display_name(&mut self, ctx: &TraverseCtx<'a>) -> Atom<'a> {
729729
let component_name = Self::get_component_name(ctx);
730730

731-
let Some(block_name) = self.get_block_name(ctx) else { return component_name };
731+
let Some(block_name) = self.get_block_name(ctx) else {
732+
return component_name.unwrap_or(Atom::from(""));
733+
};
732734

733-
let name = if let Some(component_name) = component_name {
735+
if let Some(component_name) = component_name {
734736
if block_name == component_name {
735737
component_name
736738
} else {
737739
ctx.ast.atom_from_strs_array([&block_name, "__", &component_name])
738740
}
739741
} else {
740742
block_name
741-
};
742-
Some(name)
743+
}
743744
}
744745

745746
/// Returns true if the given callee is a styled-components binding.
@@ -833,11 +834,11 @@ impl<'a> StyledComponents<'a, '_> {
833834
// ^^^^^^^^^^
834835
fn create_object_property(
835836
key: &'static str,
836-
value: Option<Atom<'a>>,
837+
value: Atom<'a>,
837838
ctx: &TraverseCtx<'a>,
838839
) -> ObjectPropertyKind<'a> {
839840
let key = ctx.ast.property_key_static_identifier(SPAN, key);
840-
let value = ctx.ast.expression_string_literal(SPAN, value.unwrap_or(Atom::from("")), None);
841+
let value = ctx.ast.expression_string_literal(SPAN, value, None);
841842
ctx.ast.object_property_kind_object_property(
842843
SPAN,
843844
PropertyKind::Init,

0 commit comments

Comments
 (0)