Skip to content

Commit b2b3724

Browse files
committed
Support Rails 7 syntax for Rails/EnumUniqueness cop
1 parent bb1d373 commit b2b3724

File tree

3 files changed

+255
-78
lines changed

3 files changed

+255
-78
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#1298](https://github.com/rubocop/rubocop-rails/pull/1298): Support Rails 7 syntax for `Rails/EnumUniqueness` cop. ([@ytjmt][])

lib/rubocop/cop/rails/enum_uniqueness.rb

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@ module Rails
77
#
88
# @example
99
# # bad
10+
# enum :status, { active: 0, archived: 0 }
11+
#
12+
# # good
13+
# enum :status, { active: 0, archived: 1 }
14+
#
15+
# # bad
16+
# enum :status, [:active, :archived, :active]
17+
#
18+
# # good
19+
# enum :status, [:active, :archived]
20+
#
21+
# # bad
1022
# enum status: { active: 0, archived: 0 }
1123
#
1224
# # good
@@ -24,6 +36,10 @@ class EnumUniqueness < Base
2436
RESTRICT_ON_SEND = %i[enum].freeze
2537

2638
def_node_matcher :enum?, <<~PATTERN
39+
(send nil? :enum $_ ${array hash} ...)
40+
PATTERN
41+
42+
def_node_matcher :enum_with_old_syntax?, <<~PATTERN
2743
(send nil? :enum (hash $...))
2844
PATTERN
2945

@@ -32,15 +48,17 @@ class EnumUniqueness < Base
3248
PATTERN
3349

3450
def on_send(node)
35-
enum?(node) do |pairs|
51+
enum?(node) do |key, args|
52+
consecutive_duplicates(args.values).each do |item|
53+
add_offense(item, message: message(key, item))
54+
end
55+
end
56+
57+
enum_with_old_syntax?(node) do |pairs|
3658
pairs.each do |pair|
3759
enum_values(pair) do |key, args|
38-
items = args.values
39-
40-
next unless duplicates?(items)
41-
42-
consecutive_duplicates(items).each do |item|
43-
add_offense(item, message: format(MSG, value: item.source, enum: enum_name(key)))
60+
consecutive_duplicates(args.values).each do |item|
61+
add_offense(item, message: message(key, item))
4462
end
4563
end
4664
end
@@ -57,6 +75,10 @@ def enum_name(key)
5775
key.source
5876
end
5977
end
78+
79+
def message(key, item)
80+
format(MSG, value: item.source, enum: enum_name(key))
81+
end
6082
end
6183
end
6284
end

0 commit comments

Comments
 (0)