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 #10711] Fix an error for Style/MultilineTernaryOperator #10713

Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#10711](https://github.com/rubocop/rubocop/issues/10711): Fix an error for `Style/MultilineTernaryOperator` when the false branch is on a separate line. ([@koic][])
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/multiline_ternary_operator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def replacement(node)
end

def enforce_single_line_ternary_operator?(node)
SINGLE_LINE_TYPES.include?(node.parent.type) && !use_assignment_method?(node.parent)
SINGLE_LINE_TYPES.include?(node.parent&.type) && !use_assignment_method?(node.parent)
end

def use_assignment_method?(node)
Expand Down
18 changes: 17 additions & 1 deletion spec/rubocop/cop/style/multiline_ternary_operator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
RUBY
end

it 'registers an offense and corrects 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 and assigning a return value' do
expect_offense(<<~RUBY)
a = cond ? b :
^^^^^^^^^^ Avoid multi-line ternary operators, use `if` or `unless` instead.
Expand All @@ -34,6 +34,22 @@
RUBY
end

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

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

it 'registers an offense and corrects when everything is on a separate line' do
expect_offense(<<~RUBY)
a = cond ?
Expand Down