Module: RuboCop::Ext::RegexpNode

Defined in:
lib/rubocop/ext/regexp_node.rb

Overview

Extensions to AST::RegexpNode for our cached parsed regexp info

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#parsed_treeRegexp::Expression::Root? (readonly)

Note: we extend Regexp nodes to provide ‘loc` and `expression` see `ext/regexp_parser`.

Returns:

  • (Regexp::Expression::Root, nil)


16
17
18
# File 'lib/rubocop/ext/regexp_node.rb', line 16

def parsed_tree
  @parsed_tree
end

Instance Method Details

#assign_propertiesObject



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rubocop/ext/regexp_node.rb', line 18

def assign_properties(*)
  super

  str = with_interpolations_blanked
  @parsed_tree = begin
    Regexp::Parser.parse(str, options: options)
  rescue StandardError
    nil
  end
  origin = loc.begin.end
  @parsed_tree&.each_expression(true) { |e| e.origin = origin }
end

#each_capture(named: ANY) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/rubocop/ext/regexp_node.rb', line 31

def each_capture(named: ANY)
  return enum_for(__method__, named: named) unless block_given?

  parsed_tree&.traverse do |event, exp, _index|
    yield(exp) if named_capturing?(exp, event, named)
  end

  self
end