Skip to content

Commit bb0dd97

Browse files
authored
Merge pull request #12771 from Earlopain/node-matcher-directive-followup
Fix a wrong offense message for `InternalAffairs/NodeMatcherDirective`
2 parents 9b39a55 + e49bf8a commit bb0dd97

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

lib/rubocop/cop/internal_affairs/node_matcher_directive.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,7 @@ def directive_offense_type(directive, actual_name)
116116
# The method directive being prefixed by 'self.' is always an offense.
117117
# The matched method_name does not contain the receiver but the
118118
# def_node_match method name may so it must be removed.
119-
actual_name_without_receiver = actual_name.delete_prefix('self.')
120-
if directive[:method_name] != actual_name_without_receiver || directive[:receiver]
119+
if directive[:method_name] != remove_receiver(actual_name) || directive[:receiver]
121120
:wrong_name
122121
end
123122
end
@@ -157,7 +156,8 @@ def formatted_message(offense_type, directive, actual_name, method_name)
157156
else
158157
directive[:method_name]
159158
end
160-
format(MSG_WRONG_NAME, expected: actual_name, actual: current_name)
159+
# The correct name will never include a receiver, remove it
160+
format(MSG_WRONG_NAME, expected: remove_receiver(actual_name), actual: current_name)
161161
when :wrong_scope
162162
MSG_WRONG_SCOPE_SELF
163163
when :no_scope
@@ -168,6 +168,10 @@ def formatted_message(offense_type, directive, actual_name, method_name)
168168
end
169169
# rubocop:enable Metrics/MethodLength
170170

171+
def remove_receiver(current)
172+
current.delete_prefix('self.')
173+
end
174+
171175
def insert_method_directive(corrector, node, actual_name)
172176
# If the pattern matcher uses arguments (`%1`, `%2`, etc.), include them in the directive
173177
arguments = pattern_arguments(node.arguments[1].source)
@@ -216,7 +220,7 @@ def last_line(node)
216220
end
217221

218222
def correct_method_directive(corrector, directive, actual_name)
219-
correct = "@!method #{actual_name.delete_prefix('self.')}"
223+
correct = "@!method #{remove_receiver(actual_name)}"
220224
current_name = (directive[:receiver] || '') + directive[:method_name]
221225
regexp = /@!method\s+#{Regexp.escape(current_name)}/
222226

spec/rubocop/cop/internal_affairs/node_matcher_directive_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,25 @@ class MyCop
393393
PATTERN
394394
RUBY
395395
end
396+
397+
it 'gives the correct message for "self." prefix on @!method when @!scope is correctly "class"' do
398+
expect_offense(<<~RUBY, method: method)
399+
# @!method self.foo?(node)
400+
# @!scope class
401+
#{method} 'self.foo?', <<~PATTERN
402+
^{method}^^^^^^^^^^^^^^^^^^^^^^^^ `@!method` YARD directive has invalid method name, use `foo?` instead of `self.foo?`.
403+
(str)
404+
PATTERN
405+
RUBY
406+
407+
expect_correction(<<~RUBY)
408+
# @!method foo?(node)
409+
# @!scope class
410+
#{method} 'self.foo?', <<~PATTERN
411+
(str)
412+
PATTERN
413+
RUBY
414+
end
396415
end
397416
end
398417
end

0 commit comments

Comments
 (0)