123456789_123456789_123456789_123456789_123456789_

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

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Extended In:
::RuboCop::AST::AliasNode, ::RuboCop::AST::AndAsgnNode, ::RuboCop::AST::AndNode, ::RuboCop::AST::ArgNode, ::RuboCop::AST::ArgsNode, ::RuboCop::AST::ArrayNode, ::RuboCop::AST::AsgnNode, ::RuboCop::AST::BlockNode, ::RuboCop::AST::BreakNode, ::RuboCop::AST::CaseMatchNode, ::RuboCop::AST::CaseNode, ::RuboCop::AST::CasgnNode, ::RuboCop::AST::ClassNode, ::RuboCop::AST::ComplexNode, ::RuboCop::AST::ConstNode, ::RuboCop::AST::CsendNode, ::RuboCop::AST::DefNode, ::RuboCop::AST::DefinedNode, ::RuboCop::AST::DstrNode, ::RuboCop::AST::EnsureNode, ::RuboCop::AST::FloatNode, ::RuboCop::AST::ForNode, ::RuboCop::AST::ForwardArgsNode, ::RuboCop::AST::HashNode, ::RuboCop::AST::IfNode, ::RuboCop::AST::InPatternNode, ::RuboCop::AST::IndexNode, ::RuboCop::AST::IndexasgnNode, ::RuboCop::AST::IntNode, ::RuboCop::AST::KeywordBeginNode, ::RuboCop::AST::KeywordSplatNode, ::RuboCop::AST::LambdaNode, ::RuboCop::AST::MasgnNode, ::RuboCop::AST::MethodDispatchNode, ::RuboCop::AST::MlhsNode, ::RuboCop::AST::ModuleNode, ::RuboCop::AST::NextNode, ::RuboCop::AST::Node, Node::AnyOrder, Node::Capture, Node::Predicate, Node::Repetition, Node::Rest, Node::Sequence, Node::Subsequence, Node::Union, ::RuboCop::AST::OpAsgnNode, ::RuboCop::AST::OrAsgnNode, ::RuboCop::AST::OrNode, ::RuboCop::AST::PairNode, ::RuboCop::AST::Procarg0Node, ::RuboCop::AST::RangeNode, ::RuboCop::AST::RationalNode, ::RuboCop::AST::RegexpNode, ::RuboCop::AST::ResbodyNode, ::RuboCop::AST::RescueNode, ::RuboCop::AST::ReturnNode, ::RuboCop::AST::SelfClassNode, ::RuboCop::AST::SendNode, ::RuboCop::AST::StrNode, ::RuboCop::AST::SuperNode, ::RuboCop::AST::SymbolNode, ::RuboCop::AST::UntilNode, ::RuboCop::AST::VarNode, ::RuboCop::AST::WhenNode, ::RuboCop::AST::WhileNode, ::RuboCop::AST::YieldNode
Defined in: lib/rubocop/ast/node_pattern.rb

Overview

Helpers for defining methods based on a pattern string

Instance Method Summary

Instance Method Details

#def_node_matcher(method_name, pattern_str, **keyword_defaults)

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.

[ GitHub ]

  
# File 'lib/rubocop/ast/node_pattern.rb', line 50

def def_node_matcher(method_name, pattern_str, **keyword_defaults)
  if NodePattern.lazy_compilation
    def_node_pattern_method_lazily(:def_node_matcher, method_name, pattern_str,
                                   keyword_defaults, caller_locations(1, 1).first)
  else
    NodePattern.new(pattern_str).def_node_matcher(self, method_name, **keyword_defaults)
  end
end

#def_node_pattern_method_lazily(definer, method_name, pattern_str, keyword_defaults, location) (private)

Defines a stub that compiles the pattern and replaces itself on first invocation. The compiled method is invoked through the owner’s instance method rather than a regular dispatch, so that overrides calling super keep working.

[ GitHub ]

  
# File 'lib/rubocop/ast/node_pattern.rb', line 80

def def_node_pattern_method_lazily(definer, method_name, pattern_str, keyword_defaults,
                                   location)
  base = self

  define_method(method_name) do |*args, **kwargs, &block|
    # Remove the stub before defining the compiled method in its
    # place, so that no method redefinition warning is emitted.
    begin
      base.send(:remove_method, method_name)
    rescue NameError
      nil # the method was already compiled concurrently
    end

    pattern = NodePattern.new(pattern_str)
    pattern.definition_location = location
    pattern.public_send(definer, base, method_name, **keyword_defaults)

    base.instance_method(method_name).bind_call(self, *args, **kwargs, &block)
  end

  method_name
end

#def_node_search(method_name, pattern_str, **keyword_defaults)

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.

[ GitHub ]

  
# File 'lib/rubocop/ast/node_pattern.rb', line 65

def def_node_search(method_name, pattern_str, **keyword_defaults)
  if NodePattern.lazy_compilation
    def_node_pattern_method_lazily(:def_node_search, method_name, pattern_str,
                                   keyword_defaults, caller_locations(1, 1).first)
  else
    NodePattern.new(pattern_str).def_node_search(self, method_name, **keyword_defaults)
  end
end