Skip to content

Commit

Permalink
Allow safe-navigation in ternary conditions without parenthesis
Browse files Browse the repository at this point in the history
When enforced style is `require_parenthesis_if_complex`, the following
code is considered valid:

```ruby
  foo = bar.baz? ? a : b
```

This commit makes the following code also acceptable:

```ruby
  foo = bar&.baz ? a : b
```
  • Loading branch information
Artem Ignatyev authored and bbatsov committed Jun 25, 2019
1 parent de29e43 commit 239e2b6
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

### Bug fixes

* [#7121](https://github.com/rubocop-hq/rubocop/pull/7121): Fix `Style/TernaryParentheses` cop to allow safe navigation operator without parentheses. ([@timon][])
* [#7063](https://github.com/rubocop-hq/rubocop/issues/7063): Fix autocorrect in `Style/TernaryParentheses` cop. ([@parkerfinch][])
* [#7106](https://github.com/rubocop-hq/rubocop/issues/7106): Fix an error for `Lint/NumberConversion` when `#to_i` called on a variable on a hash. ([@koic][])
* [#7107](https://github.com/rubocop-hq/rubocop/issues/7107): Fix parentheses offence for numeric arguments with an operator in `Style/MethodCallWithArgsParentheses`. ([@gsamokovarov][])
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/ternary_parentheses.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def non_complex_expression?(condition)
end

def non_complex_send?(node)
return false unless node.send_type?
return false unless node.call_type?

!node.operator_method? || node.method?(:[])
end
Expand Down
4 changes: 4 additions & 0 deletions spec/rubocop/cop/style/ternary_parentheses_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,10 @@
it_behaves_like 'code with offense',
'foo = bar or (baz) ? a : b',
'foo = bar or baz ? a : b'

it_behaves_like 'code with offense',
'foo = (bar&.baz) ? a : b',
'foo = bar&.baz ? a : b'
end

context 'with a complex condition' do
Expand Down

0 comments on commit 239e2b6

Please sign in to comment.