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 Style/HashAsLastArrayItem with no_braces for empty hash #8681

Merged
merged 2 commits into from Sep 15, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Expand Up @@ -26,6 +26,7 @@
* [#8664](https://github.com/rubocop-hq/rubocop/issues/8664): Fix a false positive for `Naming/BinaryOperatorParameterName` when naming multibyte character method name. ([@koic][])
* [#8604](https://github.com/rubocop-hq/rubocop/issues/8604): Fix a false positive for `Bundler/DuplicatedGem` when gem is duplciated in condition. ([@tejasbubane][])
* [#8671](https://github.com/rubocop-hq/rubocop/issues/8671): Fix an error for `Style/ExplicitBlockArgument` when using safe navigation method call. ([@koic][])
* [#8681](https://github.com/rubocop-hq/rubocop/pull/8681): Fix an error for `Style/HashAsLastArrayItem` with `no_braces` for empty hash. ([@fsateler][])
* [#8682](https://github.com/rubocop-hq/rubocop/pull/8682): Fix a positive for `Style/HashTransformKeys` and `Style/HashTransformValues` when the `each_with_object` hash is used in the transformed key or value. ([@eugeneius][])
* [#8688](https://github.com/rubocop-hq/rubocop/issues/8688): Mark `Style/GlobalStdStream` as unsafe autocorrection. ([@marcandre][])
* [#8642](https://github.com/rubocop-hq/rubocop/issues/8642): Fix a false negative for `Style/SpaceInsideHashLiteralBraces` when a correct empty hash precedes the incorrect hash. ([@dvandersluis][])
Expand Down Expand Up @@ -4871,4 +4872,5 @@
[@jaimerave]: https://github.com/jaimerave
[@Skipants]: https://github.com/Skipants
[@sascha-wolf]: https://github.com/sascha-wolf
[@iSarCasm]: https://github.com/iSarCasm
[@fsateler]: https://github.com/fsateler
[@iSarCasm]: https://github.com/iSarCasm
1 change: 1 addition & 0 deletions lib/rubocop/cop/style/hash_as_last_array_item.rb
Expand Up @@ -62,6 +62,7 @@ def check_braces(node)

def check_no_braces(node)
return unless node.braces?
return if node.children.empty? # Empty hash cannot be "unbraced"

add_offense(node, message: 'Omit the braces around the hash.') do |corrector|
corrector.remove(node.loc.begin)
Expand Down
12 changes: 12 additions & 0 deletions spec/rubocop/cop/style/hash_as_last_array_item_spec.rb
Expand Up @@ -36,6 +36,12 @@
[{ one: 1 }, { two: 2 }]
RUBY
end

it 'does not register an offense when the hash is empty' do
expect_no_offenses(<<~RUBY)
[1, {}]
RUBY
end
end

context 'when EnforcedStyle is no_braces' do
Expand Down Expand Up @@ -71,5 +77,11 @@
[{ one: 1 }, { two: 2 }]
RUBY
end

it 'does not register an offense when the hash is empty' do
expect_no_offenses(<<~RUBY)
[1, {}]
RUBY
end
end
end