From d08a919a469e152dd62a715032308ab8a881d78e Mon Sep 17 00:00:00 2001 From: William Montgomery Date: Fri, 7 Aug 2020 15:02:26 -0400 Subject: [PATCH] [Fix #8475] Fix a false positive for Style/HashAsLastArrayItem when there are duplicate hashes in the array Co-authored-by: Koichi ITO --- CHANGELOG.md | 2 ++ lib/rubocop/cop/style/hash_as_last_array_item.rb | 2 +- spec/rubocop/cop/style/hash_as_last_array_item_spec.rb | 6 +++--- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 88ef3c4e6e4..c13f8f1c6d1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ * [#8478](https://github.com/rubocop-hq/rubocop/issues/8478): Relax `Lint/BinaryOperatorWithIdenticalOperands` for mathematical operations. ([@marcandre][]) * [#8480](https://github.com/rubocop-hq/rubocop/issues/8480): Tweak callback list of `Lint/MissingSuper`. ([@marcandre][]) * [#8481](https://github.com/rubocop-hq/rubocop/pull/8481): Fix autocorrect for elements with newlines in `Style/SymbolArray` and `Style/WordArray`. ([@biinari][]) +* [#8475](https://github.com/rubocop-hq/rubocop/issues/8475): Fix a false positive for `Style/HashAsLastArrayItem` when there are duplicate hashes in the array. ([@wcmonty][]) ## 0.89.0 (2020-08-05) @@ -4764,3 +4765,4 @@ [@volfgox]: https://github.com/volfgox [@dsavochkin]: https://github.com/dmytro-savochkin [@sonalinavlakhe]: https://github.com/sonalinavlakhe +[@wcmonty]: https://github.com/wcmonty 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 fe739e2f0de..a761e46f5a4 100644 --- a/lib/rubocop/cop/style/hash_as_last_array_item.rb +++ b/lib/rubocop/cop/style/hash_as_last_array_item.rb @@ -40,7 +40,7 @@ def last_array_item?(node) parent = node.parent return false unless parent - parent.array_type? && parent.values.last == node + parent.array_type? && 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 a65336920a0..08e7192f0a8 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 @@ -39,12 +39,12 @@ it 'registers an offense and corrects when hash with braces' do expect_offense(<<~RUBY) - [{ one: 1 }, 2, { three: 3 }] - ^^^^^^^^^^^^ Omit the braces around the hash. + [{ one: 1 }, { three: 3 }, 2, { three: 3 }] + ^^^^^^^^^^^^ Omit the braces around the hash. RUBY expect_correction(<<~RUBY) - [{ one: 1 }, 2, three: 3 ] + [{ one: 1 }, { three: 3 }, 2, three: 3 ] RUBY end