Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Regexp::Expression#loc and #expression. Remove parsed_tree_expr_loc #8960

Merged
merged 2 commits into from
Oct 28, 2020

Conversation

marcandre
Copy link
Contributor

The source information provided by regexp_parser is difficult to use.

This PR adds a parser-like location map to these nodes. E.g.:

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

Will be useful for #8948

We'll be adding references to the source to the nodes,
so I can't think of any other way to proceed that won't involve
unacceptable memory leaks.
@marcandre
Copy link
Contributor Author

cc/ @owst

Copy link
Contributor

@owst owst left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really nice @marcandre, I wish I'd thought of this 😄 - a few minor comments.

changelog/new_add_regexpregexpexpressionloc_and.md Outdated Show resolved Hide resolved
lib/rubocop/ext/regexp_node.rb Outdated Show resolved Hide resolved
@@ -43,7 +43,7 @@ def each_repeated_character_class_element_loc(node)

child_source = child.to_s

yield node.parsed_tree_expr_loc(child) if seen.include?(child_source)
yield child.expression if seen.include?(child_source)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much neater 👍

lib/rubocop/ext/regexp_parser.rb Outdated Show resolved Hide resolved
@marcandre marcandre changed the title Add RegexpRegexp::Expression#loc and #expression. Deprecate parsed_tree_expr_loc Add Regexp::Expression#loc and #expression. Deprecate parsed_tree_expr_loc Oct 27, 2020
@marcandre marcandre changed the title Add Regexp::Expression#loc and #expression. Deprecate parsed_tree_expr_loc Add Regexp::Expression#loc and #expression. Remove parsed_tree_expr_loc Oct 27, 2020
@owst
Copy link
Contributor

owst commented Oct 27, 2020

LGTM 👍

@marcandre
Copy link
Contributor Author

Thanks for the quick review @owst 💛

@bbatsov
Copy link
Collaborator

bbatsov commented Oct 28, 2020

Nice improvement!

@bbatsov bbatsov merged commit 40ce90e into rubocop:master Oct 28, 2020
ysakasin added a commit to ysakasin/rubocop that referenced this pull request Oct 28, 2020
…terClass with %r

This commit uses `RegexpNode::RegexpParser#body` (rubocop#8960) instead of `Parser::Source::Range#adjust` in Style/RedundantRegexpCharacterClass.

When regular expression are written by %r literal, RedundantRegexpCharacterClass made incorrect loc as following.
```
%r{abc[\d]+}
    ^^^^^^
```

This bug are caused by calling`node.loc.begin.adjust` to get range of character class with %r regexp.
`Parser::Source::Range#adjust` adjust based on `Parser::Source::Range#begin_pos` but it with %r literal doesn't indicate start of regular expression source, point to location of '%'.
So, RedundantRegexpCharacterClass makes incorrect loc if %r literal are used in code.

```
r = %r{abc}
    ^      # `node.loc.begin.begin_pos`
```

```
r = %r{abs}
       ^   # `node.loc.begin.end_pos`
```
bbatsov pushed a commit that referenced this pull request Oct 29, 2020
…s with %r

This commit uses `RegexpNode::RegexpParser#body` (#8960) instead of `Parser::Source::Range#adjust` in Style/RedundantRegexpCharacterClass.

When regular expression are written by %r literal, RedundantRegexpCharacterClass made incorrect loc as following.
```
%r{abc[\d]+}
    ^^^^^^
```

This bug are caused by calling`node.loc.begin.adjust` to get range of character class with %r regexp.
`Parser::Source::Range#adjust` adjust based on `Parser::Source::Range#begin_pos` but it with %r literal doesn't indicate start of regular expression source, point to location of '%'.
So, RedundantRegexpCharacterClass makes incorrect loc if %r literal are used in code.

```
r = %r{abc}
    ^      # `node.loc.begin.begin_pos`
```

```
r = %r{abs}
       ^   # `node.loc.begin.end_pos`
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants