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 a false positive for Style/HashAsLastArrayItem when hash is not a last array item #8333

Merged
merged 1 commit into from Jul 14, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,7 @@
### Bug fixes

* [#8324](https://github.com/rubocop-hq/rubocop/issues/8324): Fix crash for `Layout/SpaceAroundMethodCallOperator` when using `Proc#call` shorthand syntax. ([@fatkodima][])
* [#8323](https://github.com/rubocop-hq/rubocop/issues/8323): Fix a false positive for `Style/HashAsLastArrayItem` when hash is not a last array item. ([@fatkodima][])

## 0.88.0 (2020-07-13)

Expand Down
9 changes: 8 additions & 1 deletion lib/rubocop/cop/style/hash_as_last_array_item.rb
Expand Up @@ -25,7 +25,7 @@ class HashAsLastArrayItem < Base
extend AutoCorrector

def on_hash(node)
return unless node.parent&.array_type?
return unless last_array_item?(node)

if braces_style?
check_braces(node)
Expand All @@ -36,6 +36,13 @@ def on_hash(node)

private

def last_array_item?(node)
parent = node.parent
return false unless parent

parent.array_type? && parent.values.last == node
end

def check_braces(node)
return if node.braces?

Expand Down
6 changes: 3 additions & 3 deletions spec/rubocop/cop/style/hash_as_last_array_item_spec.rb
Expand Up @@ -39,12 +39,12 @@

it 'registers an offense and corrects when hash with braces' do
expect_offense(<<~RUBY)
[1, 2, { one: 1, two: 2 }]
^^^^^^^^^^^^^^^^^^ Omit the braces around the hash.
[{ one: 1 }, 2, { three: 3 }]
^^^^^^^^^^^^ Omit the braces around the hash.
RUBY

expect_correction(<<~RUBY)
[1, 2, one: 1, two: 2 ]
[{ one: 1 }, 2, three: 3 ]
RUBY
end

Expand Down