Skip to content

Commit

Permalink
[Fix #3918] Prevent Rails/EnumUniqueness from breaking on a non-lit…
Browse files Browse the repository at this point in the history
…eral hash value

This cop would break on any enum hash that did not have a literal value.
This change fixes that.
  • Loading branch information
Drenmi authored and bbatsov committed Jan 16, 2017
1 parent 88a3a9b commit de4e4c5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,7 @@

* [#3911](https://github.com/bbatsov/rubocop/issues/3911): Prevent a crash in `Performance/RegexpMatch` cop with module definition. ([@pocke][])
* [#3908](https://github.com/bbatsov/rubocop/issues/3908): Prevent `Style/AlignHash` from breaking on a keyword splat when using enforced `table` style. ([@drenmi][])
* [#3918](https://github.com/bbatsov/rubocop/issues/3918): Prevent `Rails/EnumUniqueness` from breaking on a non-literal hash value. ([@drenmi][])

## 0.47.0 (2017-01-16)

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rails/enum_uniqueness.rb
Expand Up @@ -23,7 +23,7 @@ class EnumUniqueness < Cop
MSG = 'Duplicate value `%s` found in `%s` enum declaration.'.freeze

def_node_matcher :enum_declaration, <<-END
(send nil :enum (hash (pair (_ $_) $_)))
(send nil :enum (hash (pair (_ $_) ${array hash})))
END

def on_send(node)
Expand Down
28 changes: 27 additions & 1 deletion spec/rubocop/cop/rails/enum_uniqueness_spec.rb
Expand Up @@ -64,11 +64,37 @@
end

context 'when receiving a variable' do
it 'does not register an offence' do
it 'does not register an offense' do
inspect_source(cop, ['var = { status: { active: 0, archived: 1 } }',
'enum var'])

expect(cop.offenses).to be_empty
end
end

context 'when receiving a hash without literal values' do
context 'when value is a variable' do
it 'does not register an offense' do
inspect_source(cop, 'enum status: statuses')

expect(cop.offenses).to be_empty
end
end

context 'when value is a method chain' do
it 'does not register an offense' do
inspect_source(cop, 'enum status: User.statuses.keys')

expect(cop.offenses).to be_empty
end
end

context 'when value is a constant' do
it 'does not register an offense' do
inspect_source(cop, 'enum status: STATUSES')

expect(cop.offenses).to be_empty
end
end
end
end

0 comments on commit de4e4c5

Please sign in to comment.