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

Support Rails 7 enum syntax for Rails/EnumUniqueness cop #1298

Merged

Conversation

ytjmt
Copy link
Contributor

@ytjmt ytjmt commented Jun 16, 2024

Rails 7.0 introduced a new enum syntax ( rails/rails#41328 ):

enum :status, [:active, :archived]

Rails/EnumUniqueness cop does't support the new syntax yet. This PR adds the new syntax support.


Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • 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.

@ytjmt ytjmt force-pushed the support-new-enum-syntax-for-enum-uniqueness-cop branch from 88df10a to b2b3724 Compare June 16, 2024 10:29
Comment on lines -38 to +61
items = args.values

next unless duplicates?(items)

consecutive_duplicates(items).each do |item|
add_offense(item, message: format(MSG, value: item.source, enum: enum_name(key)))
consecutive_duplicates(args.values).each do |item|
add_offense(item, message: message(key, item))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

refactor to suppress Metrics/AbcSize and Metrics/MethodLength offences.

Details
$ bundle exec rubocop lib/rubocop/cop/rails/enum_uniqueness.rb
Inspecting 1 file
C

Offenses:

lib/rubocop/cop/rails/enum_uniqueness.rb:50:9: C: Metrics/AbcSize: Assignment Branch Condition size for on_send is too high. [<9, 19, 4> 21.4/17]
        def on_send(node) ...
        ^^^^^^^^^^^^^^^^^
lib/rubocop/cop/rails/enum_uniqueness.rb:50:9: C: Metrics/MethodLength: Method has too many lines. [16/14]
        def on_send(node) ...
        ^^^^^^^^^^^^^^^^^

1 file inspected, 2 offenses detected


$ git diff lib/rubocop/cop/rails/enum_uniqueness.rb 
diff --git a/lib/rubocop/cop/rails/enum_uniqueness.rb b/lib/rubocop/cop/rails/enum_uniqueness.rb
index 2885315a..4b1e4230 100644
--- a/lib/rubocop/cop/rails/enum_uniqueness.rb
+++ b/lib/rubocop/cop/rails/enum_uniqueness.rb
@@ -56,9 +56,13 @@ module RuboCop
 
           enum_with_old_syntax?(node) do |pairs|
             pairs.each do |pair|
+              items = args.values
+
+              next unless duplicates?(items)
+
               enum_values(pair) do |key, args|
                 consecutive_duplicates(args.values).each do |item|
-                  add_offense(item, message: message(key, item))
+                  add_offense(item, message: format(MSG, value: item.source, enum: enum_name(key)))
                 end
               end
             end

@ytjmt ytjmt marked this pull request as ready for review June 16, 2024 10:38
@koic koic merged commit 4aa830b into rubocop:master Jul 5, 2024
14 checks passed
@koic
Copy link
Member

koic commented Jul 5, 2024

Thanks!

@ytjmt ytjmt deleted the support-new-enum-syntax-for-enum-uniqueness-cop branch July 6, 2024 07:11
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.

2 participants