From 5cc162e7ab4d4122a070a88a605ec457a3079df9 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Wed, 23 Sep 2020 13:58:37 +0900 Subject: [PATCH] [Fix #8771] Fix an error for `Style/OneLineConditional` Fixes #8771. This PR fixes an error for `Style/OneLineConditional` when using `if-then-elsif-then-end`. --- CHANGELOG.md | 1 + lib/rubocop/cop/style/one_line_conditional.rb | 4 +++- .../cop/style/one_line_conditional_spec.rb | 16 ++++++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 998c0542e2c..f7ad9864746 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/rubocop/cop/style/one_line_conditional.rb b/lib/rubocop/cop/style/one_line_conditional.rb index 56c90a44471..0b079c4fbe1 100644 --- a/lib/rubocop/cop/style/one_line_conditional.rb +++ b/lib/rubocop/cop/style/one_line_conditional.rb @@ -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 diff --git a/spec/rubocop/cop/style/one_line_conditional_spec.rb b/spec/rubocop/cop/style/one_line_conditional_spec.rb index 3f6351ae95c..551cc430bda 100644 --- a/spec/rubocop/cop/style/one_line_conditional_spec.rb +++ b/spec/rubocop/cop/style/one_line_conditional_spec.rb @@ -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)