File tree Expand file tree Collapse file tree 3 files changed +26
-4
lines changed Expand file tree Collapse file tree 3 files changed +26
-4
lines changed Original file line number Diff line number Diff line change
1
+ * [ #831 ] ( https://github.com/rubocop/rubocop-rails/pull/831 ) : Fix a false positive for ` Rails/Pluck ` when using block argument in ` [] ` . ([ @koic ] [ ] )
Original file line number Diff line number Diff line change @@ -26,15 +26,16 @@ class Pluck < Base
26
26
minimum_target_rails_version 5.0
27
27
28
28
def_node_matcher :pluck_candidate? , <<~PATTERN
29
- ({block numblock} (send _ {:map :collect}) $_argument (send ( lvar $_element) :[] $_key))
29
+ ({block numblock} (send _ {:map :collect}) $_argument (send lvar :[] $_key))
30
30
PATTERN
31
31
32
32
def on_block ( node )
33
- pluck_candidate? ( node ) do |argument , element , key |
33
+ pluck_candidate? ( node ) do |argument , key |
34
34
match = if node . block_type?
35
- argument . children . first . source . to_sym == element
35
+ block_argument = argument . children . first . source
36
+ use_block_argument_in_key? ( block_argument , key )
36
37
else # numblock
37
- argument == 1 && element == :_1
38
+ argument == 1 && use_block_argument_in_key? ( '_1' , key )
38
39
end
39
40
next unless match
40
41
@@ -50,6 +51,10 @@ def on_block(node)
50
51
51
52
private
52
53
54
+ def use_block_argument_in_key? ( block_argument , key )
55
+ key . each_descendant ( :lvar ) . none? { |lvar | block_argument == lvar . source }
56
+ end
57
+
53
58
def offense_range ( node )
54
59
node . send_node . loc . selector . join ( node . loc . end )
55
60
end
Original file line number Diff line number Diff line change 50
50
end
51
51
end
52
52
53
+ context 'when the block argument is used in `[]`' do
54
+ it 'does not register an offense' do
55
+ expect_no_offenses ( <<~RUBY )
56
+ x.#{ method } { |a| a[foo...a.to_someghing] }
57
+ RUBY
58
+ end
59
+ end
60
+
53
61
context 'when using Ruby 2.7 or newer' , :ruby27 do
54
62
context 'when using numbered parameter' do
55
63
context "when `#{ method } ` can be replaced with `pluck`" do
64
72
RUBY
65
73
end
66
74
end
75
+
76
+ context 'when the numblock argument is used in `[]`' do
77
+ it 'does not register an offense' do
78
+ expect_no_offenses ( <<~RUBY )
79
+ x.#{ method } { _1[foo..._1.to_someghing] }
80
+ RUBY
81
+ end
82
+ end
67
83
end
68
84
end
69
85
end
You can’t perform that action at this time.
0 commit comments