class RuboCop::Cop::InternalAffairs::NodePatternGroups::ASTProcessor
rather to the Node Pattern node types. Not every node type is handled.
NOTE: The ‘on_*` methods in this class relate not to the normal node types but
in order to find node types in a `union` node that can be rewritten as a node group.
The resulting AST will be walked by `InternalAffairs::NodePatternGroups::ASTWalker`
node type).
and converts them to `node_sequence` nodes (not a true `Rubocop::AST::NodePattern`
Looks for sequences and subsequences where the first item is a `node_type` node,
AST Processor for NodePattern ASTs, for use with `InternalAffairs/NodePatternGroups`.
def handler_missing(node)
def handler_missing(node) node.updated(nil, process_children(node)) end
def n(type, children = [], properties = {})
def n(type, children = [], properties = {}) NodePattern::Node.new(type, children, properties) end
def on_sequence(node)
This is necessary so that extended patterns can be matched and replaced.
possible to compare nodes while looking for replacement candidates for node groups.
their first child. These are rewritten as `node_sequence` nodes so that it is
Look for `sequence` and `subsequence` nodes that contain a `node_type` node as
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
def process_children(node, range = 0..-1)
def process_children(node, range = 0..-1) node.children[range].map do |child| child.is_a?(::AST::Node) ? process(child) : child end end