Skip to content

Commit

Permalink
FIX rubocop#159 - enum hash auto correct for % arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
ngouy committed Dec 3, 2019
1 parent 1ab92f2 commit f830f29
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
13 changes: 12 additions & 1 deletion lib/rubocop/cop/rails/enum_hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def on_send(node)

def autocorrect(node)
hash = node.children.each_with_index.map do |elem, index|
"#{elem.source} => #{index}"
"#{source(elem)} => #{index}"
end.join(', ')

->(corrector) { corrector.replace(node.loc.expression, "{#{hash}}") }
Expand All @@ -58,6 +58,17 @@ def enum_name(key)
key.source
end
end

def source(elem)
case elem.type
when :str
"\"#{elem.value}\""
when :sym
elem.source.include?(":") ? elem.source : ":#{elem.value}"
else
elem.source
end
end
end
end
end
Expand Down
31 changes: 27 additions & 4 deletions spec/rubocop/cop/rails/enum_hash_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,54 @@

context 'when array syntax is used' do
context 'with %i[] syntax' do
it 'registers an offense' do
it 'autocorrects' do
expect_offense(<<~RUBY)
enum status: %i[active archived]
^^^^^^^^^^^^^^^^^^^ Enum defined as an array found in `status` enum declaration. Use hash syntax instead.
RUBY

expect_correction(<<~RUBY)
enum status: {:active => 0, :archived => 1}
RUBY
end
end

context 'with %w[] syntax' do
it 'registers an offense' do
it 'autocorrects' do
expect_offense(<<~RUBY)
enum status: %w[active archived]
^^^^^^^^^^^^^^^^^^^ Enum defined as an array found in `status` enum declaration. Use hash syntax instead.
RUBY

expect_correction(<<~RUBY)
enum status: {"active" => 0, "archived" => 1}
RUBY
end
end

context 'with %i() syntax' do
it 'registers an offense' do
it 'autocorrects' do
expect_offense(<<~RUBY)
enum status: %i(active archived)
^^^^^^^^^^^^^^^^^^^ Enum defined as an array found in `status` enum declaration. Use hash syntax instead.
RUBY

expect_correction(<<~RUBY)
enum status: {:active => 0, :archived => 1}
RUBY
end
end

context 'with %w() syntax' do
it 'registers an offense' do
it 'autocorrects' do
expect_offense(<<~RUBY)
enum status: %w(active archived)
^^^^^^^^^^^^^^^^^^^ Enum defined as an array found in `status` enum declaration. Use hash syntax instead.
RUBY

expect_correction(<<~RUBY)
enum status: {"active" => 0, "archived" => 1}
RUBY
end
end

Expand All @@ -49,6 +65,13 @@
^^^^^^^^^^^^^^^^^^^^ Enum defined as an array found in `status` enum declaration. Use hash syntax instead.
RUBY
end

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

context 'when the enum name is a string' do
Expand Down

0 comments on commit f830f29

Please sign in to comment.