From 28b5ff46dbe0afe45fdd0bcf00ae7637cf6ba370 Mon Sep 17 00:00:00 2001 From: Daniel Vandersluis Date: Wed, 2 Sep 2020 15:04:35 -0400 Subject: [PATCH] [Fix #8617] Style/HashAsLastArrayItem no longer flags hashes if all elements in an array are hashes. --- CHANGELOG.md | 1 + docs/modules/ROOT/pages/cops_style.adoc | 9 +++++++++ lib/rubocop/cop/style/hash_as_last_array_item.rb | 13 +++++++++++-- .../cop/style/hash_as_last_array_item_spec.rb | 12 ++++++++++++ 4 files changed, 33 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ebbfe72bf2d..304728e7f05 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/docs/modules/ROOT/pages/cops_style.adoc b/docs/modules/ROOT/pages/cops_style.adoc index d1a84bdd840..cdf8383a586 100644 --- a/docs/modules/ROOT/pages/cops_style.adoc +++ b/docs/modules/ROOT/pages/cops_style.adoc @@ -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) @@ -3702,6 +3705,9 @@ array item depending on configuration. # good [1, 2, { one: 1, two: 2 }] + +# good +[{ one: 1 }, { two: 2 }] ---- ==== EnforcedStyle: no_braces @@ -3713,6 +3719,9 @@ array item depending on configuration. # good [1, 2, one: 1, two: 2] + +# good +[{ one: 1 }, { two: 2 }] ---- === Configurable attributes diff --git a/lib/rubocop/cop/style/hash_as_last_array_item.rb b/lib/rubocop/cop/style/hash_as_last_array_item.rb index a761e46f5a4..8e253ae4056 100644 --- a/lib/rubocop/cop/style/hash_as_last_array_item.rb +++ b/lib/rubocop/cop/style/hash_as_last_array_item.rb @@ -6,6 +6,9 @@ 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] @@ -13,6 +16,9 @@ module Style # # good # [1, 2, { one: 1, two: 2 }] # + # # good + # [{ one: 1 }, { two: 2 }] + # # @example EnforcedStyle: no_braces # # bad # [1, 2, { one: 1, two: 2 }] @@ -20,6 +26,8 @@ module Style # # good # [1, 2, one: 1, two: 2] # + # # good + # [{ one: 1 }, { two: 2 }] class HashAsLastArrayItem < Base include ConfigurableEnforcedStyle extend AutoCorrector @@ -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) diff --git a/spec/rubocop/cop/style/hash_as_last_array_item_spec.rb b/spec/rubocop/cop/style/hash_as_last_array_item_spec.rb index 08e7192f0a8..16914a149f8 100644 --- a/spec/rubocop/cop/style/hash_as_last_array_item_spec.rb +++ b/spec/rubocop/cop/style/hash_as_last_array_item_spec.rb @@ -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 @@ -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