Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support autocorrection for Style/MultilineTernaryOperator #8148

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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