Skip to content

Commit

Permalink
Support autocorrection for Style/MultilineTernaryOperator
Browse files Browse the repository at this point in the history
This PR supports autocorrection for `Style/MultilineTernaryOperator`.
  • Loading branch information
koic authored and bbatsov committed Jun 14, 2020
1 parent 41b5b5e commit 37c34a4
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,7 @@

* [#8111](https://github.com/rubocop-hq/rubocop/pull/8111): Add auto-correct for `Style/StructInheritance`. ([@tejasbubane][])
* [#8113](https://github.com/rubocop-hq/rubocop/pull/8113): Let `expect_offense` templates add variable-length whitespace with `_{foo}`. ([@eugeneius][])
* [#8148](https://github.com/rubocop-hq/rubocop/pull/8148): Support autocorrection for `Style/MultilineTernaryOperator`. ([@koic][])

### Bug fixes

Expand Down
1 change: 1 addition & 0 deletions config/default.yml
Expand Up @@ -3239,6 +3239,7 @@ Style/MultilineTernaryOperator:
StyleGuide: '#no-multiline-ternary'
Enabled: true
VersionAdded: '0.9'
VersionChanged: '0.86'

Style/MultilineWhenThen:
Description: 'Do not use then for multi-line when statement.'
Expand Down
15 changes: 7 additions & 8 deletions docs/modules/ROOT/pages/cops_style.adoc
Expand Up @@ -5170,9 +5170,9 @@ end

| Enabled
| Yes
| No
| Yes
| 0.9
| -
| 0.86
|===

This cop checks for multi-line ternary op expressions.
Expand All @@ -5192,12 +5192,11 @@ a = cond ?
# good
a = cond ? b : c
a =
if cond
b
else
c
end
a = if cond
b
else
c
end
----

=== References
Expand Down
23 changes: 17 additions & 6 deletions lib/rubocop/cop/style/multiline_ternary_operator.rb
Expand Up @@ -17,12 +17,11 @@ module Style
#
# # good
# a = cond ? b : c
# a =
# if cond
# b
# else
# c
# end
# a = if cond
# b
# else
# c
# end
class MultilineTernaryOperator < Cop
MSG = 'Avoid multi-line ternary operators, ' \
'use `if` or `unless` instead.'
Expand All @@ -32,6 +31,18 @@ def on_if(node)

add_offense(node)
end

def autocorrect(node)
lambda do |corrector|
corrector.replace(node, <<~RUBY.chop)
if #{node.condition.source}
#{node.if_branch.source}
else
#{node.else_branch.source}
end
RUBY
end
end
end
end
end
Expand Down
30 changes: 27 additions & 3 deletions spec/rubocop/cop/style/multiline_ternary_operator_spec.rb
Expand Up @@ -3,30 +3,54 @@
RSpec.describe RuboCop::Cop::Style::MultilineTernaryOperator do
subject(:cop) { described_class.new }

it 'registers offense when the if branch and the else branch are ' \
it 'registers offense and corrects when the if branch and the else branch are ' \
'on a separate line from the condition' do
expect_offense(<<~RUBY)
a = cond ?
^^^^^^ Avoid multi-line ternary operators, use `if` or `unless` instead.
b : c
RUBY

expect_correction(<<~RUBY)
a = if cond
b
else
c
end
RUBY
end

it 'registers an offense when the false branch is on a separate line' do
it 'registers an offense and corrects when the false branch is on a separate line' do
expect_offense(<<~RUBY)
a = cond ? b :
^^^^^^^^^^ Avoid multi-line ternary operators, use `if` or `unless` instead.
c
RUBY

expect_correction(<<~RUBY)
a = if cond
b
else
c
end
RUBY
end

it 'registers an offense when everything is on a separate line' do
it 'registers an offense and corrects when everything is on a separate line' do
expect_offense(<<~RUBY)
a = cond ?
^^^^^^ Avoid multi-line ternary operators, use `if` or `unless` instead.
b :
c
RUBY

expect_correction(<<~RUBY)
a = if cond
b
else
c
end
RUBY
end

it 'accepts a single line ternary operator expression' do
Expand Down

0 comments on commit 37c34a4

Please sign in to comment.