-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
New cop for Hash#dig #5332
Comments
The method are not exactly equivalente with symbols as well: params = { "user": { name: "Nicolas Cage", married: false } }
=> {:user=>{:name=>"Nicolas Cage", :married=>false}}
params[:user] && params[:user][:married] && params[:user][:married][:date]
=> false
params.dig(:user, :married, :date)
=> TypeError: FalseClass does not have #dig method
from (irb):6:in `dig'
from (irb):6
from /usr/bin/irb.ruby2.4:11:in `<main>' I am not sure if this can be safely implemented. 🤔 |
I wrote a custom cop to do this: https://github.com/ezcater/ezcater_rubocop/blob/master/lib/rubocop/cop/ezcater/style_dig.rb We've found it useful, but it is definitely susceptible to false positives, e.g. ranges also have to be excluded. I wouldn't enable it by default for rubocop. |
@tjwp what about sending a PR to Rubocop? 😉
yes, me neither 😉 |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contribution and understanding! |
This issues been automatically closed due to lack of activity. Feel free to re-open it if you ever come back to it. |
Add a new cop that enforce the use of
instead of
Concerns
When using symbol as key I would say that the complain and the autocorrect are always safe, but not in the following cases:
Strings
Due to the
str[match_str]
method, things like this happens:Integers
The same for integers:
And that's the reason why I think it may be not safe that this cop check for
Array#dig
as well, as in this case numbers are always use and we get things like:For these cases we could add in the Ruby style guide that
dig
is preferred. I would say those cases are not the normal ones but still maybe not possible that a cop look for them.The text was updated successfully, but these errors were encountered: