File tree Expand file tree Collapse file tree 3 files changed +28
-7
lines changed Expand file tree Collapse file tree 3 files changed +28
-7
lines changed Original file line number Diff line number Diff line change
1
+ * [ #1505 ] ( https://github.com/rubocop/rubocop-rails/issues/1505 ) : Fix false negatives for ` Rails/Pluck ` when ` map ` method call is used in a block without a receiver. ([ @koic ] [ ] )
Original file line number Diff line number Diff line change @@ -27,6 +27,9 @@ module Rails
27
27
# end
28
28
# ----
29
29
#
30
+ # If a method call has no receiver, like `do_something { users.map { |user| user[:foo] }`,
31
+ # it is not considered part of an iteration and will be detected.
32
+ #
30
33
# @safety
31
34
# This cop is unsafe because model can use column aliases.
32
35
#
@@ -59,9 +62,9 @@ class Pluck < Base
59
62
(any_block (call _ {:map :collect}) $_argument (send lvar :[] $_key))
60
63
PATTERN
61
64
62
- # rubocop:disable Metrics/AbcSize
65
+ # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
63
66
def on_block ( node )
64
- return if node . each_ancestor ( :any_block ) . any?
67
+ return if node . each_ancestor ( :any_block ) . first &. receiver
65
68
66
69
pluck_candidate? ( node ) do |argument , key |
67
70
next if key . regexp_type? || !use_one_block_argument? ( argument )
@@ -79,7 +82,7 @@ def on_block(node)
79
82
register_offense ( node , key )
80
83
end
81
84
end
82
- # rubocop:enable Metrics/AbcSize
85
+ # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
83
86
alias on_numblock on_block
84
87
alias on_itblock on_block
85
88
Original file line number Diff line number Diff line change 154
154
end
155
155
end
156
156
157
- context "when `#{ method } ` is used in block" do
157
+ context "when `#{ method } ` is used in a block without a receiver" do
158
+ it 'registers an offense' do
159
+ expect_offense ( <<~RUBY , method : method )
160
+ foo do
161
+ x.%{method} { |a| a[:foo] }
162
+ ^{method}^^^^^^^^^^^^^^^^ Prefer `pluck(:foo)` over `%{method} { |a| a[:foo] }`.
163
+ end
164
+ RUBY
165
+
166
+ expect_correction ( <<~RUBY )
167
+ foo do
168
+ x.pluck(:foo)
169
+ end
170
+ RUBY
171
+ end
172
+ end
173
+
174
+ context "when `#{ method } ` is used in a repeatable block" do
158
175
it 'does not register an offense' do
159
176
expect_no_offenses ( <<~RUBY )
160
177
n.each do |x|
164
181
end
165
182
end
166
183
167
- context "when `#{ method } ` is used in block with other operations" do
184
+ context "when `#{ method } ` is used in a repeatable block with other operations" do
168
185
it 'does not register an offense' do
169
186
expect_no_offenses ( <<~RUBY )
170
187
n.each do |x|
175
192
end
176
193
end
177
194
178
- context "when `#{ method } ` is used in numblock" do
195
+ context "when `#{ method } ` is used in a repeatable numblock" do
179
196
it 'does not register an offense' do
180
197
expect_no_offenses ( <<~RUBY )
181
198
n.each do
185
202
end
186
203
end
187
204
188
- context "when `#{ method } ` is used in numblock with other operations" do
205
+ context "when `#{ method } ` is used in reapeatable numblock with other operations" do
189
206
it 'does not register an offense' do
190
207
expect_no_offenses ( <<~RUBY )
191
208
n.each do
You can’t perform that action at this time.
0 commit comments