Class: RuboCop::Cop::InternalAffairs::NodePatternGroups

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
RangeHelp
Defined in:
lib/rubocop/cop/internal_affairs/node_pattern_groups.rb,
lib/rubocop/cop/internal_affairs/node_pattern_groups/ast_walker.rb,
lib/rubocop/cop/internal_affairs/node_pattern_groups/ast_processor.rb
more...

Overview

rubocop:disable InternalAffairs/RedundantSourceRange – node here is a ‘NodePattern::Node`

Defined Under Namespace

Classes: ASTProcessor, ASTWalker

Constant Summary collapse

MSG =
'Replace `%<names>s` in node pattern union with `%<replacement>s`.'
RESTRICT_ON_SEND =
%i[def_node_matcher def_node_search].freeze
NODE_GROUPS =
{
  any_block: %i[block numblock itblock],
  argument: %i[arg optarg restarg kwarg kwoptarg kwrestarg blockarg forward_arg shadowarg],
  boolean: %i[true false],
  call: %i[send csend],
  numeric: %i[int float rational complex],
  range: %i[irange erange]
}.freeze

Constants included from RangeHelp

RangeHelp::BYTE_ORDER_MARK, RangeHelp::NOT_GIVEN

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_other_file, #parse, #parser_engine, #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

#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

#after_send(_) ⇒ Object

[View source]

78
79
80
# File 'lib/rubocop/cop/internal_affairs/node_pattern_groups.rb', line 78

def after_send(_)
  @walker.reset!
end

#on_new_investigationObject

[View source]

38
39
40
# File 'lib/rubocop/cop/internal_affairs/node_pattern_groups.rb', line 38

def on_new_investigation
  @walker = ASTWalker.new
end

#on_send(node) ⇒ Object

When a Node Pattern matcher is defined, investigate the pattern string to search for node types that can be replaced with a node group (ie. ‘csend` can be replaced with `call`).

In order to deal with node patterns in an efficient and non-brittle way, we will parse the Node Pattern string given to this ‘send` node using `RuboCop::AST::NodePattern::Parser::WithMeta`. `WithMeta` is important! We need location information so that we can calculate the exact locations within the pattern to report and correct.

The resulting AST is processed by ‘NodePatternGroups::ASTProccessor` which rewrites the AST slightly to handle node sequences (ie. `(send _ :foo …)`). See the documentation of that class for more details.

Then the processed AST is walked, and metadata is collected for node types that can be replaced with a node group.

Finally, the metadata is used to register offenses and make corrections, using the location data captured earlier. The ranges captured while parsing the Node Pattern are offset using the string argument to this ‘send` node to ensure that offenses are registered at the correct location.

[View source]

64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/rubocop/cop/internal_affairs/node_pattern_groups.rb', line 64

def on_send(node)
  pattern_node = node.arguments[1]
  return unless acceptable_heredoc?(pattern_node) || pattern_node.str_type?

  process_pattern(pattern_node)
  return if node_groups.nil?

  apply_range_offsets(pattern_node)

  node_groups.each_with_index do |group, index|
    register_offense(group, index)
  end
end