Class: RuboCop::Cop::InternalAffairs::NodePatternGroups::ASTProcessor
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Instance Chain:
self,
AST::Processor::Mixin
|
|
Inherits: | Object |
Defined in: | lib/rubocop/cop/internal_affairs/node_pattern_groups/ast_processor.rb |
Overview
AST Processor for NodePattern ASTs, for use with InternalAffairs/NodePatternGroups
.
Looks for sequences and subsequences where the first item is a node_type
node,
and converts them to node_sequence
nodes (not a true Rubocop::AST::NodePattern
node type).
The resulting AST will be walked by ASTWalker
in order to find node types in a union
node that can be rewritten as a node group.
Note
|
The on_* methods in this class relate not to the normal node types but
rather to the Node Pattern node types. Not every node type is handled.
|
Instance Method Summary
- #handler_missing(node)
-
#on_sequence(node)
(also: #on_subsequence)
Look for
sequence
andsubsequence
nodes that contain anode_type
node as their first child. -
#on_subsequence(node)
Alias for #on_sequence.
- #n(type, children = [], properties = {}) private
- #process_children(node, range = 0..-1)) private
Instance Method Details
#handler_missing(node)
[ GitHub ]# File 'lib/rubocop/cop/internal_affairs/node_pattern_groups/ast_processor.rb', line 22
def handler_missing(node) node.updated(nil, process_children(node)) end
#n(type, children = [], properties = {}) (private)
[ GitHub ]# File 'lib/rubocop/cop/internal_affairs/node_pattern_groups/ast_processor.rb', line 50
def n(type, children = [], properties = {}) NodePattern::Node.new(type, children, properties) end
#on_sequence(node) Also known as: #on_subsequence
Look for sequence
and subsequence
nodes that contain a node_type
node as
their first child. These are rewritten as node_sequence
nodes so that it is
possible to compare nodes while looking for replacement candidates for node groups.
This is necessary so that extended patterns can be matched and replaced.
ie. {(send _ :foo …) (csend _ :foo …)}
can become (call _ :foo …)
# File 'lib/rubocop/cop/internal_affairs/node_pattern_groups/ast_processor.rb', line 31
def on_sequence(node) first_child = node.child if first_child.type == :node_type children = [first_child.child, *process_children(node, 1..)] # The `node_sequence` node contains the `node_type` symbol as its first child, # followed by all the other nodes contained in the `sequence` node. # The location is copied from the sequence, so that the entire sequence can # eventually be corrected in the cop. n(:node_sequence, children, location: node.location) else node.updated(nil, process_children(node)) end end
#on_subsequence(node)
Alias for #on_sequence.
# File 'lib/rubocop/cop/internal_affairs/node_pattern_groups/ast_processor.rb', line 46
alias on_subsequence on_sequence
#process_children(node, range = 0..-1)) (private)
[ GitHub ]# File 'lib/rubocop/cop/internal_affairs/node_pattern_groups/ast_processor.rb', line 54
def process_children(node, range = 0..-1) node.children[range].map do |child| child.is_a?(::AST::Node) ? process(child) : child end end