Cop Suggestion: Duplicate "enum" explicit value in Rails #3723

Closed
olliebennett opened this Issue Nov 15, 2016 · 2 comments

Comments

Projects
None yet
3 participants
@olliebennett
Contributor

olliebennett commented Nov 15, 2016

An enum in Rails can be defined either as an array or a hash with specific integer keys defined.

# array syntax
enum status: [ :active, :archived ]

# hash syntax
enum status: { active: 0, archived: 1 }

With the latter syntax, it's possible to accidentally specify duplicate keys:

enum status: { active: 4, archived: 4 }

Bugs resulting from this can be hard to identify, and I cannot see a valid use-case for defining duplicate keys in this way.

Is this a reasonable candidate for inclusion as a Rails-specific cop in RuboCop?

Note: Enums are available since Rails 4.1.0.

@Drenmi

This comment has been minimized.

Show comment
Hide comment
@Drenmi

Drenmi Nov 15, 2016

Collaborator

Makes perfect sense. It would probably also be useful to have another cop that enforces either array- or hash values for enums. 😀

Collaborator

Drenmi commented Nov 15, 2016

Makes perfect sense. It would probably also be useful to have another cop that enforces either array- or hash values for enums. 😀

@bbatsov

This comment has been minimized.

Show comment
Hide comment
@bbatsov

bbatsov Nov 15, 2016

Collaborator

I'm fine with the addition of such a cop as well.

Collaborator

bbatsov commented Nov 15, 2016

I'm fine with the addition of such a cop as well.

olliebennett added a commit to olliebennett/rubocop that referenced this issue Nov 16, 2016

olliebennett added a commit to olliebennett/rubocop that referenced this issue Nov 16, 2016

olliebennett added a commit to olliebennett/rubocop that referenced this issue Nov 16, 2016

[Fix #3723] Add new Rails/EnumUniqueness cop
This introduces a new `Rails/EnumUniqueness` cop which identifies any (presumably accidental) usage of duplicate `enum` value definitions.

```ruby
enum status: { active: 4, archived: 4 }
```

An [enum in Rails](http://edgeapi.rubyonrails.org/classes/ActiveRecord/Enum.html) can be defined either as an array or a hash with specific integer keys defined.

```ruby
enum status: [ :active, :archived ]

enum status: { active: 0, archived: 1 }
```

With the latter syntax, it's possible to accidentally specify duplicate keys:

```ruby
enum status: { active: 4, archived: 4 }
```

Bugs resulting from this can be hard to identify, and I cannot see a valid use-case for defining duplicate keys in this way.

olliebennett added a commit to olliebennett/rubocop that referenced this issue Nov 16, 2016

[Fix #3723] Add new Rails/EnumUniqueness cop
This introduces a new `Rails/EnumUniqueness` cop which identifies any (presumably accidental) usage of duplicate `enum` value definitions.

```ruby
enum status: { active: 4, archived: 4 }
```

An [enum in Rails](http://edgeapi.rubyonrails.org/classes/ActiveRecord/Enum.html) can be defined either as an array or a hash with specific integer keys defined.

```ruby
enum status: [ :active, :archived ]

enum status: { active: 0, archived: 1 }
```

With the latter syntax, it's possible to accidentally specify duplicate keys:

```ruby
enum status: { active: 4, archived: 4 }
```

Bugs resulting from this can be hard to identify, and I cannot see a valid use-case for defining duplicate keys in this way.

olliebennett added a commit to olliebennett/rubocop that referenced this issue Nov 16, 2016

[Fix #3723] Add new Rails/EnumUniqueness cop
This introduces a new cop which identifies any usage of duplicate values in hash-syntax enum definitions in Rails models.

For example, the following can lead to an ambiguous "status" for the model:
enum status: { active: 4, archived: 4 }

An enum in Rails can be defined either as an array or a hash with specific integer keys defined.
See: http://edgeapi.rubyonrails.org/classes/ActiveRecord/Enum.html

enum status: [ :active, :archived ]
enum status: { active: 0, archived: 1 }

With the latter syntax, it's possible to accidentally specify duplicate keys:

enum status: { active: 4, archived: 4 }

Bugs resulting from this can be hard to identify.

@olliebennett olliebennett referenced this issue Nov 16, 2016

Merged

[Fix #3723] Add new Rails/EnumUniqueness cop. #3724

10 of 11 tasks complete

olliebennett added a commit to olliebennett/rubocop that referenced this issue Nov 16, 2016

[Fix #3723] Add new Rails/EnumUniqueness cop
This introduces a new cop which identifies any usage of duplicate values in hash-syntax enum definitions in Rails models.

For example, the following can lead to an ambiguous "status" for the model:
enum status: { active: 4, archived: 4 }

An enum in Rails can be defined either as an array or a hash with specific integer keys defined.
See: http://edgeapi.rubyonrails.org/classes/ActiveRecord/Enum.html

enum status: [ :active, :archived ]
enum status: { active: 0, archived: 1 }

With the latter syntax, it's possible to accidentally specify duplicate keys:

enum status: { active: 4, archived: 4 }

Bugs resulting from this can be hard to identify.

olliebennett added a commit to olliebennett/rubocop that referenced this issue Nov 16, 2016

[Fix #3723] Add new Rails/EnumUniqueness cop
This introduces a new cop which identifies any usage of duplicate values in hash-syntax enum definitions in Rails models.

For example, the following can lead to an ambiguous "status" for the model:
enum status: { active: 4, archived: 4 }

An enum in Rails can be defined either as an array or a hash with specific integer keys defined.
See: http://edgeapi.rubyonrails.org/classes/ActiveRecord/Enum.html

enum status: [ :active, :archived ]
enum status: { active: 0, archived: 1 }

With the latter syntax, it's possible to accidentally specify duplicate keys:

enum status: { active: 4, archived: 4 }

Bugs resulting from this can be hard to identify.

olliebennett added a commit to olliebennett/rubocop that referenced this issue Nov 17, 2016

[Fix #3723] Add new Rails/EnumUniqueness cop
This introduces a new cop which identifies any usage of duplicate values in hash-syntax enum definitions in Rails models.

For example, the following can lead to an ambiguous "status" for the model:
enum status: { active: 4, archived: 4 }

An enum in Rails can be defined either as an array or a hash with specific integer keys defined.
See: http://edgeapi.rubyonrails.org/classes/ActiveRecord/Enum.html

enum status: [ :active, :archived ]
enum status: { active: 0, archived: 1 }

With the latter syntax, it's possible to accidentally specify duplicate keys:

enum status: { active: 4, archived: 4 }

Bugs resulting from this can be hard to identify.

olliebennett added a commit to olliebennett/rubocop that referenced this issue Nov 21, 2016

[Fix #3723] Add new Rails/EnumUniqueness cop
This introduces a new cop which identifies any usage of duplicate values in hash-syntax enum definitions in Rails models.

For example, the following can lead to an ambiguous "status" for the model:
enum status: { active: 4, archived: 4 }

An enum in Rails can be defined either as an array or a hash with specific integer keys defined.
See: http://edgeapi.rubyonrails.org/classes/ActiveRecord/Enum.html

enum status: [ :active, :archived ]
enum status: { active: 0, archived: 1 }

With the latter syntax, it's possible to accidentally specify duplicate keys:

enum status: { active: 4, archived: 4 }

Bugs resulting from this can be hard to identify.

@bbatsov bbatsov closed this in 0f14012 Nov 21, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment