Skip to content

Commit 6ec4b52

Browse files
committed
perf(transformer/styled-components): cache block name
1 parent 0cfc455 commit 6ec4b52

File tree

1 file changed

+31
-17
lines changed

1 file changed

+31
-17
lines changed

crates/oxc_transformer/src/plugins/styled_components.rs

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,8 @@ pub struct StyledComponents<'a, 'ctx> {
295295
component_count: usize,
296296
/// Hash of the current file for component ID generation
297297
component_id_prefix: Option<String>,
298+
/// Filename or directory name is used for `displayName`
299+
block_name: Option<String>,
298300
}
299301

300302
impl<'a, 'ctx> StyledComponents<'a, 'ctx> {
@@ -305,6 +307,7 @@ impl<'a, 'ctx> StyledComponents<'a, 'ctx> {
305307
styled_bindings: StyledComponentsBinding::default(),
306308
component_id_prefix: None,
307309
component_count: 0,
310+
block_name: None,
308311
}
309312
}
310313
}
@@ -690,27 +693,38 @@ impl<'a> StyledComponents<'a, '_> {
690693
base36_encode(hasher.finish())
691694
}
692695

693-
/// Returns the display name which infers the component name or gets from the file name.
694-
fn get_display_name(&self, ctx: &TraverseCtx<'a>) -> Option<&'a str> {
695-
let component_name = Self::get_component_name(ctx);
696+
/// Returns the block name based on the file stem or parent directory name.
697+
fn get_block_name(&mut self) -> Option<&str> {
698+
if !self.options.file_name {
699+
return None;
700+
}
696701

697-
if self.options.file_name
698-
&& let Some(file_stem) = self.ctx.source_path.file_stem().and_then(|stem| stem.to_str())
699-
{
702+
let file_stem = self.ctx.source_path.file_stem().and_then(|stem| stem.to_str())?;
703+
704+
Some(self.block_name.get_or_insert_with(|| {
700705
// Should be a name, but if the file stem is in the meaningless file names list,
701706
// we will use the parent directory name instead.
702-
let block_name = if self.options.meaningless_file_names.contains(&file_stem.to_string())
703-
{
704-
self.ctx
705-
.source_path
706-
.parent()
707-
.and_then(|parent| parent.file_name())
708-
.and_then(|name| name.to_str())
709-
.unwrap_or(file_stem)
710-
} else {
711-
file_stem
712-
};
707+
let block_name =
708+
if self.options.meaningless_file_names.iter().any(|name| name == file_stem) {
709+
self.ctx
710+
.source_path
711+
.parent()
712+
.and_then(|parent| parent.file_name())
713+
.and_then(|name| name.to_str())
714+
.unwrap_or(file_stem)
715+
} else {
716+
file_stem
717+
};
718+
719+
block_name.to_string()
720+
}))
721+
}
722+
723+
/// Returns the display name which infers the component name or gets from the file name.
724+
fn get_display_name(&mut self, ctx: &TraverseCtx<'a>) -> Option<&'a str> {
725+
let component_name = Self::get_component_name(ctx);
713726

727+
if let Some(block_name) = self.get_block_name() {
714728
if let Some(component_name) = component_name {
715729
if block_name == component_name {
716730
return Some(ctx.ast.str(component_name.as_str()));

0 commit comments

Comments
 (0)