Skip to content

Commit

Permalink
[Fix #2874] Accept parentheses in hash and array (#3078)
Browse files Browse the repository at this point in the history
`Style/RedundantParentheses` doesn't register an offense for
parentheses in array and hash literals when the removal would cause
invalid syntax. For example, the following is accepted:

    array = [(
             <<-EOF
               something
             EOF
             ),
             'something']
  • Loading branch information
lumeet authored and bbatsov committed Apr 26, 2016
1 parent 0aeca81 commit bf39de2
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -38,6 +38,7 @@
* [#3048](https://github.com/bbatsov/rubocop/issues/3048): `Lint/NestedMethodDefinition` shouldn't flag methods defined on Structs. ([@owst][])
* [#2912](https://github.com/bbatsov/rubocop/issues/2912): Check whether a line is aligned with the following line if the preceding line is not an assignment. ([@akihiro17][])
* [#3036](https://github.com/bbatsov/rubocop/issues/3036): Don't let `Lint/UnneededDisable` inspect files that are excluded for the cop. ([@jonas054][])
* [#2874](https://github.com/bbatsov/rubocop/issues/2874): Fix bug when the closing parenthesis is preceded by a newline in array and hash literals in `Style/RedundantParentheses`. ([@lumeet][])

### Changes

Expand Down
28 changes: 27 additions & 1 deletion lib/rubocop/cop/style/redundant_parentheses.rb
Expand Up @@ -42,7 +42,13 @@ def parens_allowed?(node)
# don't flag `rescue(ExceptionClass)`
rescue?(node) ||
# don't flag `method (arg) { }`
(arg_in_call_with_block?(node) && !parentheses?(parent))
(arg_in_call_with_block?(node) && !parentheses?(parent)) ||
# don't flag
# ```
# { a: (1
# ), }
# ```
allowed_array_or_hash_element?(node)
end

def check(begin_node)
Expand Down Expand Up @@ -83,6 +89,26 @@ def keyword_ancestor?(node)
node.parent && node.parent.keyword?
end

def allowed_array_or_hash_element?(node)
(hash_element?(node) || array_element?(node)) &&
only_closing_paren_before_comma?(node)
end

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

def array_element?(node)
node.parent && node.parent.array_type?
end

def only_closing_paren_before_comma?(node)
source_buffer = node.source_range.source_buffer
line_range = source_buffer.line_range(node.loc.end.line)

line_range.source =~ /^\s*\)\s*,/
end

def disallowed_literal?(node)
node.literal? && !ALLOWED_LITERALS.include?(node.type)
end
Expand Down
5 changes: 5 additions & 0 deletions spec/rubocop/cop/style/redundant_parentheses_spec.rb
Expand Up @@ -104,6 +104,11 @@
it_behaves_like 'plausible', '+(1.foo.bar)'

it_behaves_like 'redundant', '[(1)]', '[1]', 'a literal', '(1)'
it_behaves_like 'redundant', "[(1\n)]", "[1\n]", 'a literal', "(1\n)"
it_behaves_like 'plausible', "[(1\n),]"
it_behaves_like 'redundant', '{a: (1)}', '{a: 1}', 'a literal', '(1)'
it_behaves_like 'redundant', "{a: (1\n)}", "{a: 1\n}", 'a literal', "(1\n)"
it_behaves_like 'plausible', "{a: (1\n),}"

it 'accepts parentheses around a method call with unparenthesized ' \
'arguments' do
Expand Down

0 comments on commit bf39de2

Please sign in to comment.