Skip to content

Commit

Permalink
Merge pull request #375 from koic/fix_an_error_for_performance_map_me…
Browse files Browse the repository at this point in the history
…thod_chain

[Fix #374] Fix an error for `Performance/MapMethodChain`
  • Loading branch information
koic committed Oct 1, 2023
2 parents 14b6e21 + 296489d commit 2086a84
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/fix_an_error_for_performance_map_method_chain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#374](https://github.com/rubocop/rubocop-performance/issues/374): Fix an error for `Performance/MapMethodChain` when using `map` method chain without receiver. ([@koic][])
4 changes: 3 additions & 1 deletion lib/rubocop/cop/performance/map_method_chain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def on_send(node)

private

# rubocop:disable Metrics/CyclomaticComplexity
def find_begin_of_chained_map_method(node, map_args)
return unless (chained_map_method = node.receiver)
return if !chained_map_method.call_type? || !RESTRICT_ON_SEND.include?(chained_map_method.method_name)
Expand All @@ -77,10 +78,11 @@ def find_begin_of_chained_map_method(node, map_args)

receiver = chained_map_method.receiver

return chained_map_method unless receiver.call_type? && block_pass_with_symbol_arg?(receiver.first_argument)
return chained_map_method unless receiver&.call_type? && block_pass_with_symbol_arg?(receiver.first_argument)

find_begin_of_chained_map_method(chained_map_method, map_args)
end
# rubocop:enable Metrics/CyclomaticComplexity
end
end
end
Expand Down
7 changes: 7 additions & 0 deletions spec/rubocop/cop/performance/map_method_chain_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@
RUBY
end

it 'registers an offense when using `map` method chain without receiver' do
expect_offense(<<~RUBY)
map(&:foo).map(&:bar)
^^^^^^^^^^^^^^^^^^^^^ Use `map { |x| x.foo.bar }` instead of `map` method chain.
RUBY
end

it 'does not register an offense when using `do_something` method chain and receiver is a method call' do
expect_no_offenses(<<~RUBY)
array.do_something(&:foo).do_something(&:bar)
Expand Down

0 comments on commit 2086a84

Please sign in to comment.