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 #8617] Style/HashAsLastArrayItem no longer flags hashes if all elements in an array are hashes #8635

Merged
merged 1 commit into from Sep 7, 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 @@ -10,6 +10,7 @@
### Changes

* [#8470](https://github.com/rubocop-hq/rubocop/issues/8470): Do not autocorrect `Style/StringConcatenation` when parts of the expression are too complex. ([@dvandersluis][])
* [#8617](https://github.com/rubocop-hq/rubocop/issues/8617): Fix `Style/HashAsLastArrayItem` to not register an offense when all items in an array are hashes. ([@dvandersluis][])
dvandersluis marked this conversation as resolved.
Show resolved Hide resolved

## 0.90.0 (2020-09-01)

Expand Down
9 changes: 9 additions & 0 deletions docs/modules/ROOT/pages/cops_style.adoc
Expand Up @@ -3691,6 +3691,9 @@ ok
Checks for presence or absence of braces around hash literal as a last
array item depending on configuration.

NOTE: This cop will ignore arrays where all items are hashes, regardless of
EnforcedStyle.

=== Examples

==== EnforcedStyle: braces (default)
Expand All @@ -3702,6 +3705,9 @@ array item depending on configuration.

# good
[1, 2, { one: 1, two: 2 }]

# good
[{ one: 1 }, { two: 2 }]
----

==== EnforcedStyle: no_braces
Expand All @@ -3713,6 +3719,9 @@ array item depending on configuration.

# good
[1, 2, one: 1, two: 2]

# good
[{ one: 1 }, { two: 2 }]
----

=== Configurable attributes
Expand Down
13 changes: 11 additions & 2 deletions lib/rubocop/cop/style/hash_as_last_array_item.rb
Expand Up @@ -6,20 +6,28 @@ module Style
# Checks for presence or absence of braces around hash literal as a last
# array item depending on configuration.
#
# NOTE: This cop will ignore arrays where all items are hashes, regardless of
# EnforcedStyle.
#
# @example EnforcedStyle: braces (default)
# # bad
# [1, 2, one: 1, two: 2]
#
# # good
# [1, 2, { one: 1, two: 2 }]
#
# # good
# [{ one: 1 }, { two: 2 }]
#
# @example EnforcedStyle: no_braces
# # bad
# [1, 2, { one: 1, two: 2 }]
#
# # good
# [1, 2, one: 1, two: 2]
#
# # good
# [{ one: 1 }, { two: 2 }]
class HashAsLastArrayItem < Base
include ConfigurableEnforcedStyle
extend AutoCorrector
Expand All @@ -38,9 +46,10 @@ def on_hash(node)

def last_array_item?(node)
parent = node.parent
return false unless parent
return false unless parent&.array_type?
return false if parent.child_nodes.all?(&:hash_type?)

parent.array_type? && parent.children.last.equal?(node)
parent.children.last.equal?(node)
end

def check_braces(node)
Expand Down
12 changes: 12 additions & 0 deletions spec/rubocop/cop/style/hash_as_last_array_item_spec.rb
Expand Up @@ -30,6 +30,12 @@
foo(one: 1, two: 2)
RUBY
end

it 'does not register an offense when the array is all hashes' do
expect_no_offenses(<<~RUBY)
[{ one: 1 }, { two: 2 }]
RUBY
end
end

context 'when EnforcedStyle is no_braces' do
Expand Down Expand Up @@ -59,5 +65,11 @@
foo({ one: 1, two: 2 })
RUBY
end

it 'does not register an offense when the array is all hashes' do
expect_no_offenses(<<~RUBY)
[{ one: 1 }, { two: 2 }]
RUBY
end
end
end