Skip to content

Commit

Permalink
[Fix rubocop#12691] Fix an error for Style/MultilineTernaryOperator
Browse files Browse the repository at this point in the history
Fixes rubocop#12691.

This PR fixes an error for `Style/MultilineTernaryOperator`
when nesting multiline ternary operators.
  • Loading branch information
koic committed Feb 16, 2024
1 parent d8d8474 commit 8d354c3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/rubocop/cop/style/multiline_ternary_operator.rb
Expand Up @@ -47,7 +47,11 @@ def on_if(node)
message = enforce_single_line_ternary_operator?(node) ? MSG_SINGLE_LINE : MSG_IF

add_offense(node, message: message) do |corrector|
next if part_of_ignored_node?(node)

autocorrect(corrector, node)

ignore_node(node)
end
end

Expand Down
22 changes: 22 additions & 0 deletions spec/rubocop/cop/style/multiline_ternary_operator_spec.rb
Expand Up @@ -50,6 +50,28 @@
RUBY
end

it 'registers an offense and corrects when nesting multiline ternary operators' do
expect_offense(<<~RUBY)
cond_a? ? foo :
^^^^^^^^^^^^^^^ Avoid multi-line ternary operators, use `if` or `unless` instead.
cond_b? ? bar :
^^^^^^^^^^^^^^^ Avoid multi-line ternary operators, use `if` or `unless` instead.
cond_c? ? baz : qux
RUBY

expect_correction(<<~RUBY)
if cond_a?
foo
else
if cond_b?
bar
else
cond_c? ? baz : qux
end
end
RUBY
end

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

0 comments on commit 8d354c3

Please sign in to comment.