rubocop-rails icon indicating copy to clipboard operation
rubocop-rails copied to clipboard

Add Rails/EnumKeywordArgs cop

Open maxprokopiev opened this issue 1 year ago • 5 comments

This PR adds a new cop called Rails/EnumKeywordArgs.

This cop checks for the use of deprecated keyword arguments in enums. See more in https://github.com/rubocop/rubocop-rails/issues/1238

~Style guide PR: https://github.com/rubocop/rails-style-guide/pull/356~


Before submitting the PR make sure the following are checked:

  • [x] The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • [x] Wrote good commit messages.
  • [x] Commit message starts with [Fix #issue-number] (if the related issue exists).
  • [x] Feature branch is up-to-date with master (if not - rebase it).
  • [x] Squashed related commits together.
  • [x] Added tests.
  • [x] Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • [x] Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.
  • [ ] If this is a new cop, consider making a corresponding update to the Rails Style Guide.

maxprokopiev avatar Feb 16 '24 10:02 maxprokopiev

see https://github.com/rubocop/rails-style-guide/pull/356#discussion_r1492355019

maxprokopiev avatar Feb 16 '24 12:02 maxprokopiev

see rubocop/rails-style-guide#356 (comment)

A linter would still be helpful if it helps people autocorrect so they can migrate to the new API automatically.

ghiculescu avatar Feb 19 '24 07:02 ghiculescu

reopening this one since there seem to be some demand to have a linter, but not an entry to the style guide

maxprokopiev avatar Feb 28 '24 11:02 maxprokopiev

Rails 7.2 is about to be released (there is a stable branch already https://github.com/rails/rails/tree/7-2-stable).

@koic Can you please consider this PR?

fatkodima avatar May 16 '24 19:05 fatkodima

@maxprokopiev Hi, btw i've used your branch to upgrade a legacy rails app that uses this old format of keyword args. your implementation detected pretty much all my cases except these 2 cases :

`-

SOME_HASH_CONSTANT = { 0: 'active', 1: 'inactive }

enum my_enum: SOME_HASH_CONSTANT

==> the cop does not detect this at all. But since I have deprecations raise in development, i was able to eager load ( by calling bin/rails zeitwerk:check and find these edge cases.

2- Second point i noticed, the autocorrection is not available for _suffix and _prefix

# Here the hash is correct, your cop detects the use of "undierscore
# but the autocorrection is not available to change `_suffix` to `suffix
enum :my_enum,  { 0: 'active', 1: 'inactive }, _suffix: true

for my case, i think it was : enum :my_enum, SOME_HASH_CONSTANT, _suffix: true

chaadow avatar May 27 '24 12:05 chaadow

Now that Rails 7.2.0 has been released, I hope this pull request moves forward for a smooth upgrade. 👍

https://rubyonrails.org/2024/8/10/Rails-7-2-0-has-been-released

ohbarye avatar Aug 10 '24 01:08 ohbarye

@maxprokopiev ping.

koic avatar Aug 19 '24 08:08 koic

I opened #1336 based on your commit to include it in the upcoming new version planned for release soon. Thank you.

koic avatar Aug 22 '24 07:08 koic