Skip to content

Commit

Permalink
Merge pull request rubocop#13159 from koic/fix_an_error_for_style_one…
Browse files Browse the repository at this point in the history
…_line_conditional

Fix an error for `StyleOneLineConditional`
  • Loading branch information
koic authored Aug 26, 2024
2 parents 72836e6 + b2417b8 commit 985c322
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/fix_an_error_for_style_one_line_conditional.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#13159](https://github.com/rubocop/rubocop/pull/13159): Fix an error for `Style/OneLineConditional` when using if/then/else/end with multiple expressions in the `then` body. ([@koic][])
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/one_line_conditional.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class OneLineConditional < Base
def on_normal_if_unless(node)
return unless node.single_line?
return unless node.else_branch
return if node.elsif?
return if node.elsif? || node.if_branch&.begin_type?

message = message(node)
add_offense(node, message: message) do |corrector|
Expand Down
6 changes: 6 additions & 0 deletions spec/rubocop/cop/style/one_line_conditional_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@
expect_no_offenses('if cond then run end')
end

it 'does not register an offense when using if/then/else/end with multiple expressions in the `then` body' do
expect_no_offenses(<<~RUBY)
if cond then x; y else z end
RUBY
end

it 'registers and corrects an offense with ternary operator for unless/then/else/end' do
expect_offense(<<~RUBY)
unless cond then run else dont end
Expand Down

0 comments on commit 985c322

Please sign in to comment.