Skip to content

Commit 4ec7f5a

Browse files
committed
Fix false negatives for Rails/EnumUniqueness cop
1 parent 20de97e commit 4ec7f5a

File tree

3 files changed

+58
-8
lines changed

3 files changed

+58
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
### Bug fixes
1010

1111
* [#53](https://github.com/rubocop-hq/rubocop-rails/issues/53): Fix a false positive for `Rails/SaveBang` when implicitly return using finder method and creation method connected by `||`. ([@koic][])
12+
* [#97](https://github.com/rubocop-hq/rubocop-rails/pull/97): Fix two false negatives for `Rails/EnumUniqueness`. 1. When `enum` name is not a literal. 2. When `enum` has multiple definitions. ([@santib][])
1213

1314
### Changes
1415

lib/rubocop/cop/rails/enum_uniqueness.rb

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,42 @@ class EnumUniqueness < Cop
2323
MSG = 'Duplicate value `%<value>s` found in `%<enum>s` ' \
2424
'enum declaration.'
2525

26-
def_node_matcher :enum_declaration, <<-PATTERN
27-
(send nil? :enum (hash (pair (_ $_) ${array hash})))
26+
def_node_matcher :enum?, <<~PATTERN
27+
(send nil? :enum (hash $...))
28+
PATTERN
29+
30+
def_node_matcher :enum_values, <<~PATTERN
31+
(pair $_ ${array hash})
2832
PATTERN
2933

3034
def on_send(node)
31-
enum_declaration(node) do |name, args|
32-
items = args.values
35+
enum?(node) do |pairs|
36+
pairs.each do |pair|
37+
enum_values(pair) do |key, args|
38+
items = args.values
3339

34-
return unless duplicates?(items)
40+
next unless duplicates?(items)
3541

36-
consecutive_duplicates(items).each do |item|
37-
add_offense(item, message: format(MSG, value: item.source,
38-
enum: name))
42+
consecutive_duplicates(items).each do |item|
43+
add_offense(item, message: format(
44+
MSG, value: item.source, enum: enum_name(key)
45+
))
46+
end
47+
end
3948
end
4049
end
4150
end
51+
52+
private
53+
54+
def enum_name(key)
55+
case key.type
56+
when :sym, :str
57+
key.value
58+
else
59+
key.source
60+
end
61+
end
4262
end
4363
end
4464
end

spec/rubocop/cop/rails/enum_uniqueness_spec.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,33 @@
8585
end
8686
end
8787
end
88+
89+
context 'when the enum name is a string' do
90+
it 'registers an offense' do
91+
expect_offense(<<~RUBY)
92+
enum "status" => [:active, :archived, :active]
93+
^^^^^^^ Duplicate value `:active` found in `status` enum declaration.
94+
RUBY
95+
end
96+
end
97+
98+
context 'when the enum name is not a literal' do
99+
it 'registers an offense' do
100+
expect_offense(<<~RUBY)
101+
enum KEY => [:active, :archived, :active]
102+
^^^^^^^ Duplicate value `:active` found in `KEY` enum declaration.
103+
RUBY
104+
end
105+
end
106+
107+
context 'with multiple enum definition for a `enum` method call' do
108+
it 'registers an offense' do
109+
expect_offense(<<~RUBY)
110+
enum status: [:active, :archived, :active],
111+
^^^^^^^ Duplicate value `:active` found in `status` enum declaration.
112+
role: [:owner, :member, :guest, :member]
113+
^^^^^^^ Duplicate value `:member` found in `role` enum declaration.
114+
RUBY
115+
end
116+
end
88117
end

0 commit comments

Comments
 (0)