123456789_123456789_123456789_123456789_123456789_

Class: RuboCop::AST::NodePattern

Overview

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 #match. Alternatively, use one of the class macros in 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.)

  • With block: #match yields the captures (if any) and passes the return value of the block through.

  • With no block, but one capture: the capture is returned.

  • With no block, but multiple captures: captures are returned as an array.

  • With no block and no captures: #match returns true.

Constant Summary

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(str, compiler: Compiler.new) ⇒ NodePattern

[ GitHub ]

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

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

Class Method Details

.descend(element) {|element| ... }

Yields its argument and any descendants, depth-first.

Yields:

  • (element)
[ GitHub ]

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

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

Instance Attribute Details

#ast (readonly)

[ GitHub ]

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

attr_reader :pattern, :ast, :match_code

#match_code (readonly)

[ GitHub ]

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

attr_reader :pattern, :ast, :match_code

#pattern (readonly)

[ GitHub ]

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

attr_reader :pattern, :ast, :match_code

Instance Method Details

#==(other) Also known as: #eql?

[ GitHub ]

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

def ==(other)
  other.is_a?(NodePattern) && other.ast == ast
end

#as_json(_options = nil)

This method is for internal use only.
[ GitHub ]

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

def as_json(_options = nil) # :nodoc:
  pattern
end

#encode_with(coder)

This method is for internal use only.
[ GitHub ]

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

def encode_with(coder) # :nodoc:
  coder['pattern'] = pattern
end

#eql?(other)

Alias for #==.

[ GitHub ]

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

alias eql? ==

#freeze

[ GitHub ]

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

def freeze
  @match_code.freeze
  @compiler.freeze
  super
end

#init_with(coder)

This method is for internal use only.
[ GitHub ]

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

def init_with(coder) # :nodoc:
  initialize(coder['pattern'])
end

#marshal_dump

This method is for internal use only.
[ GitHub ]

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

def marshal_dump # :nodoc:
  pattern
end

#marshal_load(pattern)

This method is for internal use only.
[ GitHub ]

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

def marshal_load(pattern) # :nodoc:
  initialize pattern
end

#match(*args, **rest, &block)

[ GitHub ]

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

def match(*args, **rest, &block)
  @cache[:lambda] ||= as_lambda
  @cache[:lambda].call(*args, block: block, **rest)
end

#to_s

[ GitHub ]

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

def to_s
  "#<#{self.class} #{pattern}>"
end