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 breaks case statements with {} #7616

Closed
tas50 opened this issue Jan 2, 2020 · 0 comments · Fixed by #7622
Closed

Style/MultilineWhenThen breaks case statements with {} #7616

tas50 opened this issue Jan 2, 2020 · 0 comments · Fixed by #7622
Labels

Comments

@tas50
Copy link
Contributor

tas50 commented Jan 2, 2020

Expected behavior

The Style/MultilineWhenThen cop should remove then statements from all mulitline case statements w/o creating invalid ruby code.

Actual behavior

If RuboCop encounters a case statement that uses then and {}s it will just remove the then statements, which results in invalid ruby:

Steps to reproduce the problem

Run rubocop --only Style/MultilineWhenThen on the following code:

      case version.to_s # to_s to make sure someone didn't pass us an int
      when '9.0' then {
        'ActiveDirectory' => 'Veeam Explorer for Microsoft Active Directory',
        'SQL' => 'Veeam Explorer for Microsoft SQL Server',
        'Exchange' => 'Veeam Explorer for Microsoft Exchange',
        'SharePoint' => 'Veeam Explorer for Microsoft SharePoint',
        'Oracle' => 'Veeam Explorer for Oracle'
      }
     end

This will autocorrect to:

      case version.to_s # to_s to make sure someone didn't pass us an int
      when '9.0' {
        'ActiveDirectory' => 'Veeam Explorer for Microsoft Active Directory',
        'SQL' => 'Veeam Explorer for Microsoft SQL Server',
        'Exchange' => 'Veeam Explorer for Microsoft Exchange',
        'SharePoint' => 'Veeam Explorer for Microsoft SharePoint',
        'Oracle' => 'Veeam Explorer for Oracle'
      }
     end

RuboCop version

0.78.0
@Drenmi Drenmi added the bug label Jan 3, 2020
koic added a commit to koic/rubocop that referenced this issue Jan 4, 2020
…henThen`

Fixes rubocop#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.
@koic koic closed this as completed in #7622 Jan 4, 2020
koic added a commit that referenced this issue Jan 4, 2020
…ultiline_when_then

[Fix #7616] Fix an incorrect autocorrect for `Style/MultilineWhenThen`
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