Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #562] Add new Rails/CompactBlank cop #598

Merged
merged 1 commit into from
Dec 23, 2021

Conversation

koic
Copy link
Member

@koic koic commented Dec 20, 2021

Fixes #562.

This cop checks for places where custom logic on rejection blanks from arrays and hashes can be replaced with compact_blank.

# bad
collection.reject { |e| e.blank? }
collection.reject { |e| e.empty? }

# good
collection.compact_blank

# bad
collection.reject! { |e| e.blank? }
collection.reject! { |e| e.empty? }

# good
collection.compact_blank!

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.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.
  • If this is a new cop, consider making a corresponding update to the Rails Style Guide.

@koic koic force-pushed the add_new_rails_compact_blank_cop branch from f03f4e5 to 401297a Compare December 20, 2021 04:07
Copy link
Member

@pirj pirj left a comment

Choose a reason for hiding this comment

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

I'd throw in specs for detection for Hash, where this really gets ugly.
Now it fails to detect this case:

        collection.reject! { |_k, e| e.empty? }
       -           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `compact_blank!` instead.

Comment on lines 38 to 39
(args
(arg _))
Copy link
Member

Choose a reason for hiding this comment

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

Wondering how this works.
ruby-parse returns:

  (args
    (procarg0
      (arg :e)))

# @example
#
# # bad
# collection.reject { |e| e.blank? }
Copy link
Member

Choose a reason for hiding this comment

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

To me, this is not sufficiently bad. Especially with block-pass:

collection.reject(&:blank?)

However, with hashes this becomes unwieldy, and I don't think there's a better option than compact_blank:

hash_collection.reject { |_k, v| v.blank? }

Even though I'd rather prefer having something more generic like:

hash_collection.reject_values(&:blank?)

Comment on lines 6 to 7
# This cop checks for places where custom logic on rejection blanks
# from arrays and hashes can be replaced with `compact_blank`.
Copy link
Member

Choose a reason for hiding this comment

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

WDYT of

Checks if collection can be blank-compacted with compact_blank.

@koic koic force-pushed the add_new_rails_compact_blank_cop branch from 401297a to e001bc0 Compare December 21, 2021 06:22
@koic
Copy link
Member Author

koic commented Dec 21, 2021

@pirj Thank you for your review! I've updated the PR overall.

Copy link
Member

@pirj pirj left a comment

Choose a reason for hiding this comment

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

Thank you!

#
# # bad
# collection.reject!(&:blank?)
# collection.reject!(&:empty?)
Copy link
Member

Choose a reason for hiding this comment

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

I'd through in the example with a Hash, since it's outstandingly bad.

# bad
hashmap.reject { |_k, v| b.blank? }

# good
hashmap.compact_blank

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure! Thanks!

Fixes rubocop#562.

This cop checks for places where custom logic on rejection blanks
from arrays and hashes can be replaced with `compact_blank`.

```ruby
# bad
collection.reject { |e| e.blank? }
collection.reject { |e| e.empty? }

# good
collection.compact_blank

# bad
collection.reject! { |e| e.blank? }
collection.reject! { |e| e.empty? }

# good
collection.compact_blank!
```
@koic koic force-pushed the add_new_rails_compact_blank_cop branch from e001bc0 to c7f39c4 Compare December 21, 2021 10:19
@koic koic merged commit a80ac3b into rubocop:master Dec 23, 2021
@koic koic deleted the add_new_rails_compact_blank_cop branch December 23, 2021 16:21
koic added a commit that referenced this pull request Dec 24, 2021
Follow up to #598.

This PR marks `Rails/CompactBlank` as unsafe.

It is unsafe by default because false positives may occur in the
blank check of block arguments to the receiver object.

```ruby
[[1, 2], [3, nil]].reject { |first, second| second.blank? }
# => [[1, 2]]

[[1, 2], [3, nil]].compact_blank
# => [[1, 2], [3, nil]]
```
okuramasafumi added a commit to okuramasafumi/rubocop-rails that referenced this pull request Jan 5, 2022
In rubocop#598 new `Rails/CompactBlank` cop was added,
but there's no entry for it in CHANGELOG.
@okuramasafumi okuramasafumi mentioned this pull request Jan 5, 2022
10 tasks
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: Style/CompactBlank
2 participants