From aef3d43b0dcd6e4cd5d687c81749df01a09e2b81 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Sun, 28 Feb 2021 03:53:24 +0900 Subject: [PATCH] Fix an error for `InternalAffairs/NodeMatcherDirective` 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 ``` --- .../cop/internal_affairs/node_matcher_directive.rb | 2 +- .../internal_affairs/node_matcher_directive_spec.rb | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb b/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb index dcf36b49c521..f1956529a762 100644 --- a/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +++ b/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb @@ -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, diff --git a/spec/rubocop/cop/internal_affairs/node_matcher_directive_spec.rb b/spec/rubocop/cop/internal_affairs/node_matcher_directive_spec.rb index 2f4d7fafd7c5..6db361b25a5f 100644 --- a/spec/rubocop/cop/internal_affairs/node_matcher_directive_spec.rb +++ b/spec/rubocop/cop/internal_affairs/node_matcher_directive_spec.rb @@ -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