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

[#78] Autocorrect EnumHash Cop #95

Merged
merged 2 commits into from Jul 29, 2019
Merged

Conversation

santib
Copy link
Contributor

@santib santib commented Jul 24, 2019

Resolves #78.

Adds autocorrect feature for #78.
Depends on #94.

@santib santib mentioned this pull request Jul 24, 2019
@santib santib changed the title [#78] Autocorrect HashEnum Cop [#78] Autocorrect EnumHash Cop Jul 25, 2019
@koic
Copy link
Member

koic commented Jul 25, 2019

#94 has been merged. Can you rebase this PR with latest master branch? After rebasing I will review this PR.

@santib
Copy link
Contributor Author

santib commented Jul 25, 2019

#94 has been merged. Can you rebase this PR with latest master branch? After rebasing I will review this PR.

Thanks! I just pushed the branch after rebasing.

@santib santib force-pushed the autocorrect-hashenum-cop branch 2 times, most recently from 9dc926b to c5141f1 Compare July 25, 2019 12:42
value = child.children.first
case value
when String
"'#{value}' => #{index}"
Copy link
Contributor

Choose a reason for hiding this comment

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

Use String#dup instead of "'#{value}'".
https://docs.ruby-lang.org/en/2.3.0/String.html#method-i-dump

eval(any_string.dump) always eauqlas any_string.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did it this way to prevent escaped keys, for example:
got: "enum status: {:old=>0, :\"very active\"=>1, \"is archived\"=>2, 42=>3}\n"

if that's not a problem would it be fine to just build a real hash and stringify it? It'd solve the problem being discussed here #95 (comment)

when String
"'#{value}' => #{index}"
when Symbol
value = "'#{value}'" if value =~ /\s/
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd like to use a stricter way to quote the symbol. Comparing /\s/ is not match :'foo-bar' and so on.

@koic Do you have any idea to make it strict? I guess we have a method to detect the necessity of quoting.

Copy link
Member

Choose a reason for hiding this comment

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

@pocke I agree with it is better to be stricter, but unfortunately I have not got a good idea yet...

Copy link
Contributor

Choose a reason for hiding this comment

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

https://github.com/rubocop-hq/rubocop/blob/971ac419d76e62d394a7abd9e941335574cb4dd3/lib/rubocop/cop/style/hash_syntax.rb#L140-L155

I found a check that foo: style is acceptable.
But it is not portable. We need to extend the method to a node extension or mixin if we'd like to use it for the cop.

So I think the current implementation is acceptable for now. I think it works on 99% cases because enum key name does not have special symbols in most cases.

@koic
Copy link
Member

koic commented Jul 26, 2019

@santib The master branch has been updated by #96. Please update this PR with latest master again.

@santib
Copy link
Contributor Author

santib commented Jul 26, 2019

@koic Perfect, will fix it later today

@santib
Copy link
Contributor Author

santib commented Jul 26, 2019

@koic @pocke Just rebased and built a new version of the autocorrect. I did a separate commit just for now but I'll squash once everything looks good.

I'm not sure if it's acceptable to do it this way because in other cops I saw mostly changes to strings rather than stringifying objects such as I just did. If it's not acceptable, just let me know and I'll work on another version.

I assumed that other cops (such as prefer new hash syntax) will autocorrect the hash built in this solution, is that correct?

@koic
Copy link
Member

koic commented Jul 27, 2019

I assumed that other cops (such as prefer new hash syntax) will autocorrect the hash built in this solution, is that correct?

I think it is good as a minimal implementation because this approach has achieved its purpose to prevent unintended reordering of database values.

@pocke Do you have any thoughts?

@koic
Copy link
Member

koic commented Jul 28, 2019

If there is no problem, it will merge soon.

@pocke
Copy link
Contributor

pocke commented Jul 28, 2019

I agree with @koic . The implementation looks good.

@koic koic merged commit aedb97c into rubocop:master Jul 29, 2019
@koic
Copy link
Member

koic commented Jul 29, 2019

Thanks!

@santib santib deleted the autocorrect-hashenum-cop branch July 29, 2019 12:16
@mlarraz
Copy link

mlarraz commented Aug 21, 2019

There seems to be an issue with autocorrection of nested constants: #114

Any ideas on the right way to approach fixing this?

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.

New Cop: Explicit enum definition
4 participants