Module: RuboCop::AST::NodePattern::Macros

Included in:
MethodDispatchNode, RuboCop::AST::Node
Defined in:
lib/rubocop/ast/node_pattern.rb

Overview

Helpers for defining methods based on a pattern string

Instance Method Summary collapse

Instance Method Details

#def_node_matcher(method_name, pattern_str, **keyword_defaults) ⇒ Object

Define a method which applies a pattern to an AST node

The new method will return nil if the node does not match If the node matches, and a block is provided, the new method will yield to the block (passing any captures as block arguments). If the node matches, and no block is provided, the new method will return the captures, or `true` if there were none.


867
868
869
870
# File 'lib/rubocop/ast/node_pattern.rb', line 867

def def_node_matcher(method_name, pattern_str, **keyword_defaults)
  Compiler.new(pattern_str, 'node')
          .def_node_matcher(self, method_name, **keyword_defaults)
end

#def_node_search(method_name, pattern_str, **keyword_defaults) ⇒ Object

Define a method which recurses over the descendants of an AST node, checking whether any of them match the provided pattern

If the method name ends with '?', the new method will return `true` as soon as it finds a descendant which matches. Otherwise, it will yield all descendants which match.


878
879
880
881
# File 'lib/rubocop/ast/node_pattern.rb', line 878

def def_node_search(method_name, pattern_str, **keyword_defaults)
  Compiler.new(pattern_str, 'node0', 'node')
          .def_node_search(self, method_name, **keyword_defaults)
end