module RuboCop::Ext::RegexpParser::Expression::Base

Add ‘expression` and `loc` to all `regexp_parser` nodes

Attributes

origin[RW]

Public Instance Methods

expression() click to toggle source

Shortcut to ‘loc.expression`

# File lib/rubocop/ext/regexp_parser.rb, line 26
def expression
  @expression ||= origin.adjust(begin_pos: ts, end_pos: ts + full_length)
end
loc() click to toggle source

@returns a location map like ‘parser` does, with:

- expression: complete expression
- quantifier: for `+`, `{1,2}`, etc.
- begin/end: for `[` and `]` (only CharacterSet for now)

E.g.

[a-z]{2,}
^^^^^^^^^ expression
     ^^^^ quantifier
^^^^^     body
^         begin
    ^     end

Please open issue if you need other locations

# File lib/rubocop/ext/regexp_parser.rb, line 44
def loc
  @loc ||= Map.new(expression, **build_location)
end

Private Instance Methods

build_location() click to toggle source
# File lib/rubocop/ext/regexp_parser.rb, line 50
def build_location
  return { body: expression } unless (q = quantifier)

  body = expression.adjust(end_pos: -q.text.length)
  q.origin = origin
  q_loc = q.expression

  { body: body, quantifier: q_loc }
end