From dbbf620ac52a2bd3dc9a1c973e43044586221f45 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Wed, 16 Oct 2019 18:21:01 +0900 Subject: [PATCH] [Fix #7434] Fix an incorrect autocorrect for `Style/MultilineWhenThen` Fixes #7434. This PR fixes an incorrect autocorrect for `Style/MultilineWhenThen` when the body of `when` branch starts with `then`. The following is a reproduction procedure. ```console % cat example.rb # frozen_string_literal: true case foo when bar then do_something end ``` ``` % ruby -c example.rb Syntax OK ``` ## Before ```console % rubocop example.rb Inspecting 1 file C Offenses: example.rb:5:3: C: Style/MultilineWhenThen: Do not use then for multiline when statement. then do_something ^^^^ ``` It is changed to the code with syntax error as follows. ```ruby # frozen_string_literal: true case foo when bar do_something end ``` ```console % ruby -c example.rb example.rb:4: syntax error, unexpected tIDENTIFIER, expecting do or '{' or '(' when bar do_something ``` ## After ```console % rubocop example.rb -a Inspecting 1 file C Offenses: example.rb:5:1: C: [Corrected] Layout/IndentationWidth: Use 2 (not 1) spaces for indentation. do_something ^ example.rb:5:3: C: [Corrected] Style/MultilineWhenThen: Do not use then for multiline when statement. then do_something ^^^^ 1 file inspected, 2 offenses detected, 2 offenses corrected ``` It will be changed to valid Ruby code as follows. ```ruby # frozen_string_literal: true case foo when bar do_something end ``` There is a slight difference in indentation using only this cop. That indentation is corrected using `Layout/IndentationWidth` cop. ```diff case foo when bar - do_something + do_something end ``` --- CHANGELOG.md | 1 + lib/rubocop/cop/style/multiline_when_then.rb | 2 +- .../cop/style/multiline_when_then_spec.rb | 17 +++++++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d1926291449..576a6db828e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ ### Changes * [#7446](https://github.com/rubocop-hq/rubocop/issues/7446): Add `merge` to list of non-mutating methods. ([@cstyles][]) +* [#7434](https://github.com/rubocop-hq/rubocop/issues/7434): Fix an incorrect autocorrect for `Style/MultilineWhenThen` when the body of `when` branch starts with `then`. ([@koic][]) ## 0.75.1 (2019-10-14) diff --git a/lib/rubocop/cop/style/multiline_when_then.rb b/lib/rubocop/cop/style/multiline_when_then.rb index 38325dacd64..2adca8f750d 100644 --- a/lib/rubocop/cop/style/multiline_when_then.rb +++ b/lib/rubocop/cop/style/multiline_when_then.rb @@ -44,7 +44,7 @@ def autocorrect(node) lambda do |corrector| corrector.remove( range_with_surrounding_space( - range: node.loc.begin, side: :left + range: node.loc.begin, side: :left, newlines: false ) ) end diff --git a/spec/rubocop/cop/style/multiline_when_then_spec.rb b/spec/rubocop/cop/style/multiline_when_then_spec.rb index 231b9408032..29241e6697e 100644 --- a/spec/rubocop/cop/style/multiline_when_then_spec.rb +++ b/spec/rubocop/cop/style/multiline_when_then_spec.rb @@ -84,4 +84,21 @@ end RUBY end + + it 'autocorrects when the body of `when` branch starts ' \ + 'with `then`' do + new_source = autocorrect_source(<<~RUBY) + case foo + when bar + then do_something + end + RUBY + + expect(new_source).to eq(<<~RUBY) + case foo + when bar + do_something + end + RUBY + end end