rubocop-rails
rubocop-rails copied to clipboard
Add Rails/EnumKeywordArgs cop
This PR adds a new cop called Rails/EnumKeywordArgs
.
This cop checks for the use of deprecated keyword arguments in enum
s. 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.
see https://github.com/rubocop/rails-style-guide/pull/356#discussion_r1492355019
A linter would still be helpful if it helps people autocorrect so they can migrate to the new API automatically.
reopening this one since there seem to be some demand to have a linter, but not an entry to the style guide
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?
@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
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
@maxprokopiev ping.
I opened #1336 based on your commit to include it in the upcoming new version planned for release soon. Thank you.