diff --git a/CHANGELOG.md b/CHANGELOG.md index ac4a3584441..42b0e485dd4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/config/default.yml b/config/default.yml index 462c00e481b..15274f537eb 100644 --- a/config/default.yml +++ b/config/default.yml @@ -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.' diff --git a/docs/modules/ROOT/pages/cops_style.adoc b/docs/modules/ROOT/pages/cops_style.adoc index 96c268e7ba5..dcb14d4ff5a 100644 --- a/docs/modules/ROOT/pages/cops_style.adoc +++ b/docs/modules/ROOT/pages/cops_style.adoc @@ -5170,9 +5170,9 @@ end | Enabled | Yes -| No +| Yes | 0.9 -| - +| 0.86 |=== This cop checks for multi-line ternary op expressions. @@ -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 diff --git a/lib/rubocop/cop/style/multiline_ternary_operator.rb b/lib/rubocop/cop/style/multiline_ternary_operator.rb index e897c8643f3..cf69c248c16 100644 --- a/lib/rubocop/cop/style/multiline_ternary_operator.rb +++ b/lib/rubocop/cop/style/multiline_ternary_operator.rb @@ -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.' @@ -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 diff --git a/spec/rubocop/cop/style/multiline_ternary_operator_spec.rb b/spec/rubocop/cop/style/multiline_ternary_operator_spec.rb index cd96bdea495..ddd4bcdac0c 100644 --- a/spec/rubocop/cop/style/multiline_ternary_operator_spec.rb +++ b/spec/rubocop/cop/style/multiline_ternary_operator_spec.rb @@ -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