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
Add cop to enforce use of transform_keys
and transform_values
#7663
Add cop to enforce use of transform_keys
and transform_values
#7663
Conversation
See https://github.com/rubocop-hq/rubocop/pull/7646/files for an example of how to specify |
908dcbb
to
1e4b1d0
Compare
Added |
Nice idea! I've added a few small remarks and I'd suggest also adding a matching guideline in the community Ruby style guide. You'll also have to get the build to pass. :-) |
f4db82f
to
02e7906
Compare
Thanks for the feedback! I think I've responded to everything either by fixing or with a question. |
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 was just about to submit a very similar PR to the performance extension when I found this 😅
I left a few notes based on my own implementation. Feel free to plunder anything else valuable:
rubocop/rubocop-performance@master...eugeneius:hash_transformation
b2e2437
to
bff89b5
Compare
Btw, I've been thinking that probably we need to have 2 different cops (e.g. |
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.
@bbatsov I thought about that but decided not to bother since EOL for Ruby 2.4 is coming up in a couple of months--and I agree we could factor out a lot of common code between the two, but it still seems likely to end up a more complex implementation to me rather than simpler.
If you think it's important I can separate them, but now that the weekend is over I'm not sure when I'll get to it; if @eugeneius has time to iterate on their branch it might make sense to use that as the base and steal anything valuable from this (probably mostly the each_with_object
case?) instead of vice versa.
5965a45
to
09013f0
Compare
@bbatsov I did end up getting some time to work on this today, so I've pushed a split into two cops (and also fixed the autocorrect bug @eugeneius mentioned) |
Thanks! |
Thank you! |
This PR suppress the following warning. ```console % cd path/to/rubocop % bundle install Your Gemfile lists the gem pry (>= 0) more than once. You should probably keep only one of them. Remove any duplicate entries and specify the gem only once. While it's not a problem now, it could cause errors if you change the version of one of them later. Using rake 12.3.3 Using public_suffix 4.0.3 (snip) ``` `pry` gem is already in the Gemfile. This looks like it was accidentally added in rubocop#7663.
This PR suppress the following warning. ```console % cd path/to/rubocop % bundle install Your Gemfile lists the gem pry (>= 0) more than once. You should probably keep only one of them. Remove any duplicate entries and specify the gem only once. While it's not a problem now, it could cause errors if you change the version of one of them later. Using rake 12.3.3 Using public_suffix 4.0.3 (snip) ``` `pry` gem is already in the Gemfile. This looks like it was accidentally added in #7663.
end | ||
|
||
def handle_possible_offense(node, match, match_desc) | ||
puts node.class |
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.
Perhaps we should have a cop for catching puts
? 😄
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.
Wow! For example, it would be good to prepare a cop of InternalAffairs
department for lib/rubocop/cop directory. At this time, puts
method is not used in this directory.
This PR added `VersionAdded` to `Style/HashTransformKeys` and `Style/HashTransformValue`. This Cops was added at rubocop#7663 and published at 0.80.0.
Hey @eugeneius, I just noticed that your branch makes mention and adds a cop to use Will you keep working on yours? |
Add a
Style/HashTransformMethods
cop that enforces the use of the Ruby 2.5+Hash#transform_keys
andHash#transform_values
methods in place of patterns that are slower and less readable.ie, replace:
with:
Includes an autocorrect (which is not safe because we can't fully guarantee that the receiver is a Hash, rather than some other enumerable, but which empirically is fairly good in Stripe's codebase).
This cop should only be run on Ruby 2.5 or greater.