-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Follow rubocop/rubocop-performance#211 (comment) This PR adds `Performance/MapCompact` cop In Ruby 2.7, `Enumerable#filter_map` has been added. This cop identifies places where `map { ... }.compact` can be replaced by `filter_map`. It is marked as unsafe auto-correction by default because `map { ... }.compact` that is not compatible with `filter_map`. The following is good and bad cases. ```ruby # bad ary.map(&:foo).compact ary.collect(&:foo).compact # good ary.filter_map(&:foo) ary.map(&:foo).compact! ```
- Loading branch information
1 parent
9253077
commit 12bb874
Showing
7 changed files
with
215 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Performance | ||
# In Ruby 2.7, `Enumerable#filter_map` has been added. | ||
# | ||
# This cop identifies places where `map { ... }.compact` can be replaced by `filter_map`. | ||
# It is marked as unsafe auto-correction by default because `map { ... }.compact` | ||
# that is not compatible with `filter_map`. | ||
# | ||
# [source,ruby] | ||
# ---- | ||
# [true, false, nil].compact #=> [true, false] | ||
# [true, false, nil].filter_map(&:itself) #=> [true] | ||
# ---- | ||
# | ||
# @example | ||
# # bad | ||
# ary.map(&:foo).compact | ||
# ary.collect(&:foo).compact | ||
# | ||
# # good | ||
# ary.filter_map(&:foo) | ||
# ary.map(&:foo).compact! | ||
# | ||
class MapCompact < Base | ||
include RangeHelp | ||
extend AutoCorrector | ||
extend TargetRubyVersion | ||
|
||
MSG = 'Use `filter_map` instead.' | ||
RESTRICT_ON_SEND = %i[compact].freeze | ||
|
||
minimum_target_ruby_version 2.7 | ||
|
||
def_node_matcher :map_compact, <<~PATTERN | ||
{ | ||
(send | ||
$(send _ {:map :collect} | ||
(block_pass | ||
(sym _))) _) | ||
(send | ||
(block | ||
$(send _ {:map :collect}) | ||
(args ...) _) _) | ||
} | ||
PATTERN | ||
|
||
def on_send(node) | ||
return unless (map_node = map_compact(node)) | ||
|
||
compact_loc = node.loc | ||
range = range_between(map_node.loc.selector.begin_pos, compact_loc.selector.end_pos) | ||
|
||
add_offense(range) do |corrector| | ||
corrector.replace(map_node.loc.selector, 'filter_map') | ||
corrector.remove(compact_loc.dot) | ||
corrector.remove(compact_loc.selector) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Performance::MapCompact, :config do | ||
context 'TargetRubyVersion >= 2.7', :ruby27 do | ||
it 'registers an offense when using `collection.map(&:do_something).compact`' do | ||
expect_offense(<<~RUBY) | ||
collection.map(&:do_something).compact | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `filter_map` instead. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
collection.filter_map(&:do_something) | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `collection.map { |item| item.do_something }.compact`' do | ||
expect_offense(<<~RUBY) | ||
collection.map { |item| item.do_something }.compact | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `filter_map` instead. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
collection.filter_map { |item| item.do_something } | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `collection.collect(&:do_something).compact`' do | ||
expect_offense(<<~RUBY) | ||
collection.collect(&:do_something).compact | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `filter_map` instead. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
collection.filter_map(&:do_something) | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `collection.collect { |item| item.do_something }.compact`' do | ||
expect_offense(<<~RUBY) | ||
collection.collect { |item| item.do_something }.compact | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `filter_map` instead. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
collection.filter_map { |item| item.do_something } | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `collection.map(&:do_something).compact!`' do | ||
expect_no_offenses(<<~RUBY) | ||
collection.map(&:do_something).compact! | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `collection.map { |item| item.do_something }.compact!`' do | ||
expect_no_offenses(<<~RUBY) | ||
collection.map { |item| item.do_something }.compact! | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `collection.collect(&:do_something).compact!`' do | ||
expect_no_offenses(<<~RUBY) | ||
collection.collect(&:do_something).compact! | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `collection.collect { |item| item.do_something }.compact!`' do | ||
expect_no_offenses(<<~RUBY) | ||
collection.collect { |item| item.do_something }.compact! | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `collection.not_map_method(&:do_something).compact`' do | ||
expect_no_offenses(<<~RUBY) | ||
collection.not_map_method(&:do_something).compact | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `collection.filter_map(&:do_something)`' do | ||
expect_no_offenses(<<~RUBY) | ||
collection.filter_map(&:do_something) | ||
RUBY | ||
end | ||
end | ||
|
||
context 'TargetRubyVersion <= 2.6', :ruby26 do | ||
it 'does not register an offense when using `collection.map(&:do_something).compact`' do | ||
expect_no_offenses(<<~RUBY) | ||
collection.map(&:do_something).compact | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `collection.map { |item| item.do_something }.compact`' do | ||
expect_no_offenses(<<~RUBY) | ||
collection.map { |item| item.do_something }.compact | ||
RUBY | ||
end | ||
end | ||
end |