Skip to content

Commit

Permalink
[Fix #8821] Fix an incorrect autocorrect for `Style/NestedTernaryOper…
Browse files Browse the repository at this point in the history
…ator` (#8832)

Fixes #8821

This PR fixes an incorrect autocorrect for `Style/NestedTernaryOperator` when
using a nested ternary operator expression with no parentheses on the outside.

Co-authored-by: Bozhidar Batsov <[email protected]>
  • Loading branch information
koic and bbatsov authored Oct 8, 2020
1 parent 1a84099 commit 8f9b913
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* [#8825](https://github.com/rubocop-hq/rubocop/issues/8825): Fix crash in `Style/ExplicitBlockArgument` when code is called outside of a method. ([@ghiculescu][])
* [#8718](https://github.com/rubocop-hq/rubocop/issues/8718): Fix undefined methods of pseudo location. ([@ybiquitous][])
* [#8354](https://github.com/rubocop-hq/rubocop/issues/8354): Detect regexp named captures in `Style/CaseLikeIf` cop. ([@dsavochkin][])
* [#8821](https://github.com/rubocop-hq/rubocop/issues/8821): Fix an incorrect autocorrect for `Style/NestedTernaryOperator` when using a nested ternary operator expression with no parentheses on the outside. ([@koic][])
* [#8834](https://github.com/rubocop-hq/rubocop/issues/8834): Fix a false positive for `Style/ParenthesesAsGroupedExpression` when method argument parentheses are omitted and hash argument key is enclosed in parentheses. ([@koic][])
* [#8830](https://github.com/rubocop-hq/rubocop/issues/8830): Fix bad autocorrect of `Style/StringConcatenation` when string includes double quotes. ([@tleish][])
* [#8807](https://github.com/rubocop-hq/rubocop/pull/8807): Fix a false positive for `Style/RedundantCondition` when using assignment by hash key access. ([@koic][])
Expand Down
2 changes: 2 additions & 0 deletions lib/rubocop/cop/style/nested_ternary_operator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ def if_node(node)
end

def remove_parentheses(source)
return source unless source.start_with?('(')

source.gsub(/\A\(/, '').gsub(/\)\z/, '')
end
end
Expand Down
15 changes: 15 additions & 0 deletions spec/rubocop/cop/style/nested_ternary_operator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,21 @@
RUBY
end

it 'registers an offense and corrects for a nested ternary operator expression with no parentheses on the outside' do
expect_offense(<<~RUBY)
x ? y + (z ? 1 : 0) : nil
^^^^^^^^^ Ternary operators must not be nested. Prefer `if` or `else` constructs instead.
RUBY

expect_correction(<<~RUBY)
if x
y + (z ? 1 : 0)
else
nil
end
RUBY
end

it 'accepts a non-nested ternary operator within an if' do
expect_no_offenses(<<~RUBY)
a = if x
Expand Down

0 comments on commit 8f9b913

Please sign in to comment.