forked from rubocop/rubocop-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fix rubocop#78] Create HashEnum Cop
- Loading branch information
Showing
7 changed files
with
137 additions
and
0 deletions.
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,34 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Rails | ||
# This cop looks for enums written with array syntax | ||
# | ||
# @example | ||
# # bad | ||
# enum status: %i(active archived) | ||
# | ||
# # bad | ||
# enum status: [:active, :archived] | ||
# | ||
# # good | ||
# enum status: { active: 0, archived: 1 } | ||
# | ||
class HashEnum < 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, _args| | ||
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::HashEnum 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 |