@@ -44,6 +44,7 @@ class SpecFilePathFormat < Base
4444 include FileHelp
4545
4646 MSG = 'Spec path should end with `%<suffix>s`.'
47+ PATH_NAME_BOUNDARY = '(?![[:alnum:]])'
4748
4849 # @!method example_group_arguments(node)
4950 def_node_matcher :example_group_arguments , <<~PATTERN
@@ -119,7 +120,11 @@ def ensure_correct_file_path(send_node, class_name, arguments)
119120 # For the suffix shown in the offense message, modify the regular
120121 # expression pattern to resemble a glob pattern for clearer error
121122 # messages.
122- suffix = pattern . sub ( '.*' , '*' ) . sub ( '[^/]*' , '*' ) . sub ( '\.' , '.' )
123+ suffix = pattern
124+ . sub ( PATH_NAME_BOUNDARY , '' )
125+ . sub ( '.*' , '*' )
126+ . sub ( '[^/]*' , '*' )
127+ . sub ( '\.' , '.' )
123128 add_offense ( send_node , message : format ( MSG , suffix : suffix ) )
124129 end
125130
@@ -132,19 +137,21 @@ def ignore_metadata?(arguments)
132137 end
133138
134139 def correct_path_pattern ( class_name , arguments )
135- path = [ expected_path ( class_name ) ]
136- path << '.*' unless ignore? ( arguments . first )
137- path << [ name_pattern ( arguments . first ) , '[^/]*_spec\.rb' ]
138- path . join
140+ [
141+ expected_path ( class_name ) ,
142+ PATH_NAME_BOUNDARY ,
143+ method_name_pattern ( arguments . first ) ,
144+ '[^/]*_spec\.rb'
145+ ] . join
139146 end
140147
141- def name_pattern ( method_name )
142- return if ignore ?( method_name )
148+ def method_name_pattern ( method_name )
149+ return if ignore_method_name ?( method_name )
143150
144- method_name . str_content . gsub ( /\s / , '_' ) . gsub ( /\W / , '' )
151+ ".* #{ method_name . str_content . gsub ( /\s / , '_' ) . gsub ( /\W / , '' ) } "
145152 end
146153
147- def ignore ?( method_name )
154+ def ignore_method_name ?( method_name )
148155 !method_name &.str_type? || ignore_methods?
149156 end
150157
@@ -175,7 +182,7 @@ def ignore_metadata
175182 end
176183
177184 def filename_ends_with? ( pattern )
178- expanded_file_path . match? ( " #{ pattern } $" )
185+ expanded_file_path . match? ( %r{(?: \A |/) #{ pattern } $} )
179186 end
180187 end
181188 end
0 commit comments