Module: RuboCop::Ext::RegexpNode

Includes:
self
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

Please remove this `else` branch when support for regexp_parser 1.8 will be dropped. It's for compatibility with regexp_arser 1.8 and will never be maintained.


34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rubocop/ext/regexp_node.rb', line 34

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


53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rubocop/ext/regexp_node.rb', line 53

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

  parsed_tree&.traverse do |event, exp, _index|
    yield(exp) if event == :enter &&
                  named == exp.respond_to?(:name) &&
                  exp.respond_to?(:capturing?) &&
                  exp.capturing?
  end

  self
end