forked from rubocop/rubocop-performance
-
-
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.
Fixes rubocop#178. This PR adds new `Performance/FilterMap` cop. In Ruby 2.7, `Enumerable#filter_map` has been added. This cop identifies places where `select.map` can be replaced by `filter_map`. And it allows `map { ... }.compact` that is not compatible with `filter_map`. ```ruby [true, false, nil].compact #=> [true, false] [true, false, nil].filter_map(&:itself) #=> [true] ``` The following is good and bad cases. ```ruby # bad ary.select(&:foo).map(&:bar) ary.filter(&:foo).map(&:bar) # good ary.filter_map { |o| o.bar if o.foo } ary.map(&:foo).compact # Consider whether you can safely replace it with `filter_map`. ```
- Loading branch information
Showing
7 changed files
with
226 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,69 @@ | ||
# 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 `select.map` can be replaced by `filter_map`. | ||
# | ||
# And it allows `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.select(&:foo).map(&:bar) | ||
# ary.filter(&:foo).map(&:bar) | ||
# | ||
# # good | ||
# ary.filter_map { |o| o.bar if o.foo } | ||
# ary.map(&:foo).compact # Consider whether you can safely replace it with `filter_map`. | ||
# | ||
class FilterMap < Base | ||
include RangeHelp | ||
extend TargetRubyVersion | ||
|
||
minimum_target_ruby_version 2.7 | ||
|
||
MSG = 'Use `filter_map` instead of `%<method_name>s.map`.' | ||
RESTRICT_ON_SEND = %i[select filter].freeze | ||
|
||
def_node_matcher :bad_method?, <<~PATTERN | ||
(send nil? :bad_method ...) | ||
PATTERN | ||
|
||
def on_send(node) | ||
return if (first_argument = node.first_argument) && !first_argument.block_pass_type? | ||
return unless (send_node = map_method_candidate(node)) | ||
return unless send_node.method?(:map) | ||
|
||
map_method = send_node.parent&.block_type? ? send_node.parent : send_node | ||
|
||
range = offense_range(node, map_method) | ||
add_offense(range, message: format(MSG, method_name: node.method_name)) | ||
end | ||
|
||
private | ||
|
||
def map_method_candidate(node) | ||
return unless (parent = node.parent) | ||
|
||
if parent.block_type? && parent.parent&.send_type? | ||
parent.parent | ||
elsif parent.send_type? | ||
parent | ||
end | ||
end | ||
|
||
def offense_range(node, map_method) | ||
range_between(node.loc.selector.begin_pos, map_method.loc.expression.end_pos) | ||
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,110 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Performance::FilterMap, :config do | ||
context 'TargetRubyVersion >= 2.7', :ruby27 do | ||
it 'registers an offense when using `select(&:...).map(&:...)`' do | ||
expect_offense(<<~RUBY) | ||
ary.select(&:present?).map(&:to_i) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `filter_map` instead of `select.map`. | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `filter(&:...).map(&:...)`' do | ||
expect_offense(<<~RUBY) | ||
ary.filter(&:present?).map(&:to_i) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `filter_map` instead of `filter.map`. | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `select { ... }.map { ... }`' do | ||
expect_offense(<<~RUBY) | ||
ary.select { |o| o.present? }.map { |o| o.to_i } | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `filter_map` instead of `select.map`. | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `select { ... }.map(&:...)`' do | ||
expect_offense(<<~RUBY) | ||
ary.select { |o| o.present? }.map(&:to_i) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `filter_map` instead of `select.map`. | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `select(&:...).map { ... }`' do | ||
expect_offense(<<~RUBY) | ||
ary.select(&:present?).map { |o| o.to_i } | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `filter_map` instead of `select.map`. | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `select(&:...).map(&:...)` in method chain' do | ||
expect_offense(<<~RUBY) | ||
ary.do_something.select(&:present?).map(&:to_i).max | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `filter_map` instead of `select.map`. | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `select(&:...).map(&:...)` without receiver' do | ||
expect_offense(<<~RUBY) | ||
select(&:present?).map(&:to_i) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `filter_map` instead of `select.map`. | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `filter_map`' do | ||
expect_no_offenses(<<~RUBY) | ||
ary.filter_map { |o| o.to_i if o.present? } | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `select`' do | ||
expect_no_offenses(<<~RUBY) | ||
ary.select(&:present?) | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `select(key: value).map`' do | ||
expect_no_offenses(<<~RUBY) | ||
ary.do_something.select(key: value).map(&:to_i) | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `select` with assignment' do | ||
expect_no_offenses(<<~RUBY) | ||
ret = select | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `select(&:...).stranger.map(&:...)`' do | ||
expect_no_offenses(<<~RUBY) | ||
ary.do_something.select(&:present?).stranger.map(&:to_i).max | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `select { ... }.stranger.map { ... }`' do | ||
expect_no_offenses(<<~RUBY) | ||
ary.select { |o| o.present? }.stranger.map { |o| o.to_i } | ||
RUBY | ||
end | ||
|
||
# | ||
# `compact` is not compatible with `filter_map`. | ||
# | ||
# [true, false, nil].compact #=> [true, false] | ||
# [true, false, nil].filter_map(&:itself) #=> [true] | ||
# | ||
it 'does not register an offense when using `map(&:...).compact`' do | ||
expect_no_offenses(<<~RUBY) | ||
ary.map(&:to_i).compact | ||
RUBY | ||
end | ||
end | ||
|
||
context 'TargetRubyVersion <= 2.6', :ruby26 do | ||
it 'does not register an offense when using `select.map`' do | ||
expect_no_offenses(<<~RUBY) | ||
ary.select(&:present?).map(&:to_i) | ||
RUBY | ||
end | ||
end | ||
end |