Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
Regexp::Expression#loc
and #expression
to replace `parsed_tre…
…e_expr_loc`
- Loading branch information
Showing
6 changed files
with
99 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* [#8960](https://github.com/rubocop-hq/rubocop/pull/8960): Add `Regexp::Expression#loc` and `#expression` to replace `parsed_tree_expr_loc`. ([@marcandre][]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Ext | ||
# Extensions for `regexp_parser` gem | ||
module RegexpParser | ||
# Source map for RegexpParser nodes | ||
class Map < ::Parser::Source::Map | ||
attr_reader :body, :quantifier, :begin, :end | ||
|
||
def initialize(expression, body:, quantifier: nil, begin_l: nil, end_l: nil) | ||
@begin = begin_l | ||
@end = end_l | ||
@body = body | ||
@quantifier = quantifier | ||
super(expression) | ||
end | ||
end | ||
|
||
module Expression | ||
# Add `expression` and `loc` to all `regexp_parser` nodes | ||
module Base | ||
attr_accessor :origin | ||
|
||
# Shortcut to `loc.expression` | ||
def expression | ||
@expression ||= origin.adjust(begin_pos: ts, end_pos: ts + full_length) | ||
end | ||
|
||
# @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 | ||
def loc | ||
@loc ||= begin | ||
Map.new(expression, **build_location) | ||
end | ||
end | ||
|
||
private | ||
|
||
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 | ||
end | ||
|
||
# Provide `CharacterSet` with `begin` and `end` locations. | ||
module CharacterSet | ||
def build_location | ||
h = super | ||
body = h[:body] | ||
h.merge!( | ||
begin_l: body.with(end_pos: body.begin_pos + 1), | ||
end_l: body.with(begin_pos: body.end_pos - 1) | ||
) | ||
end | ||
end | ||
end | ||
::Regexp::Expression::Base.include Expression::Base | ||
::Regexp::Expression::CharacterSet.include Expression::CharacterSet | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters