Class: RuboCop::AST::NodePattern

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
MethodDefiner
Defined in:
lib/rubocop/ast/node_pattern.rb,
lib/rubocop/ast/node_pattern/node.rb,
lib/rubocop/ast/node_pattern/sets.rb,
lib/rubocop/ast/node_pattern/lexer.rb,
lib/rubocop/ast/node_pattern/parser.rb,
lib/rubocop/ast/node_pattern/builder.rb,
lib/rubocop/ast/node_pattern/comment.rb,
lib/rubocop/ast/node_pattern/compiler.rb,
lib/rubocop/ast/node_pattern/with_meta.rb,
lib/rubocop/ast/node_pattern/compiler/debug.rb,
lib/rubocop/ast/node_pattern/method_definer.rb,
lib/rubocop/ast/node_pattern/compiler/binding.rb,
lib/rubocop/ast/node_pattern/compiler/subcompiler.rb,
lib/rubocop/ast/node_pattern/compiler/atom_subcompiler.rb,
lib/rubocop/ast/node_pattern/compiler/sequence_subcompiler.rb,
lib/rubocop/ast/node_pattern/compiler/node_pattern_subcompiler.rb

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 `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.)

  • 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`.

Defined Under Namespace

Modules: Macros, MethodDefiner, Sets Classes: Builder, Comment, Compiler, Lexer, Node, Parser

Constant Summary collapse

Invalid =
Class.new(StandardError)
VAR =
'node'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from MethodDefiner

#as_lambda, #compile_as_lambda, #def_node_matcher, #def_node_search

Constructor Details

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

Returns a new instance of NodePattern.



77
78
79
80
81
82
83
# 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

Instance Attribute Details

#astObject (readonly)

Returns the value of attribute ast.



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

def ast
  @ast
end

#match_codeObject (readonly)

Returns the value of attribute match_code.



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

def match_code
  @match_code
end

#patternObject (readonly)

Returns the value of attribute pattern.



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

def pattern
  @pattern
end

Class Method Details

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

Yields its argument and any descendants, depth-first.

Yields:

  • (element)


59
60
61
62
63
64
65
66
67
68
69
70
71
# 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 Method Details

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



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

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

#as_json(_options = nil) ⇒ Object

:nodoc:



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

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

#encode_with(coder) ⇒ Object

:nodoc:



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

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

#freezeObject



119
120
121
122
123
# File 'lib/rubocop/ast/node_pattern.rb', line 119

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

#init_with(coder) ⇒ Object

:nodoc:



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

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

#marshal_dumpObject

:nodoc:



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

def marshal_dump # :nodoc:
  pattern
end

#marshal_load(pattern) ⇒ Object

:nodoc:



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

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

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



85
86
87
88
# 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_sObject



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

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