@@ -291,6 +291,8 @@ pub struct StyledComponents<'a, 'ctx> {
291291 component_count : usize ,
292292 /// Hash of the current file for component ID generation
293293 file_hash : Option < & ' a str > ,
294+ /// Filename or directory name is used for `displayName`
295+ block_name : Option < String > ,
294296}
295297
296298impl < ' a , ' ctx > StyledComponents < ' a , ' ctx > {
@@ -299,8 +301,9 @@ impl<'a, 'ctx> StyledComponents<'a, 'ctx> {
299301 options,
300302 ctx,
301303 styled_bindings : StyledComponentsBinding :: default ( ) ,
302- file_hash : None ,
303304 component_count : 0 ,
305+ file_hash : None ,
306+ block_name : None ,
304307 }
305308 }
306309}
@@ -680,27 +683,38 @@ impl<'a> StyledComponents<'a, '_> {
680683 } )
681684 }
682685
683- /// Returns the display name which infers the component name or gets from the file name.
684- fn get_display_name ( & self , ctx : & TraverseCtx < ' a > ) -> Option < & ' a str > {
685- let component_name = Self :: get_component_name ( ctx) ;
686+ /// Returns the block name based on the file stem or parent directory name.
687+ fn get_block_name ( & mut self ) -> Option < & str > {
688+ if !self . options . file_name {
689+ return None ;
690+ }
686691
687- if self . options . file_name
688- && let Some ( file_stem ) = self . ctx . source_path . file_stem ( ) . and_then ( |stem| stem . to_str ( ) )
689- {
692+ let file_stem = self . ctx . source_path . file_stem ( ) . and_then ( |stem| stem . to_str ( ) ) ? ;
693+
694+ Some ( self . block_name . get_or_insert_with ( || {
690695 // Should be a name, but if the file stem is in the meaningless file names list,
691696 // we will use the parent directory name instead.
692- let block_name = if self . options . meaningless_file_names . contains ( & file_stem. to_string ( ) )
693- {
694- self . ctx
695- . source_path
696- . parent ( )
697- . and_then ( |parent| parent. file_name ( ) )
698- . and_then ( |name| name. to_str ( ) )
699- . unwrap_or ( file_stem)
700- } else {
701- file_stem
702- } ;
697+ let block_name =
698+ if self . options . meaningless_file_names . iter ( ) . any ( |name| name == file_stem) {
699+ self . ctx
700+ . source_path
701+ . parent ( )
702+ . and_then ( |parent| parent. file_name ( ) )
703+ . and_then ( |name| name. to_str ( ) )
704+ . unwrap_or ( file_stem)
705+ } else {
706+ file_stem
707+ } ;
708+
709+ block_name. to_string ( )
710+ } ) )
711+ }
712+
713+ /// Returns the display name which infers the component name or gets from the file name.
714+ fn get_display_name ( & mut self , ctx : & TraverseCtx < ' a > ) -> Option < & ' a str > {
715+ let component_name = Self :: get_component_name ( ctx) ;
703716
717+ if let Some ( block_name) = self . get_block_name ( ) {
704718 if let Some ( component_name) = component_name {
705719 if block_name == component_name {
706720 return Some ( ctx. ast . str ( component_name. as_str ( ) ) ) ;
0 commit comments