Skip to content

Commit

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

This PR fixes an error for `Style/OneLineConditional` when using
`if-then-elsif-then-end`.
  • Loading branch information
koic authored and bbatsov committed Sep 23, 2020
1 parent faaa683 commit 5cc162e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* [#8754](https://github.com/rubocop-hq/rubocop/pull/8754): Fix an error for `Style/RandomWithOffset` when using a range with non-integer bounds. ([@eugeneius][])
* [#8756](https://github.com/rubocop-hq/rubocop/issues/8756): Fix an infinite loop error for `Layout/EmptyLinesAroundAccessModifier` with `Layout/EmptyLinesAroundBlockBody` when using access modifier with block argument. ([@koic][])
* [#8764](https://github.com/rubocop-hq/rubocop/issues/8764): Fix `Layout/CaseIndentation` not showing the cop name in output messages. ([@dvandersluis][])
* [#8771](https://github.com/rubocop-hq/rubocop/issues/8771): Fix an error for `Style/OneLineConditional` when using `if-then-elsif-then-end`. ([@koic][])

### Changes

Expand Down
4 changes: 3 additions & 1 deletion lib/rubocop/cop/style/one_line_conditional.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ def multiline_replacement(node, indentation = nil)
end

def else_branch_to_multiline(else_branch, indentation)
if else_branch.if_type? && else_branch.elsif?
if else_branch.nil?
'end'
elsif else_branch.if_type? && else_branch.elsif?
multiline_replacement(else_branch, indentation)
else
<<~RUBY.chomp
Expand Down
16 changes: 16 additions & 0 deletions spec/rubocop/cop/style/one_line_conditional_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,22 @@
RUBY
end

it 'registers and corrects an offense with multi-line construct for ' \
'if-then-elsif-then-end' do
expect_offense(<<~RUBY)
if cond1 then run elsif cond2 then maybe end
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{if_offense_message}
RUBY

expect_correction(<<~RUBY)
if cond1
run
elsif cond2
maybe
end
RUBY
end

it 'registers and corrects an offense with multi-line construct for ' \
'if-then-elsif-then-else-end' do
expect_offense(<<~RUBY)
Expand Down

0 comments on commit 5cc162e

Please sign in to comment.