-
-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #94 from santib/add-hashenum-cop
[#78] Create EnumHash Cop
- Loading branch information
Showing
7 changed files
with
147 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Rails | ||
# This cop looks for enums written with array syntax. | ||
# | ||
# When using array syntax, adding an element in a | ||
# position other than the last causes all previous | ||
# definitions to shift. Explicitly specifying the | ||
# value for each key prevents this from happening. | ||
# | ||
# @example | ||
# # bad | ||
# enum status: [:active, :archived] | ||
# | ||
# # good | ||
# enum status: { active: 0, archived: 1 } | ||
# | ||
class EnumHash < Cop | ||
MSG = 'Enum defined as an array found in `%<enum>s` enum declaration. '\ | ||
'Use hash syntax instead.' | ||
|
||
def_node_matcher :enum_with_array?, <<~PATTERN | ||
(send nil? :enum (hash (pair (_ $_) array))) | ||
PATTERN | ||
|
||
def on_send(node) | ||
enum_with_array?(node) do |name| | ||
add_offense(node, message: format(MSG, enum: name)) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Rails::EnumHash do | ||
subject(:cop) { described_class.new(config) } | ||
|
||
let(:config) { RuboCop::Config.new } | ||
|
||
context 'when array syntax is used' do | ||
context 'with %i[] syntax' do | ||
it 'registers an offense' do | ||
expect_offense(<<~RUBY) | ||
enum status: %i[active archived] | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Enum defined as an array found in `status` enum declaration. Use hash syntax instead. | ||
RUBY | ||
end | ||
end | ||
|
||
context 'with %w[] syntax' do | ||
it 'registers an offense' do | ||
expect_offense(<<~RUBY) | ||
enum status: %w[active archived] | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Enum defined as an array found in `status` enum declaration. Use hash syntax instead. | ||
RUBY | ||
end | ||
end | ||
|
||
context 'with %i() syntax' do | ||
it 'registers an offense' do | ||
expect_offense(<<~RUBY) | ||
enum status: %i(active archived) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Enum defined as an array found in `status` enum declaration. Use hash syntax instead. | ||
RUBY | ||
end | ||
end | ||
|
||
context 'with %w() syntax' do | ||
it 'registers an offense' do | ||
expect_offense(<<~RUBY) | ||
enum status: %w(active archived) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Enum defined as an array found in `status` enum declaration. Use hash syntax instead. | ||
RUBY | ||
end | ||
end | ||
|
||
context 'with [] syntax' do | ||
it 'registers an offense' do | ||
expect_offense(<<~RUBY) | ||
enum status: [:active, :archived] | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Enum defined as an array found in `status` enum declaration. Use hash syntax instead. | ||
RUBY | ||
end | ||
end | ||
end | ||
|
||
context 'when hash syntax is used' do | ||
it 'does not register an offense' do | ||
expect_no_offenses('enum status: { active: 0, archived: 1 }') | ||
end | ||
end | ||
end |