123456789_123456789_123456789_123456789_123456789_

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

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, ::RuboCop::Cop::Base, ::RuboCop::ExcludeLimit, NodePattern::Macros, RuboCop::AST::Sexp
Instance Chain:
Inherits: RuboCop::Cop::Base
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

::RuboCop::Cop::Base - Inherited

DEPARTMENT_SEVERITIES, EMPTY_OFFENSES, RESTRICT_ON_SEND

Class Attribute Summary

::RuboCop::Cop::Base - Inherited

.gem_requirements, .lint?,
.support_autocorrect?

Returns if class supports autocorrect.

.support_multiple_source?

Override if your cop should be called repeatedly for multiple investigations Between calls to on_new_investigation and on_investigation_end, the result of processed_source will remain constant.

Class Method Summary

::RuboCop::Cop::Base - Inherited

.autocorrect_incompatible_with

List of cops that should not try to autocorrect at the same time as this cop.

.badge

Naming.

.callbacks_needed, .cop_name, .department,
.documentation_url

Returns a url to view this cops documentation online.

.exclude_from_registry

Call for abstract Cop classes.

.inherited,
.joining_forces

Override and return the Force class(es) you need to join.

.match?

Returns true if the cop name or the cop namespace matches any of the given names.

.new,
.requires_gem

Register a version requirement for the given gem name.

.restrict_on_send

Reserved for Commissioner.

::RuboCop::ExcludeLimit - Extended

exclude_limit

Sets up a configuration option to have an exclude limit tracked.

transform

Instance Attribute Summary

::RuboCop::Cop::Base - Inherited

::RuboCop::Cop::AutocorrectLogic - Included

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

Whether a correction exists but is skipped because it is unsafe in a safe autocorrect run, while --disable-uncorrectable asks for a todo comment on everything this run cannot correct.

#contextual_autocorrect_held_back?

When LSP is enabled or the --editor-mode option is on, it is considered as editing source code, and autocorrection with AutoCorrect: contextual will not be performed.

Instance Method Summary

::RuboCop::Cop::Base - Inherited

#add_global_offense

Adds an offense that has no particular location.

#add_offense

Adds an offense on the specified range (or node with an expression) Unless that offense is disabled for this range, a corrector will be yielded to provide the cop the opportunity to autocorrect the offense.

#begin_investigation

Called before any investigation.

#callbacks_needed

 — grouped under the Commissioner heading above.

#cop_config

Configuration Helpers.

#cop_name, #excluded_file?,
#external_dependency_checksum

This method should be overridden when a cop’s behavior depends on state that lives outside of these locations:

#inspect,
#message

Gets called if no message is specified when calling add_offense or add_global_offense Cops are discouraged to override this; instead pass your message directly.

#name

Alias for Base#cop_name.

#offenses,
#on_investigation_end

Called after all on_…​

#on_new_investigation

Called before all on_…​

#on_other_file

Called instead of all on_…​

#parse

There should be very limited reasons for a Cop to do it’s own parsing The source is parsed the way the file under investigation was.

#parser_engine,
#ready

Called between investigations.

#relevant_file?,
#target_gem_version

Returns the version of a gem in the target, by default its locked version (i.e. from Gemfile.lock or gems.locked).

#target_rails_version, #target_ruby_version, #annotate, #apply_correction, #attempt_correction,
#callback_argument

Reserved for Cop::Cop.

#complete_investigation

Called to complete an investigation.

#correct, #covering_disabled_range, #current_corrector,
#current_offense_locations

Reserved for Commissioner:

#current_offenses, #currently_disabled_lines, #custom_severity, #default_severity, #disable_uncorrectable, #enabled_line?,
#enabled_lines?

A multi-line offense is suppressed by a directive on any line of its range, not only its first line, matching the intuition that the directive is attached to the offending code.

#file_name_matches_any?, #find_message, #find_severity, #matches_absolute_include_pattern?, #new_offense, #range_for_original, #range_from_node_or_range,
#reset_investigation

Actually private methods.

#suppression_reason

The -- reason on the directive that suppresses offenses on this range, or nil when the directive carries none.

#use_corrector

::RuboCop::Cop::AutocorrectLogic - Included

::RuboCop::Cop::IgnoredNode - Included

Constructor Details

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

Instance Method Details

#offense_range(start, range) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/internal_affairs/invalid_node_pattern.rb', line 70

def offense_range(start, range)
  begin_pos = start.begin_pos + range.begin_pos

  start.with(begin_pos: begin_pos, end_pos: begin_pos + range.size)
end

#on_csend(node)

Alias for #on_send.

[ GitHub ]

  
# File 'lib/rubocop/cop/internal_affairs/invalid_node_pattern.rb', line 56

alias on_csend on_send

#on_send(node) Also known as: #on_csend

[ GitHub ]

  
# 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

#pattern_start(pattern_node) ⇒ Parser::Source::Range? (private)

Returns:

  • (Parser::Source::Range, nil) —

    where the pattern begins, if the positions in its value are the positions in the source

[ GitHub ]

  
# File 'lib/rubocop/cop/internal_affairs/invalid_node_pattern.rb', line 62

def pattern_start(pattern_node)
  if pattern_node.heredoc?
    pattern_node.loc.heredoc_body unless pattern_node.each_child_node(:begin).any?
  elsif pattern_node.str_type? && pattern_node.loc?(:begin) && verbatim?(pattern_node)
    pattern_node.loc.begin.end
  end
end

#problems(pattern_node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/internal_affairs/invalid_node_pattern.rb', line 81

def problems(pattern_node)
  value = pattern_node.heredoc? ? pattern_node.loc.heredoc_body.source : pattern_node.value

  RuboCop::AST::NodePattern::Checker.new.check(value)
rescue RuboCop::AST::NodePattern::Invalid
  []
end

#verbatim?(pattern_node) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/internal_affairs/invalid_node_pattern.rb', line 76

def verbatim?(pattern_node)
  pattern_node.source == "#{pattern_node.loc.begin.source}#{pattern_node.value}" \
                         "#{pattern_node.loc.end.source}"
end