Skip to content

Commit

Permalink
[Fix #7434] Fix an incorrect autocorrect for Style/MultilineWhenThen
Browse files Browse the repository at this point in the history
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
```
  • Loading branch information
koic authored and bbatsov committed Oct 21, 2019
1 parent 777eeae commit 12f07bb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/multiline_when_then.rb
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions spec/rubocop/cop/style/multiline_when_then_spec.rb
Expand Up @@ -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

0 comments on commit 12f07bb

Please sign in to comment.