Skip to content

Commit f6e59b5

Browse files
committed
[Fix rubocop#236] Fix an incorrect auto-correct for Performance/MapCompact
Fixes rubocop#236. This PR fix an incorrect auto-correct for `Performance/MapCompact` when using multi-line leading dot method calls.
1 parent d51f1c7 commit f6e59b5

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## master (unreleased)
44

5+
### Bug fixes
6+
7+
* [#236](https://github.com/rubocop/rubocop-performance/issues/236): Fix an incorrect auto-correct for `Performance/MapCompact` when using multi-line leading dot method calls. ([@koic][])
8+
59
## 1.11.0 (2021-04-22)
610

711
### New features

lib/rubocop/cop/performance/map_compact.rb

+13-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,19 @@ def on_send(node)
5656
add_offense(range) do |corrector|
5757
corrector.replace(map_node.loc.selector, 'filter_map')
5858
corrector.remove(compact_loc.dot)
59-
corrector.remove(compact_loc.selector)
59+
corrector.remove(compact_method_range(node))
60+
end
61+
end
62+
63+
private
64+
65+
def compact_method_range(compact_node)
66+
compact_method_range = compact_node.loc.selector
67+
68+
if compact_node.multiline?
69+
range_by_whole_lines(compact_method_range, include_final_newline: true)
70+
else
71+
compact_method_range
6072
end
6173
end
6274
end

spec/rubocop/cop/performance/map_compact_spec.rb

+40
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,46 @@
4646
RUBY
4747
end
4848

49+
it 'registers an offense when using `map.compact.first` with single-line method calls' do
50+
expect_offense(<<~RUBY)
51+
collection.map { |item| item.do_something }.compact.first
52+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `filter_map` instead.
53+
RUBY
54+
55+
expect_correction(<<~RUBY)
56+
collection.filter_map { |item| item.do_something }.first
57+
RUBY
58+
end
59+
60+
it 'registers an offense when using `map.compact.first` with multi-line leading dot method calls' do
61+
expect_offense(<<~RUBY)
62+
collection
63+
.map { |item| item.do_something }
64+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `filter_map` instead.
65+
.compact
66+
.first
67+
RUBY
68+
69+
expect_correction(<<~RUBY)
70+
collection
71+
.filter_map { |item| item.do_something }
72+
.first
73+
RUBY
74+
end
75+
76+
it 'registers an offense when using `map.compact.first` and there is a line break after `map.compact`' do
77+
expect_offense(<<~RUBY)
78+
collection.map { |item| item.do_something }.compact
79+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `filter_map` instead.
80+
.first
81+
RUBY
82+
83+
expect_correction(<<~RUBY)
84+
collection.filter_map { |item| item.do_something }
85+
.first
86+
RUBY
87+
end
88+
4989
it 'does not register an offense when using `collection.map(&:do_something).compact!`' do
5090
expect_no_offenses(<<~RUBY)
5191
collection.map(&:do_something).compact!

0 commit comments

Comments
 (0)