File tree 3 files changed +57
-1
lines changed
lib/rubocop/cop/performance
spec/rubocop/cop/performance
3 files changed +57
-1
lines changed Original file line number Diff line number Diff line change 2
2
3
3
## master (unreleased)
4
4
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
+
5
9
## 1.11.0 (2021-04-22)
6
10
7
11
### New features
Original file line number Diff line number Diff line change @@ -56,7 +56,19 @@ def on_send(node)
56
56
add_offense ( range ) do |corrector |
57
57
corrector . replace ( map_node . loc . selector , 'filter_map' )
58
58
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
60
72
end
61
73
end
62
74
end
Original file line number Diff line number Diff line change 46
46
RUBY
47
47
end
48
48
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
+
49
89
it 'does not register an offense when using `collection.map(&:do_something).compact!`' do
50
90
expect_no_offenses ( <<~RUBY )
51
91
collection.map(&:do_something).compact!
You can’t perform that action at this time.
0 commit comments