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
[Fix #8948] Fix autocorrection for Style/RedundantRegexpCharacterClass with %r #8954
[Fix #8948] Fix autocorrection for Style/RedundantRegexpCharacterClass with %r #8954
Conversation
a187221
to
e397e29
Compare
Thanks for fixing this @ysakasin! I recently added What about something like: def parsed_tree_expr_loc(expr, loc_size: expr.full_length)
loc.begin.end.adjust(begin_pos: expr.ts, end_pos: expr.ts).resize(loc_size)
end |
e397e29
to
074bc8c
Compare
@owst Thanks reviewing! I modify to use |
It's very clucky to have to provide |
Like following? def each_redundant_character_class(node)
each_single_element_character_class(node) do |char_class|
next unless redundant_single_element_character_class?(node, char_class)
# This means that: '['.length + len_char_class_source + ']'.length
len_char_class = 1 + char_class.expressions.first.text.length + 1
yield node.loc.expression.adjust(begin_pos: char_class.ts).resize(len_char_class)
end
end |
Not what I meant. Let me prepare a PR |
I created #8960. I believe that the correction would then simply be to |
074bc8c
to
20ec0fb
Compare
…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` ```
20ec0fb
to
0e74823
Compare
@marcandre Thanks adding awesome method! |
Looks great! Thanks! |
This commit uses
RegexpParser::Map#body
instead ofParser::Source::Range#adjust
in Style/RedundantRegexpCharacterClass.When regular expression are written by %r literal, RedundantRegexpCharacterClass made incorrect loc as following.
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 onParser::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.
Ref. #8948
Before submitting the PR make sure the following are checked:
[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).bundle exec rake default
. It executes all tests and RuboCop for itself, and generates the documentation.