Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Style/MultilineWhenThen autocorrect generates invalid Ruby for certain case statements #7434

Closed
leklund opened this issue Oct 15, 2019 · 0 comments · Fixed by #7437
Closed
Labels

Comments

@leklund
Copy link
Contributor

leklund commented Oct 15, 2019

The autocorrect for Style/MultilineWhenThen generates invalid ruby when correcting a case statement with the following format (then on the line after the when):

case foo
when :bar
  then :value

Definitely not a standard way to write a case statement but it is valid Ruby.


Full Example code:

def flavor(flave)
  case flave
  when 'chocolate'
    then %w[dark white milk].sample
  when 'fruit'
    then %w[apple banana orange].sample
  when 'null'
    then nil
  end
end

Expected behavior

I would expect autocorrect to just remove the unnecessary then like so:

def flavor(flave)
  case flave
  when 'chocolate'
   %w[dark white milk].sample
  when 'fruit'
   %w[apple banana orange].sample
  when 'null'
   nil
  end
end

Actual behavior

Instead, what we get is invalid ruby:

def flavor(flave)
  case flave
  when 'chocolate' %w[dark white milk].sample
  when 'fruit' %w[apple banana orange].sample
  when 'null' nil
  end
end

Steps to reproduce the problem

The sample code provided above can reproduce the problem.

RuboCop version

0.75.1 (using Parser 2.6.5.0, running on ruby 2.4.2 x86_64-darwin17)
@Drenmi Drenmi added the bug label Oct 16, 2019
koic added a commit to koic/rubocop that referenced this issue Oct 21, 2019
…henThen`

Fixes rubocop#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
```
bbatsov pushed a commit that referenced this issue Oct 21, 2019
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
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants