-
-
Notifications
You must be signed in to change notification settings - Fork 83
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
1 parent
fca972f
commit ae2d49e
Showing
5 changed files
with
128 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 @@ | ||
* [#434](https://github.com/rubocop/rubocop-performance/issues/434): Add `ArrayConcatLiteral` cop, which replaces `array.concat([1, 2, 3])` with `array.push(1, 2, 3)`. ([@amomchilov][]) |
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,78 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Performance | ||
# Identifies places where concatinating a literal to an array | ||
# can be replaced by pushing its elements directly. | ||
# | ||
# @safety | ||
# This cop is unsafe because not all objects that respond to `#concat` also respond to `#push` | ||
# | ||
# @example | ||
# # bad | ||
# array.concat([1, 2, 3]) | ||
# | ||
# # good | ||
# array.push(1, 2, 3) | ||
# | ||
# # good | ||
# array.concat(not_a_literal) | ||
# | ||
class ArrayConcatLiteral < Base | ||
extend AutoCorrector | ||
|
||
MSG = 'Use `push` instead of concatinating a literal.' | ||
|
||
RESTRICT_ON_SEND = %i[concat].freeze | ||
|
||
def_node_matcher :concat_call?, <<~PATTERN | ||
(call $_receiver :concat $(array ...)) | ||
PATTERN | ||
|
||
def on_send(node) | ||
concat_call?(node) do |receiver, elements| | ||
add_offense(node) do |corrector| | ||
corrector.replace(node, "#{receiver.source}.push(#{remove_brackets(elements)})") | ||
end | ||
end | ||
end | ||
|
||
def on_csend(node) | ||
concat_call?(node) do |receiver, elements| | ||
add_offense(node) do |corrector| | ||
corrector.replace(node, "#{receiver.source}&.push(#{remove_brackets(elements)})") | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
# Copied from `Lint/RedundantSplatExpansion` | ||
# TODO: Expose this as a helper API from the base RuboCop gem | ||
PERCENT_W = '%w' | ||
PERCENT_CAPITAL_W = '%W' | ||
PERCENT_I = '%i' | ||
PERCENT_CAPITAL_I = '%I' | ||
|
||
def remove_brackets(array) | ||
array_start = array.loc.begin.source | ||
elements = *array | ||
elements = elements.map(&:source) | ||
|
||
if array_start.start_with?(PERCENT_W) | ||
"'#{elements.join("', '")}'" | ||
elsif array_start.start_with?(PERCENT_CAPITAL_W) | ||
%("#{elements.join('", "')}") | ||
elsif array_start.start_with?(PERCENT_I) | ||
":#{elements.join(', :')}" | ||
elsif array_start.start_with?(PERCENT_CAPITAL_I) | ||
%(:"#{elements.join('", :"')}") | ||
else | ||
elements.join(', ') | ||
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,41 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Performance::ArrayConcatLiteral, :config do | ||
it 'registers an offense and corrects when using `concat` with array literal' do | ||
expect_offense(<<~RUBY) | ||
array.concat([1, 2, 3]) | ||
^^^^^^^^^^^^^^^^^^^^^^^ Use `push` instead of concatinating a literal. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
array.push(1, 2, 3) | ||
RUBY | ||
end | ||
|
||
it 'registers an offense and corrects when using `concat` with array literal and safe navigation operator' do | ||
expect_offense(<<~RUBY) | ||
array&.concat([1, 2, 3]) | ||
^^^^^^^^^^^^^^^^^^^^^^^^ Use `push` instead of concatinating a literal. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
array&.push(1, 2, 3) | ||
RUBY | ||
end | ||
|
||
describe 'replacing `concat` with `push`' do | ||
it 'returns the same result' do | ||
expect([1, 2, 3].concat(4, 5, 6)).to eq([1, 2, 3].push(4, 5, 6)) | ||
end | ||
|
||
it 'has the same side-effect' do | ||
expected = [1, 2, 3] | ||
expected.push(4, 5, 6) | ||
|
||
actual = [1, 2, 3] | ||
actual.push(4, 5, 6) | ||
|
||
expect(actual).to eq(expected) | ||
end | ||
end | ||
end |