Skip to content

Commit

Permalink
[Fix rubocop#4106] Make Style/TernaryParentheses unsafe autocorrect…
Browse files Browse the repository at this point in the history
… detector smarter

This cop, despite checking for unsafe autocorrects, did not recognize
methods sent to constants or literals, leading to inappropriate omission
recommendations.

This change fixes that.
  • Loading branch information
Drenmi committed Apr 2, 2017
1 parent aa05c64 commit f00aef1
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* [#4152](https://github.com/bbatsov/rubocop/pull/4152): Make `Style/MethodCallWithArgsParentheses` not require parens on setter methods. ([@drenmi][])
* [#4226](https://github.com/bbatsov/rubocop/pull/4226): Show in `--help` output that `--stdin` takes a file name argument. ([@jonas054][])
* [#4217](https://github.com/bbatsov/rubocop/pull/4217): Fix false positive in `Rails/FilePath` cop with non string argument. ([@soutaro][])
* [#4106](https://github.com/bbatsov/rubocop/pull/4106): Make `Style/TernaryParentheses` unsafe autocorrect detector aware of literals and constants. ([@drenmi][])

## 0.48.0 (2017-03-26)

Expand Down
5 changes: 3 additions & 2 deletions lib/rubocop/cop/style/ternary_parentheses.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ module Cop
module Style
# This cop checks for the presence of parentheses around ternary
# conditions. It is configurable to enforce inclusion or omission of
# parentheses using `EnforcedStyle`.
# parentheses using `EnforcedStyle`. Omission is only enforced when
# removing the parentheses won't cause a different behaviour.
#
# @example
#
Expand Down Expand Up @@ -161,7 +162,7 @@ def unparenthesized_method_call?(child)

def_node_matcher :method_call_argument, <<-PATTERN
{(:defined? $...)
(send {(send ...) nil} _ $(send nil _)...)}
(send {_ nil} _ $(send nil _)...)}
PATTERN

def_node_matcher :square_brackets?,
Expand Down
3 changes: 2 additions & 1 deletion manual/cops_style.md
Original file line number Diff line number Diff line change
Expand Up @@ -5639,7 +5639,8 @@ Enabled | Yes

This cop checks for the presence of parentheses around ternary
conditions. It is configurable to enforce inclusion or omission of
parentheses using `EnforcedStyle`.
parentheses using `EnforcedStyle`. Omission is only enforced when
removing the parentheses won't cause a different behaviour.

### Example

Expand Down
10 changes: 10 additions & 0 deletions spec/rubocop/cop/style/ternary_parentheses_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,16 @@
'foo = (baz.foo? bar) ? a : b'
end

context 'when calling method on a literal receiver' do
it_behaves_like 'code with offense',
'foo = ("bar".foo? bar) ? a : b'
end

context 'when calling method on a constant receiver' do
it_behaves_like 'code with offense',
'foo = (Bar.foo? bar) ? a : b'
end

context 'when calling method with multiple arguments' do
it_behaves_like 'code with offense',
'foo = (baz.foo? bar, baz) ? a : b'
Expand Down

0 comments on commit f00aef1

Please sign in to comment.