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

[Fix #8843] Fix invalid Lint/AmbiguousRegexpLiteral autocorrect when the original node had internal parentheses #8846

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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