Class: RuboCop::Cop::InternalAffairs::InvalidNodePattern

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/internal_affairs/invalid_node_pattern.rb

Overview

Checks node patterns for parts that can never match: node types that do not exist, sequences with a number of children that a node of that type never has, a node where a node of that type has a value like a symbol (and the other way around), and predicates that nothing responds to. Alternatives that are in a union more than once are reported too.

Patterns that are built with interpolation are not checked.

Examples:

# bad
def_node_matcher :my_matcher, <<~PATTERN
  (def _name _args)
PATTERN

# good
def_node_matcher :my_matcher, <<~PATTERN
  (def _name _args _body)
PATTERN

# bad
def_node_matcher :my_matcher, <<~PATTERN
  (send nil? "puts" ...)
PATTERN

# good
def_node_matcher :my_matcher, <<~PATTERN
  (send nil? :puts ...)
PATTERN

# bad
def_node_matcher :my_matcher, <<~PATTERN
  (send _ {:first :last :first})
PATTERN

# good
def_node_matcher :my_matcher, <<~PATTERN
  (send _ {:first :last})
PATTERN

Constant Summary collapse

RESTRICT_ON_SEND =
%i[def_node_matcher def_node_search].freeze

Constants inherited from Base

Base::DEPARTMENT_SEVERITIES

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source, #project_index

Instance Method Summary collapse

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, #disabled_autocorrect?, 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, #preview?, #ready, #relevant_file?, requires_gem, #string_literals_frozen_by_default?, support_autocorrect?, support_multiple_source?, #target_gem_version, #target_rails_version, #target_ruby_version

Methods included from ExcludeLimit

cop_dir_for, #exclude_limit, read_limits

Methods included from AutocorrectLogic

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

Methods included from RuboCop::Cop::IgnoredNode

#ignore_node, #ignored_node?, #part_of_ignored_node?

Methods included from Util

emulate_write_read_cycle, replace_file_contents, silence_warnings

Constructor Details

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

Instance Method Details

#on_send(node) ⇒ Object Also known as: on_csend



48
49
50
51
52
53
54
55
# File 'lib/rubocop/cop/internal_affairs/invalid_node_pattern.rb', line 48

def on_send(node)
  pattern_node = node.arguments[1]
  return unless pattern_node&.any_str_type? && (start = pattern_start(pattern_node))

  problems(pattern_node).each do |problem|
    add_offense(offense_range(start, problem.range), message: problem.message)
  end
end