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_MISSING_SCOPE_SELF =
'Follow the `@!method` YARD directive with ' \
'`@!scope class` if it is a class method.'
MSG_WRONG_SCOPE_SELF =
'Do not use the `@!scope class` YARD directive if it ' \
'is not a class method.'
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_METHOD =
/
  ^\s*\#\s*
  @!method\s+(?<receiver>self\.)?(?<method_name>[a-z0-9_]+[?!]?)(?:\((?<args>.*)\))?
/x.freeze
REGEXP_SCOPE =
/^\s*\#\s*@!scope class/.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

#active_support_extensions_enabled?, #add_global_offense, #add_offense, #always_autocorrect?, autocorrect_incompatible_with, badge, #begin_investigation, #callbacks_needed, callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #contextual_autocorrect?, #cop_config, cop_name, #cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, #inspect, joining_forces, lint?, match?, #message, #offenses, #on_investigation_end, #on_new_investigation, #on_other_file, #parse, #parser_engine, #ready, #relevant_file?, requires_gem, #string_literals_frozen_by_default?, 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?, #autocorrect_with_disable_uncorrectable?, #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



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rubocop/cop/internal_affairs/node_matcher_directive.rb', line 47

def on_send(node)
  return unless node.arguments.count == 2
  return unless valid_method_name?(node)

  actual_name = node.first_argument.value.to_s

  # Ignore cases where the method has a receiver that isn't self
  return if actual_name.include?('.') && !actual_name.start_with?('self.')

  directives = method_directives(node)
  return too_many_directives(node) if directives.size > 1

  process_directive(node, actual_name, directives.first)
end

#pattern_matcher?(node) ⇒ Object



43
44
45
# File 'lib/rubocop/cop/internal_affairs/node_matcher_directive.rb', line 43

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