-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
198 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* [#8566](https://github.com/rubocop-hq/rubocop/issues/8566): Add new `Style/CollectionCompact` cop. ([@fatkodima][]) |
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,85 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Style | ||
# This cop checks for places where custom logic on rejection nils from arrays | ||
# and hashes can be replaced with `{Array,Hash}#{compact,compact!}`. | ||
# | ||
# @example | ||
# # bad | ||
# array.reject { |e| e.nil? } | ||
# array.select { |e| !e.nil? } | ||
# | ||
# # good | ||
# array.compact | ||
# | ||
# # bad | ||
# hash.reject! { |k, v| v.nil? } | ||
# hash.select! { |k, v| !v.nil? } | ||
# | ||
# # good | ||
# hash.compact! | ||
# | ||
class CollectionCompact < Base | ||
include RangeHelp | ||
extend AutoCorrector | ||
|
||
MSG = 'Use `%<good>s` instead of `%<bad>s`.' | ||
|
||
RESTRICT_ON_SEND = %i[reject reject! select select!].freeze | ||
|
||
def_node_matcher :reject_method?, <<~PATTERN | ||
(block | ||
(send | ||
_ ${:reject :reject!}) | ||
$(args ...) | ||
(send | ||
$(lvar _) :nil?)) | ||
PATTERN | ||
|
||
def_node_matcher :select_method?, <<~PATTERN | ||
(block | ||
(send | ||
_ ${:select :select!}) | ||
$(args ...) | ||
(send | ||
(send | ||
$(lvar _) :nil?) :!)) | ||
PATTERN | ||
|
||
def on_send(node) | ||
block_node = node.parent | ||
return unless block_node&.block_type? | ||
|
||
return unless (method_name, args, receiver = | ||
reject_method?(block_node) || select_method?(block_node)) | ||
|
||
return unless args.last.source == receiver.source | ||
|
||
range = offense_range(node, block_node) | ||
good = good_method_name(method_name) | ||
message = format(MSG, good: good, bad: range.source) | ||
|
||
add_offense(range, message: message) do |corrector| | ||
corrector.replace(range, good) | ||
end | ||
end | ||
|
||
private | ||
|
||
def good_method_name(method_name) | ||
if method_name.to_s.end_with?('!') | ||
'compact!' | ||
else | ||
'compact' | ||
end | ||
end | ||
|
||
def offense_range(send_node, block_node) | ||
range_between(send_node.loc.selector.begin_pos, block_node.loc.end.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Style::CollectionCompact do | ||
subject(:cop) { described_class.new } | ||
|
||
it 'registers an offense and corrects when using `reject` on array to reject nils' do | ||
expect_offense(<<~RUBY) | ||
array.reject { |e| e.nil? } | ||
^^^^^^^^^^^^^^^^^^^^^ Use `compact` instead of `reject { |e| e.nil? }`. | ||
array.reject! { |e| e.nil? } | ||
^^^^^^^^^^^^^^^^^^^^^^ Use `compact!` instead of `reject! { |e| e.nil? }`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
array.compact | ||
array.compact! | ||
RUBY | ||
end | ||
|
||
it 'registers an offense and corrects when using `reject` on hash to reject nils' do | ||
expect_offense(<<~RUBY) | ||
hash.reject { |k, v| v.nil? } | ||
^^^^^^^^^^^^^^^^^^^^^^^^ Use `compact` instead of `reject { |k, v| v.nil? }`. | ||
hash.reject! { |k, v| v.nil? } | ||
^^^^^^^^^^^^^^^^^^^^^^^^^ Use `compact!` instead of `reject! { |k, v| v.nil? }`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
hash.compact | ||
hash.compact! | ||
RUBY | ||
end | ||
|
||
it 'registers an offense and corrects when using `select/select!` to reject nils' do | ||
expect_offense(<<~RUBY) | ||
array.select { |e| !e.nil? } | ||
^^^^^^^^^^^^^^^^^^^^^^ Use `compact` instead of `select { |e| !e.nil? }`. | ||
hash.select! { |k, v| !v.nil? } | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `compact!` instead of `select! { |k, v| !v.nil? }`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
array.compact | ||
hash.compact! | ||
RUBY | ||
end | ||
|
||
it 'registers an offense and corrects when using `reject` without a receiver to reject nils' do | ||
expect_offense(<<~RUBY) | ||
reject { |e| e.nil? } | ||
^^^^^^^^^^^^^^^^^^^^^ Use `compact` instead of `reject { |e| e.nil? }`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
compact | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `reject` to not to rejecting nils' do | ||
expect_no_offenses(<<~RUBY) | ||
array.reject { |e| e.odd? } | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `compact/compact!`' do | ||
expect_no_offenses(<<~RUBY) | ||
array.compact | ||
array.compact! | ||
RUBY | ||
end | ||
end |