-
-
Notifications
You must be signed in to change notification settings - Fork 263
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 Rails/EnumKeywordArgs cop #1239
Add Rails/EnumKeywordArgs cop #1239
Conversation
3437956
to
6d3d3f7
Compare
Defining enums with keyword arguments is deprecated and will be removed in Rails 7.3.
6d3d3f7
to
cfd67dc
Compare
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 2- Second point i noticed, the autocorrection is not available for # 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 : |
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 |
# # good | ||
# enum :status, { active: 0, archived: 1 }, prefix: true | ||
# | ||
class EnumKeywordArgs < Base |
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.
How about the name Rails/EnumSyntax
?
class EnumKeywordArgs < Base | |
class EnumSyntax < Base |
# Looks for enums written with keyword arguments. | ||
# | ||
# Defining enums with keyword arguments is deprecated | ||
# and will be removed in Rails 7.3. |
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.
# and will be removed in Rails 7.3. | |
# and will be removed in Rails 8.0. |
Enabled: pending | ||
VersionAdded: '<<next>>' |
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.
Can you add the Severity: warning
setting:
Enabled: pending | |
VersionAdded: '<<next>>' | |
Enabled: pending | |
Severity: warning | |
VersionAdded: '<<next>>' |
# enum :status, { active: 0, archived: 1 }, prefix: true | ||
# | ||
class EnumKeywordArgs < Base | ||
extend AutoCorrector |
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 think that it has been supported since Rails 5.0:
extend AutoCorrector | |
extend AutoCorrector | |
extend TargetRailsVersion | |
minimum_target_rails_version 5.0 |
@@ -423,6 +423,14 @@ Rails/EnumHash: | |||
Include: | |||
- app/models/**/*.rb | |||
|
|||
Rails/EnumKeywordArgs: | |||
Description: 'Use positional arguments over keyword arguments when defining enums.' | |||
StyleGuide: 'https://rails.rubystyle.guide#enums' |
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.
Can you remove it because it follows a different style guide rule?
StyleGuide: 'https://rails.rubystyle.guide#enums' |
@@ -0,0 +1 @@ | |||
* [#1239](https://github.com/rubocop/rubocop-rails/pull/1239): Add new `Rails/EnumKeywordArgs`. ([@maxprokopiev][]) |
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.
* [#1239](https://github.com/rubocop/rubocop-rails/pull/1239): Add new `Rails/EnumKeywordArgs`. ([@maxprokopiev][]) | |
* [#1238](https://github.com/rubocop/rubocop-rails/issues/1238): Add new `Rails/EnumSyntax`. ([@maxprokopiev][]) |
@maxprokopiev ping. |
I opened #1336 based on your commit to include it in the upcoming new version planned for release soon. Thank you. |
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 #1238Style guide PR: rubocop/rails-style-guide#356Before submitting the PR make sure the following are checked:
[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).bundle exec rake default
. It executes all tests and runs RuboCop on its own code.{change_type}_{change_description}.md
if the new code introduces user-observable changes. See changelog entry format for details.