Skip to content

Commit

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

This PR fixes an error for `Style/OneLineConditional` when using
`if-then-elsif-then-end`.
  • Loading branch information
koic committed Sep 23, 2020
1 parent c70badc commit 9bdb1c5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
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
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
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 9bdb1c5

Please sign in to comment.