Skip to content

Add new ArrayPushSingle cop to replace a.push(x) with a << x#433

Open
amomchilov wants to merge 1 commit into
rubocop:masterfrom
amomchilov:ArrayPushSingle
Open

Add new ArrayPushSingle cop to replace a.push(x) with a << x#433
amomchilov wants to merge 1 commit into
rubocop:masterfrom
amomchilov:ArrayPushSingle

Conversation

@amomchilov

@amomchilov amomchilov commented Jan 12, 2024

Copy link
Copy Markdown

Closes #431


Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.

Comment thread config/default.yml
Performance/ArrayPushSingle:
Description: 'Use `array << x` instead of `array.push(x)`.'
Reference: 'https://github.com/fastruby/fast-ruby#ancestorsinclude-vs--code'
Enabled: true

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it be true, false or pending?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new cop generator should output pending. On the next major release all pending cops are enabled in bulk according to the docs.

chars = regexp_content.chars.each_with_object([]) do |char, strings|
if stack.empty? && char == '\\'
stack.push(char)
stack.push(char) # rubocop:disable Performance/ArrayPushSingle

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keeping push is nicer here, to match pop below

^^^^^^^^^^^^^^^^^^^^ Use `<<` instead of `push`.
RUBY

# gross. TODO: make a configuration option?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the best way to handle this?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd just ignore it. &.<< looks horrible

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree.

@amomchilov amomchilov marked this pull request as ready for review January 12, 2024 02:05
message = format(MSG, current: method_name)

add_offense(node, message: message) do |corrector|
corrector.replace(node, "#{receiver.source} << #{element.source}")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there are some edge cases that this won't handle correctly e.g.:

# original
[].push(1 && 2)  # result is [2]
# corrected
[] << 1 && 2     # result is 2

# original
[].push 1 << 2   # result is [4]
# corrected
[] << 1 << 2     # result is [1,2]

# original
[].push(1) + [].push(2)  # result is [1,2]
# corrected
[] << 1 + [] << 2
TypeError: Array can't be coerced into Integer

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm does Rubocop have any utilities for detecting the precedence of a subexpr, relative to the other parts of the expression it's in?

it 'registers an offense and corrects when using `push` with a single element' do
expect_offense(<<~RUBY)
array.push(element)
^^^^^^^^^^^^^^^^^^^ Use `<<` instead of `push`.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should probably add a test for append as well

^^^^^^^^^^^^^^^^^^^^ Use `<<` instead of `push`.
RUBY

# gross. TODO: make a configuration option?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd just ignore it. &.<< looks horrible

@ydakuka

ydakuka commented Jan 12, 2024

Copy link
Copy Markdown
Contributor

Is this related to rubocop/rubocop#12053 ?

@amomchilov

Copy link
Copy Markdown
Author

@ydakuka I hadn't seen that, but I guess it's pretty similar!

I think two separate cops would be good here. One to turn a.push(x) into a << x, and another to take a series of a << xs (or pushes) and turn them into one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cop idea: prefer << over push

4 participants