Skip to content

Commit

Permalink
[Fix #9689] Treat parens around array items the same for children and…
Browse files Browse the repository at this point in the history
… deeper descendants.
  • Loading branch information
dvandersluis authored and bbatsov committed Apr 19, 2021
1 parent 6136239 commit c53803b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
1 change: 1 addition & 0 deletions changelog/fix_treat_parens_around_array_items_the_same.md
@@ -0,0 +1 @@
* [#9689](https://github.com/rubocop/rubocop/issues/9689): Treat parens around array items the same for children and deeper descendants. ([@dvandersluis][])
10 changes: 3 additions & 7 deletions lib/rubocop/cop/style/redundant_parentheses.rb
Expand Up @@ -158,15 +158,11 @@ def allowed_array_or_hash_element?(node)
# { a: (1
# ), }
# ```
(hash_element?(node) || array_element?(node)) && only_closing_paren_before_comma?(node)
hash_or_array_element?(node) && only_closing_paren_before_comma?(node)
end

def hash_element?(node)
node.parent&.pair_type?
end

def array_element?(node)
node.parent&.array_type?
def hash_or_array_element?(node)
node.each_ancestor(:array, :hash).any?
end

def only_closing_paren_before_comma?(node)
Expand Down
18 changes: 18 additions & 0 deletions spec/rubocop/cop/style/redundant_parentheses_spec.rb
Expand Up @@ -162,6 +162,24 @@

it_behaves_like 'plausible', "[(1\n),]"

it_behaves_like 'plausible', <<~RUBY
[
(
1
),
2
]
RUBY

it_behaves_like 'plausible', <<~RUBY
[
x = (
1
),
y = 2
]
RUBY

it 'registers an offense for parens around a literal hash value' do
expect_offense(<<~RUBY)
{a: (1)}
Expand Down

0 comments on commit c53803b

Please sign in to comment.