Skip to content

Commit

Permalink
Fix an error for InternalAffairs/NodeMatcherDirective
Browse files Browse the repository at this point in the history
This PR fixes the following error for `InternalAffairs/NodeMatcherDirective`.

```console
% cat example.rb
def_node_matcher :foo?, format(PATTERN, type: 'const')

% bundle exec rubocop --only InternalAffairs/NodeMatcherDirective -d
(snip)

Inspecting 1 file
Scanning
/Users/koic/src/github.com/koic/rubocop-issues/ia_cop/example.rb
An error occurred while InternalAffairs/NodeMatcherDirective cop was
inspecting
/Users/koic/src/github.com/koic/rubocop-issues/ia_cop/example.rb:1:0.
undefined method `value' for s(:send, nil, :format,
  s(:const, nil, :PATTERN),
  s(:hash,
    s(:pair,
      s(:sym, :type),
      s(:str, "const")))):RuboCop::AST::SendNode
/Users/koic/src/github.com/rubocop/rubocop/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb:100:in
`insert_directive'
/Users/koic/src/github.com/rubocop/rubocop/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb:85:in
`block in register_offense'
(snip)

1 error occurred:
An error occurred while InternalAffairs/NodeMatcherDirective cop was
inspecting
/Users/koic/src/github.com/koic/rubocop-issues/ia_cop/example.rb:1:0.
Errors are usually caused by RuboCop bugs.
Please, report your problems to RuboCop's issue tracker.
https://github.com/rubocop/rubocop/issues
```
  • Loading branch information
koic committed Feb 27, 2021
1 parent 1dd69ab commit aef3d43
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/rubocop/cop/internal_affairs/node_matcher_directive.rb
Expand Up @@ -97,7 +97,7 @@ def formatted_message(directive, actual_name, method_name)

def insert_directive(corrector, node, actual_name)
# If the pattern matcher uses arguments (`%1`, `%2`, etc.), include them in the directive
arguments = pattern_arguments(node.arguments[1].value)
arguments = pattern_arguments(node.arguments[1].source)

range = range_with_surrounding_space(
range: node.loc.expression,
Expand Down
12 changes: 12 additions & 0 deletions spec/rubocop/cop/internal_affairs/node_matcher_directive_spec.rb
Expand Up @@ -29,6 +29,18 @@
RUBY
end

it 'registers an offense if the matcher does not have a directive and a method call is used for a pattern argument' do
expect_offense(<<~RUBY, method: method)
#{method} :foo?, format(PATTERN, type: 'const')
^{method}^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Preceed `#{method}` with a `@!method` YARD directive.
RUBY

expect_correction(<<~RUBY)
# @!method foo?(node)
#{method} :foo?, format(PATTERN, type: 'const')
RUBY
end

it 'registers an offense if the matcher does not have a directive but has preceeding comments' do
expect_offense(<<~RUBY, method: method)
# foo
Expand Down

0 comments on commit aef3d43

Please sign in to comment.