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
2727 # end
2828 # ----
2929 #
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+ #
3033 # @safety
3134 # This cop is unsafe because model can use column aliases.
3235 #
@@ -59,9 +62,9 @@ class Pluck < Base
5962 (any_block (call _ {:map :collect}) $_argument (send lvar :[] $_key))
6063 PATTERN
6164
62- # rubocop:disable Metrics/AbcSize
65+ # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
6366 def on_block ( node )
64- return if node . each_ancestor ( :any_block ) . any?
67+ return if node . each_ancestor ( :any_block ) . first &. receiver
6568
6669 pluck_candidate? ( node ) do |argument , key |
6770 next if key . regexp_type? || !use_one_block_argument? ( argument )
@@ -79,7 +82,7 @@ def on_block(node)
7982 register_offense ( node , key )
8083 end
8184 end
82- # rubocop:enable Metrics/AbcSize
85+ # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
8386 alias on_numblock on_block
8487 alias on_itblock on_block
8588
Original file line number Diff line number Diff line change 154154 end
155155 end
156156
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
158175 it 'does not register an offense' do
159176 expect_no_offenses ( <<~RUBY )
160177 n.each do |x|
164181 end
165182 end
166183
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
168185 it 'does not register an offense' do
169186 expect_no_offenses ( <<~RUBY )
170187 n.each do |x|
175192 end
176193 end
177194
178- context "when `#{ method } ` is used in numblock" do
195+ context "when `#{ method } ` is used in a repeatable numblock" do
179196 it 'does not register an offense' do
180197 expect_no_offenses ( <<~RUBY )
181198 n.each do
185202 end
186203 end
187204
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
189206 it 'does not register an offense' do
190207 expect_no_offenses ( <<~RUBY )
191208 n.each do
You can’t perform that action at this time.
0 commit comments