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 27
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 61
def loc
  @loc ||= Map.new(expression, **build_location)
end
start_index() click to toggle source
# File lib/rubocop/ext/regexp_parser.rb, line 35
def start_index
  # ts is a byte index; convert it to a character index
  @start_index ||= source.byteslice(0, ts).length
end

Private Instance Methods

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

  body = expression.adjust(end_pos: -q.text.length)
  q_loc = expression.with(begin_pos: body.end_pos)
  { body: body, quantifier: q_loc }
end