Skip to content

Commit

Permalink
[Fix #7616] Fix an incorrect autocorrect for Style/MultilineWhenThen
Browse files Browse the repository at this point in the history
Fixes #7616.

This PR fixes an incorrect autocorrect for `Style/MultilineWhenThen`
for when statement with then is an array or a hash.

The following is a reproduction procedure.

```console
% cat example.rb
case condition
when foo then {
    key: 'value'
  }
end

% ruby -c example.rb
Syntax OK
```

It is changed to the code with syntax error as follows.

```console
% bundle exec rubocop -a --only Style/MultilineWhenThen
(snip)

% cat example.rb
case condition
when foo {
    key: 'value'
  }
end

% ruby -c example.rb
example.rb:3: syntax error, unexpected ':', expecting '}'
    key: 'value'
example.rb:4: syntax error, unexpected '}', expecting end-of-input
```

The same is true when replacing a hash with an array.
The PR will accept these cases without offense.
  • Loading branch information
koic committed Jan 4, 2020
1 parent 81c93c6 commit 0ac1450
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* [#7607](https://github.com/rubocop-hq/rubocop/issues/7607): Fix `Style/FrozenStringLiteralComment` infinite loop when magic comments are newline-separated. ([@pirj][])
* [#7602](https://github.com/rubocop-hq/rubocop/pull/7602): Ensure proper handling of Ruby 2.7 syntax. ([@drenmi][])
* [#7620](https://github.com/rubocop-hq/rubocop/issues/7620): Fix a false positive for `Migration/DepartmentName` when a disable comment contains a plain comment. ([@koic][])
* [#7616](https://github.com/rubocop-hq/rubocop/issues/7616): Fix an incorrect autocorrect for `Style/MultilineWhenThen` for when statement with then is an array or a hash. ([@koic][])

### Changes

Expand Down
6 changes: 5 additions & 1 deletion lib/rubocop/cop/style/multiline_when_then.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def on_when(node)
return if !node.children.last.nil? && !node.multiline? && node.then?

# With more than one statements after then, there's not offense
return if node.children.last&.begin_type?
return if accept_node_type?(node.body)

add_offense(node, location: :begin)
end
Expand All @@ -49,6 +49,10 @@ def autocorrect(node)
)
end
end

def accept_node_type?(node)
node&.begin_type? || node&.array_type? || node&.hash_type?
end
end
end
end
Expand Down
20 changes: 20 additions & 0 deletions spec/rubocop/cop/style/multiline_when_then_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,26 @@
RUBY
end

it 'does not register an offense for hash when statement with then' do
expect_no_offenses(<<~RUBY)
case condition
when foo then {
key: 'value'
}
end
RUBY
end

it 'does not register an offense for array when statement with then' do
expect_no_offenses(<<~RUBY)
case condition
when foo then [
'element'
]
end
RUBY
end

it 'autocorrects then in empty when' do
new_source = autocorrect_source(<<~RUBY)
case foo
Expand Down

0 comments on commit 0ac1450

Please sign in to comment.