Skip to content

Commit

Permalink
Merge pull request #8847 from koic/fix_incorrect_autocorrect_for_ambi…
Browse files Browse the repository at this point in the history
…guous_regexp_literal

[Fix #8843] Fix an incorrect autocorrect for `Lint/AmbiguousRegexpLiteral`
  • Loading branch information
koic committed Oct 4, 2020
2 parents 99818f6 + 81857fa commit 9e8a53d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -18,6 +18,7 @@
* [#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][])
* [#8848](https://github.com/rubocop-hq/rubocop/issues/8848): Fix a false positive for `Style/CombinableLoops` when using the same method with different arguments. ([@dvandersluis][])
* [#8843](https://github.com/rubocop-hq/rubocop/issues/8843): Fix an incorrect autocorrect for `Lint/AmbiguousRegexpLiteral` when sending method to regexp literal receiver. ([@koic][])

### Changes

Expand Down
16 changes: 15 additions & 1 deletion lib/rubocop/cop/lint/ambiguous_regexp_literal.rb
Expand Up @@ -47,7 +47,21 @@ def find_offense_node_by(diagnostic)
regexp_node.source_range.begin_pos == diagnostic.location.begin_pos
end

node.parent
find_offense_node(node.parent, node)
end

def find_offense_node(node, regexp_receiver)
return node unless node.parent

if node.parent.send_type? || method_chain_to_regexp_receiver?(node)
node = find_offense_node(node.parent, regexp_receiver)
end

node
end

def method_chain_to_regexp_receiver?(node)
node.parent.parent && node.parent.receiver.receiver == regexp_receiver
end
end
end
Expand Down
33 changes: 33 additions & 0 deletions spec/rubocop/cop/lint/ambiguous_regexp_literal_spec.rb
Expand Up @@ -27,6 +27,39 @@
RUBY
end

it 'registers an offense and corrects when sending method to regexp without argument' do
expect_offense(<<~RUBY)
p /pattern/.do_something
^ 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)
p(/pattern/.do_something)
RUBY
end

it 'registers an offense and corrects when sending method to regexp with argument' do
expect_offense(<<~RUBY)
p /pattern/.do_something(42)
^ 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)
p(/pattern/.do_something(42))
RUBY
end

it 'registers an offense and corrects when sending method chain to regexp' do
expect_offense(<<~RUBY)
p /pattern/.do_something.do_something
^ 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)
p(/pattern/.do_something.do_something)
RUBY
end

it 'registers an offense and corrects when using block argument' do
expect_offense(<<~RUBY)
p /pattern/, foo do |arg|
Expand Down

0 comments on commit 9e8a53d

Please sign in to comment.