Class: RuboCop::AST::NodePattern
Relationships & Source Files | |
Namespace Children | |
Modules:
| |
Classes:
| |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
|
|
Instance Chain:
self,
MethodDefiner
|
|
Inherits: | Object |
Defined in: | lib/rubocop/ast/node_pattern.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/lexer.rb, lib/rubocop/ast/node_pattern/method_definer.rb, lib/rubocop/ast/node_pattern/node.rb, lib/rubocop/ast/node_pattern/parser.rb, lib/rubocop/ast/node_pattern/sets.rb, lib/rubocop/ast/node_pattern/with_meta.rb, lib/rubocop/ast/node_pattern/compiler/atom_subcompiler.rb, lib/rubocop/ast/node_pattern/compiler/binding.rb, lib/rubocop/ast/node_pattern/compiler/debug.rb, lib/rubocop/ast/node_pattern/compiler/node_pattern_subcompiler.rb, lib/rubocop/ast/node_pattern/compiler/sequence_subcompiler.rb, lib/rubocop/ast/node_pattern/compiler/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 #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
-
Invalid =
# File 'lib/rubocop/ast/node_pattern.rb', line 53Class.new(StandardError)
-
VAR =
# File 'lib/rubocop/ast/node_pattern.rb', line 55'node'
Class Method Summary
-
.descend(element) {|element| ... }
Yields its argument and any descendants, depth-first.
- .new(str, compiler: Compiler.new) ⇒ NodePattern constructor
::RuboCop::SimpleForwardable
- Extended
Instance Attribute Summary
- #ast readonly
- #match_code readonly
- #pattern readonly
Instance Method Summary
- #==(other) (also: #eql?)
- #as_json(_options = nil) Internal use only
- #encode_with(coder) Internal use only
-
#eql?(other)
Alias for #==.
- #freeze
- #init_with(coder) Internal use only
- #marshal_dump Internal use only
- #marshal_load(pattern) Internal use only
- #match(*args, **rest, &block)
- #to_s
MethodDefiner
- Included
#as_lambda, #compile_as_lambda, #def_node_matcher, #def_node_search, #compile_init, #def_helper, #emit_keyword_list, #emit_lambda_code, #emit_method_code, #emit_node_search, #emit_node_search_body, #emit_param_list, #emit_params, #emit_retval, #emit_yield_capture, | |
#wrapping_block | This method minimizes the closure for our method. |
Constructor Details
.new(str, compiler: Compiler.new) ⇒ NodePattern
Class Method Details
.descend(element) {|element| ... }
Yields its argument and any descendants, depth-first.
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 ]#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 ]#as_json(_options = nil)
# File 'lib/rubocop/ast/node_pattern.rb', line 107
def as_json( = nil) # :nodoc: pattern end
#encode_with(coder)
# File 'lib/rubocop/ast/node_pattern.rb', line 111
def encode_with(coder) # :nodoc: coder['pattern'] = pattern end
#eql?(other)
Alias for #==.
# 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)
# File 'lib/rubocop/ast/node_pattern.rb', line 115
def init_with(coder) # :nodoc: initialize(coder['pattern']) end
#marshal_dump
# File 'lib/rubocop/ast/node_pattern.rb', line 103
def marshal_dump # :nodoc: pattern end
#marshal_load(pattern)
#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