Skip to content

Commit

Permalink
[Fix rubocop#10510] Fix an error for Style/SingleArgumentDig
Browse files Browse the repository at this point in the history
Fixes rubocop#10510.

This PR fixes an error for `Style/SingleArgumentDig`
when using multiple `dig` in a method chain.

And this is the same solution as rubocop#10461.
  • Loading branch information
koic committed Apr 8, 2022
1 parent 2d3a972 commit e66cd48
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog/fix_an_error_for_style_single_argument_dig.md
@@ -0,0 +1 @@
* [#10510](https://github.com/rubocop/rubocop/issues/10510): Fix an error for `Style/SingleArgumentDig` when using multiple `dig` in a method chain. ([@koic][])
4 changes: 4 additions & 0 deletions lib/rubocop/cop/style/single_argument_dig.rb
Expand Up @@ -50,9 +50,13 @@ def on_send(node)

message = format(MSG, receiver: receiver, argument: argument, original: node.source)
add_offense(node, message: message) do |corrector|
next if part_of_ignored_node?(node)

correct_access = "#{receiver}[#{argument}]"
corrector.replace(node, correct_access)
end

ignore_node(node)
end
end
end
Expand Down
14 changes: 14 additions & 0 deletions spec/rubocop/cop/style/single_argument_dig_spec.rb
Expand Up @@ -54,6 +54,20 @@
end
end

context 'when using multiple `dig` in a method chain' do
it 'registers and corrects an offense' do
expect_offense(<<~RUBY)
data.dig(var1)[0].dig(var2)
^^^^^^^^^^^^^^ Use `data[var1]` instead of `data.dig(var1)`.
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `data.dig(var1)[0][var2]` instead of `data.dig(var1)[0].dig(var2)`.
RUBY

expect_correction(<<~RUBY)
data.dig(var1)[0][var2]
RUBY
end
end

context 'when using dig with splat operator' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
Expand Down

0 comments on commit e66cd48

Please sign in to comment.