Skip to content

Commit

Permalink
[Fix rubocop#8617] Style/HashAsLastArrayItem no longer flags hashes i…
Browse files Browse the repository at this point in the history
…f all elements in an array are hashes.
  • Loading branch information
dvandersluis committed Sep 4, 2020
1 parent 8cc6391 commit 28b5ff4
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
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][])

## 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

0 comments on commit 28b5ff4

Please sign in to comment.