Skip to content

Commit

Permalink
Merge pull request #12698 from koic/fix_error_for_style_multiline_ter…
Browse files Browse the repository at this point in the history
…nary_operator

[Fix #12691] Fix an error for `Style/MultilineTernaryOperator`
  • Loading branch information
koic committed Feb 16, 2024
2 parents 7b87261 + efa4eb8 commit e8df84b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
@@ -0,0 +1 @@
* [#12691](https://github.com/rubocop/rubocop/issues/12691): Fix an error for `Style/MultilineTernaryOperator` when nesting multiline ternary operators. ([@koic][])
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 e8df84b

Please sign in to comment.