-
-
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
CompactBlank cop raising an offense when using delete_if #650
Comments
Thank you for the feedback! And I'll take the issue to update the cop implementation and documentation. |
…nk)` Fixes rubocop#650. This PR makes `Rails/CompactBlank` aware of `delete_if(&:blank)`. It is unsafe because `compact_blank!` has different implementations for `Array`, `Hash`, and `ActionController::Parameters`. `Array#compact_blank!`, `Hash#compact_blank!` are equivalent to `delete_if(&:blank?)`. `ActionController::Parameters#compact_blank!` is equivalent to `reject!(&:blank?)`. If the cop makes a mistake, auto-corrected code may get unexpected behavior. This PR also updates unsafe document about it.
Oh, thanks for this explanation, I had no idea that's the difference in behavior. Great work with dealing the problem so fast 👍 |
…eject_delete_if [Fix #650] Make `Rails/CompactBlank` aware of `delete_if(&:blank)`
Currently there is an existing cop that tells us to use
compact_blank
,compact_blank!
instead ofreject
,reject!
respectively when deleting blanks.There is another option to be used and that is
delete_if
. So instead of doing this:{ a: 'a', b: 'b', c: 'c', d: {}, e: {}, f: [] }.delete_if{ |_k, v| v.blank? }
The cop should recommend doing this:
{ a: 'a', b: 'b', c: 'c', d: {}, e: {}, f: [] }.compact_blank!
It works virtually the same as
reject!
but is not considered in the cop. I have run some local bechmarking to comparereject!
anddelete_if
to see if maybe one should be favored over the other:This is the end result:
As you can see the run time is almost the same ( even when repeating the proccess 10 times the numbers were very similar)
And so because of the results I recommend adding the situation where using
delete_if
should raise an offense as well.If need be I could work on the cop myself
This is the output of rubocop -V:
The text was updated successfully, but these errors were encountered: