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

Fix an incorrect auto-correct for Style/MultilineTernaryOperator #8661

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 @@ -32,6 +32,7 @@
* [#8626](https://github.com/rubocop-hq/rubocop/issues/8626): Fix false negatives for `Lint/UselessMethodDefinition`. ([@marcandre][])
* [#8698](https://github.com/rubocop-hq/rubocop/pull/8698): Fix cache to avoid encoding exception. ([@marcandre][])
* [#8704](https://github.com/rubocop-hq/rubocop/issues/8704): Fix an error for `Lint/AmbiguousOperator` when using safe navigation operator with a unary operator. ([@koic][])
* [#8661](https://github.com/rubocop-hq/rubocop/pull/8661): Fix an incorrect auto-correct for `Style/MultilineTernaryOperator` when returning a multiline ternary operator expression. ([@koic][])

### Changes

Expand Down
3 changes: 3 additions & 0 deletions docs/modules/ROOT/pages/cops_style.adoc
Expand Up @@ -5757,6 +5757,9 @@ end

This cop checks for multi-line ternary op expressions.

NOTE: `return if ... else ... end` is syntax error. If `return` is used before
multiline ternary operator expression, it cannot be auto-corrected.

=== Examples

[source,ruby]
Expand Down
15 changes: 14 additions & 1 deletion lib/rubocop/cop/style/multiline_ternary_operator.rb
Expand Up @@ -5,6 +5,9 @@ module Cop
module Style
# This cop checks for multi-line ternary op expressions.
#
# NOTE: `return if ... else ... end` is syntax error. If `return` is used before
# multiline ternary operator expression, it cannot be auto-corrected.
#
# @example
# # bad
# a = cond ?
Expand All @@ -29,9 +32,13 @@ class MultilineTernaryOperator < Base
'use `if` or `unless` instead.'

def on_if(node)
return unless node.ternary? && node.multiline?
return unless offense?(node)

add_offense(node) do |corrector|
# `return if ... else ... end` is syntax error. If `return` is used before
# multiline ternary operator expression, it cannot be auto-corrected.
next unless offense?(node) && !node.parent.return_type?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can add a comment here explaining why this is needed for future reference.


corrector.replace(node, <<~RUBY.chop)
if #{node.condition.source}
#{node.if_branch.source}
Expand All @@ -41,6 +48,12 @@ def on_if(node)
RUBY
end
end

private

def offense?(node)
node.ternary? && node.multiline?
end
end
end
end
Expand Down
11 changes: 11 additions & 0 deletions spec/rubocop/cop/style/multiline_ternary_operator_spec.rb
Expand Up @@ -53,6 +53,17 @@
RUBY
end

it 'register an offense and does not auto-correct when returning a multiline ternary operator expression' do
expect_offense(<<~RUBY)
return cond ?
^^^^^^ Avoid multi-line ternary operators, use `if` or `unless` instead.
foo :
bar
RUBY

expect_no_corrections
end

it 'accepts a single line ternary operator expression' do
expect_no_offenses('a = cond ? b : c')
end
Expand Down