class RuboCop::AST::NodePattern
This class performs a pattern-matching operation on an AST
node.
Detailed syntax: /docs/modules/ROOT/pages/node_pattern.adoc
Initialize a new `NodePattern` with `NodePattern.new(pattern_string)`, then pass an AST
node to `NodePattern#match`. Alternatively, use one of the class macros in `NodePattern::Macros` to define your own pattern-matching method.
If the match fails, `nil` will be returned. If the match succeeds, the return value depends on whether a block was provided to `#match`, and whether the pattern contained any “captures” (values which are extracted from a matching AST
.)
Constants
- Invalid
- VAR
Attributes
ast[R]
match_code[R]
pattern[R]
Public Class Methods
descend(element) { |element| ... }
click to toggle source
Yields its argument and any descendants, depth-first.
# File lib/rubocop/ast/node_pattern.rb, line 105 def self.descend(element, &block) return to_enum(__method__, element) unless block yield element if element.is_a?(::RuboCop::AST::Node) element.children.each do |child| descend(child, &block) end end nil end
new(str, compiler: Compiler.new)
click to toggle source
# File lib/rubocop/ast/node_pattern.rb, line 61 def initialize(str, compiler: Compiler.new) @pattern = str @ast = compiler.parser.parse(str) @compiler = compiler @match_code = @compiler.compile_as_node_pattern(@ast, var: VAR) @cache = {} end
Public Instance Methods
==(other)
click to toggle source
# File lib/rubocop/ast/node_pattern.rb, line 74 def ==(other) other.is_a?(NodePattern) && other.ast == ast end
Also aliased as: eql?
freeze()
click to toggle source
Calls superclass method
# File lib/rubocop/ast/node_pattern.rb, line 119 def freeze @match_code.freeze @compiler.freeze super end
match(*args, **rest, &block)
click to toggle source
# File lib/rubocop/ast/node_pattern.rb, line 69 def match(*args, **rest, &block) @cache[:lambda] ||= as_lambda @cache[:lambda].call(*args, block: block, **rest) end
to_s()
click to toggle source
# File lib/rubocop/ast/node_pattern.rb, line 79 def to_s "#<#{self.class} #{pattern}>" end