Skip to content

Commit 85d615f

Browse files
committed
[Fix rubocop#833] Fix a false positive for Rails/Pluck
Fixes rubocop#833 and rubocop#835. This commit fixes a false positive for `Rails/Pluck` when using multiple block arguments
1 parent 8663b04 commit 85d615f

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#833](https://github.com/rubocop/rubocop-rails/issues/833): Fix a false positive for `Rails/Pluck` when using multiple block arguments. ([@koic][])

lib/rubocop/cop/rails/pluck.rb

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ class Pluck < Base
3131

3232
def on_block(node)
3333
pluck_candidate?(node) do |argument, key|
34+
next unless use_one_block_argument?(argument)
35+
3436
match = if node.block_type?
3537
block_argument = argument.children.first.source
3638
use_block_argument_in_key?(block_argument, key)
@@ -39,18 +41,19 @@ def on_block(node)
3941
end
4042
next unless match
4143

42-
replacement = "pluck(#{key.source})"
43-
message = message(replacement, node)
44-
45-
add_offense(offense_range(node), message: message) do |corrector|
46-
corrector.replace(offense_range(node), replacement)
47-
end
44+
register_offense(node, key)
4845
end
4946
end
5047
alias on_numblock on_block
5148

5249
private
5350

51+
def use_one_block_argument?(argument)
52+
return true if argument == 1 # Checks for numbered argument `_1`.
53+
54+
argument.respond_to?(:one?) && argument.one?
55+
end
56+
5457
def use_block_argument_in_key?(block_argument, key)
5558
key.each_descendant(:lvar).none? { |lvar| block_argument == lvar.source }
5659
end
@@ -59,6 +62,15 @@ def offense_range(node)
5962
node.send_node.loc.selector.join(node.loc.end)
6063
end
6164

65+
def register_offense(node, key)
66+
replacement = "pluck(#{key.source})"
67+
message = message(replacement, node)
68+
69+
add_offense(offense_range(node), message: message) do |corrector|
70+
corrector.replace(offense_range(node), replacement)
71+
end
72+
end
73+
6274
def message(replacement, node)
6375
current = offense_range(node).source
6476

spec/rubocop/cop/rails/pluck_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@
5858
end
5959
end
6060

61+
context 'when there are multiple block arguments' do
62+
it 'does not register an offense' do
63+
expect_no_offenses(<<~RUBY)
64+
x.#{method} { |_, obj| obj['id'] }
65+
RUBY
66+
end
67+
end
68+
6169
context 'when using Ruby 2.7 or newer', :ruby27 do
6270
context 'when using numbered parameter' do
6371
context "when `#{method}` can be replaced with `pluck`" do
@@ -80,6 +88,14 @@
8088
RUBY
8189
end
8290
end
91+
92+
context 'when numblock argument is not `_1`' do
93+
it 'does not register an offense' do
94+
expect_no_offenses(<<~RUBY)
95+
x.#{method} { _2['id'] }
96+
RUBY
97+
end
98+
end
8399
end
84100
end
85101
end

0 commit comments

Comments
 (0)