Skip to content

Commit

Permalink
Fix an incorrect auto-correct for Style/MultilineWhenThen
Browse files Browse the repository at this point in the history
This PR fixes the following incorrect auto-correct for `Style/MultilineWhenThen`
when line break for multiple condidate values of `when` statement.

```console
% cat example.rb
case condition
when :foo,
     :bar then nil
end

% rubocop -a
(snip)

E

Offenses:

example.rb:3:11: E: Lint/Syntax: unexpected token kNIL
(Using Ruby 2.7 parser; configure using TargetRubyVersion parameter,
under AllCops)
     :bar nil
          ^^^
example.rb:3:11: C: [Corrected] Style/MultilineWhenThen: Do not use then
for multiline when statement.
     :bar then nil
          ^^^^

1 file inspected, 2 offenses detected, 1 offense corrected

% cat example.rb
case condition
when :foo,
     :bar nil
end

% ruby -c example.rb
example.rb:3: syntax error, unexpected `nil', expecting `then' or ',' or
';' or '\n'
     :bar nil
```
  • Loading branch information
koic committed Sep 21, 2020
1 parent 760d98b commit c5e6105
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -11,6 +11,7 @@
* [#8740](https://github.com/rubocop-hq/rubocop/issues/8740): Fix a false positive for `Style/HashAsLastArrayItem` when the hash is in an implicit array. ([@dvandersluis][])
* [#8739](https://github.com/rubocop-hq/rubocop/issues/8739): Fix an error for `Lint/UselessTimes` when using empty block argument. ([@koic][])
* [#8742](https://github.com/rubocop-hq/rubocop/issues/8742): Fix some assignment counts for `Metrics/AbcSize`. ([@marcandre][])
* [#8750](https://github.com/rubocop-hq/rubocop/pull/8750): Fix an incorrect auto-correct for `Style/MultilineWhenThen` when line break for multiple condidate values of `when` statement. ([@koic][])

### Changes

Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/style/multiline_when_then.rb
Expand Up @@ -58,6 +58,7 @@ def on_when(node)
private

def require_then?(when_node)
return true if when_node.conditions.count >= 2
return false unless when_node.body

when_node.loc.line == when_node.body.loc.line
Expand Down
9 changes: 9 additions & 0 deletions spec/rubocop/cop/style/multiline_when_then_spec.rb
Expand Up @@ -136,4 +136,13 @@
end
RUBY
end

it 'does not register an offense when line break for multiple condidate values of `when`' do
expect_no_offenses(<<~RUBY)
case foo
when bar,
baz then do_something
end
RUBY
end
end

0 comments on commit c5e6105

Please sign in to comment.