Class: RuboCop::Cop::InternalAffairs::NodeMatcherDirective

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
RangeHelp
Defined in:
lib/rubocop/cop/internal_affairs/node_matcher_directive.rb

Overview

Checks that node matcher definitions are tagged with a YARD `@!method` directive so that editors are able to find the dynamically defined method.

Examples:

# bad
def_node_matcher :foo?, <<~PATTERN
  ...
PATTERN

# good
# @!method foo?(node)
def_node_matcher :foo?, <<~PATTERN
  ...
PATTERN

Constant Summary collapse

MSG =
'Precede `%<method>s` with a `@!method` YARD directive.'
MSG_WRONG_NAME =
'`@!method` YARD directive has invalid method name, ' \
'use `%<expected>s` instead of `%<actual>s`.'
MSG_TOO_MANY =
'Multiple `@!method` YARD directives found for this matcher.'
RESTRICT_ON_SEND =
%i[def_node_matcher def_node_search].to_set.freeze
REGEXP =
/^\s*#\s*@!method\s+(?<method_name>[a-z0-9_]+[?!]?)(?:\((?<args>.*)\))?/.freeze

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Instance Method Summary collapse

Methods included from AutoCorrector

support_autocorrect?

Methods inherited from Base

#add_global_offense, #add_offense, autocorrect_incompatible_with, badge, #callbacks_needed, callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #cop_config, #cop_name, cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, joining_forces, lint?, match?, #message, #offenses, #on_investigation_end, #on_new_investigation, #on_other_file, #parse, #ready, #relevant_file?, support_autocorrect?, support_multiple_source?, #target_rails_version, #target_ruby_version

Methods included from ExcludeLimit

#exclude_limit

Methods included from AutocorrectLogic

#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #correctable?, #disable_uncorrectable?, #safe_autocorrect?

Methods included from RuboCop::Cop::IgnoredNode

#ignore_node, #ignored_node?, #part_of_ignored_node?

Methods included from Util

silence_warnings

Constructor Details

This class inherits a constructor from RuboCop::Cop::Base

Instance Method Details

#on_send(node) ⇒ Object


39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rubocop/cop/internal_affairs/node_matcher_directive.rb', line 39

def on_send(node)
  return if node.arguments.none?
  return unless valid_method_name?(node)

  actual_name = node.arguments.first.value
  directives = method_directives(node)
  return too_many_directives(node) if directives.size > 1

  directive = directives.first
  return if directive_correct?(directive, actual_name)

  register_offense(node, directive, actual_name)
end

#pattern_matcher?(node) ⇒ Object


35
36
37
# File 'lib/rubocop/cop/internal_affairs/node_matcher_directive.rb', line 35

def_node_matcher :pattern_matcher?, <<~PATTERN
  (send _ %RESTRICT_ON_SEND {str sym} {str dstr})
PATTERN