Skip to content

Commit

Permalink
[Fix rubocop#8843] Fix invalid Lint/AmbiguousRegexpLiteral autocorr…
Browse files Browse the repository at this point in the history
…ect when the original node had internal parentheses.
  • Loading branch information
dvandersluis committed Oct 3, 2020
1 parent 918f7bb commit ecc84aa
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -16,6 +16,7 @@
* [#8354](https://github.com/rubocop-hq/rubocop/issues/8354): Detect regexp named captures in `Style/CaseLikeIf` cop. ([@dsavochkin][])
* [#8830](https://github.com/rubocop-hq/rubocop/issues/8830): Fix bad autocorrect of `Style/StringConcatenation` when string includes double quotes. ([@tleish][])
* [#8807](https://github.com/rubocop-hq/rubocop/pull/8807): Fix a false positive for `Style/RedundantCondition` when using assignment by hash key access. ([@koic][])
* [#8843](https://github.com/rubocop-hq/rubocop/issues/8843): Fix invalid `Lint/AmbiguousRegexpLiteral` autocorrect when the original node had internal parentheses. ([@dvandersluis][])

### Changes

Expand Down
3 changes: 2 additions & 1 deletion lib/rubocop/cop/lint/ambiguous_regexp_literal.rb
Expand Up @@ -35,7 +35,8 @@ def on_new_investigation
offense_node = find_offense_node_by(diagnostic)

add_offense(diagnostic.location, severity: diagnostic.level) do |corrector|
add_parentheses(offense_node, corrector)
corrector.replace(diagnostic.location.begin.adjust(begin_pos: -1), '(')
corrector.insert_after(offense_node, ')')
end
end
end
Expand Down
11 changes: 11 additions & 0 deletions spec/rubocop/cop/lint/ambiguous_regexp_literal_spec.rb
Expand Up @@ -55,6 +55,17 @@
end
RUBY
end

it 'correctly handles inner parentheses' do
expect_offense(<<~RUBY)
assert /foobar/.match('foo')
^ Ambiguous regexp literal. Parenthesize the method arguments if it's surely a regexp literal, or add a whitespace to the right of the `/` if it should be a division.
RUBY

expect_correction(<<~RUBY)
assert(/foobar/.match('foo'))
RUBY
end
end

context 'with parentheses' do
Expand Down

0 comments on commit ecc84aa

Please sign in to comment.