Method: RuboCop::Cop::InternalAffairs::NodePatternGroups#on_send
- Defined in:
- lib/rubocop/cop/internal_affairs/node_pattern_groups.rb
#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.
68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/rubocop/cop/internal_affairs/node_pattern_groups.rb', line 68 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 |