-
-
Notifications
You must be signed in to change notification settings - Fork 270
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
Conversation
f03f4e5
to
401297a
Compare
There was a problem hiding this 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.
(args | ||
(arg _)) |
There was a problem hiding this comment.
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? } |
There was a problem hiding this comment.
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?)
# This cop checks for places where custom logic on rejection blanks | ||
# from arrays and hashes can be replaced with `compact_blank`. |
There was a problem hiding this comment.
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
.
401297a
to
e001bc0
Compare
@pirj Thank you for your review! I've updated the PR overall. |
There was a problem hiding this 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?) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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! ```
e001bc0
to
c7f39c4
Compare
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]] ```
In rubocop#598 new `Rails/CompactBlank` cop was added, but there's no entry for it in CHANGELOG.
Fixes #562.
This cop checks for places where custom logic on rejection blanks from arrays and hashes can be replaced with
compact_blank
.Before submitting the PR make sure the following are checked:
[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).bundle exec rake default
. It executes all tests and runs RuboCop on its own code.{change_type}_{change_description}.md
if the new code introduces user-observable changes. See changelog entry format for details.and description in grammatically correct, complete sentences.