Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX #159 enum hash auto correct for % arrays #163

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## master (unreleased)

### Bug fixes

* [#159](https://github.com/rubocop-hq/rubocop-rails/issues/159): Fix autocorrect for `Rails/EnumHash` when using % arrays notations. ([@ngouy][])

## 2.4.0 (2019-11-27)

### New features
Expand Down Expand Up @@ -108,3 +112,4 @@
[@pocke]: https://github.com/pocke
[@gyfis]: https:/github.com/gyfis
[@DNA]: https://github.com/DNA
[@ngouy]: https://github.com/ngouy
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.dump
when :sym
elem.value.inspect
else
elem.source
end
end
end
end
end
Expand Down
44 changes: 44 additions & 0 deletions spec/rubocop/cop/rails/enum_hash_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,50 @@
enum status: {STATUS::OLD => 0, STATUS::ACTIVE => 1}
RUBY
end

it 'autocorrects %w[] syntax' 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

it 'autocorrect %w() syntax' 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

it 'autocorrect %i[] syntax' 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

it 'autocorrect %i() syntax' 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 'when hash syntax is used' do
Expand Down