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

[Fix #7434] Fix an incorrect autocorrect for Style/MultilineWhenThen #7437

Conversation

koic
Copy link
Member

@koic koic commented Oct 16, 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.

% cat example.rb
# frozen_string_literal: true

case foo
when bar
  then do_something
end
% ruby -c example.rb
Syntax OK

Before

% 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.

# frozen_string_literal: true

case foo
when bar do_something
end
% ruby -c example.rb
example.rb:4: syntax error, unexpected tIDENTIFIER, expecting do or '{'
or '('
when bar do_something

After

% 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.

# 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.

 case foo
 when bar
- do_something
+  do_something
 end

Before submitting the PR make sure the following are checked:

  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Run bundle exec rake default. It executes all tests and RuboCop for itself, and generates the documentation.

expect(new_source).to eq(<<~RUBY)
case foo
when bar
do_something
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 There is a slight difference in indentation using only this cop. That indentation is corrected using Layout/IndentationWidth cop.

@koic koic force-pushed the fix_incorrect_autocorrect_for_style_multiline_when_then branch 4 times, most recently from 9289808 to 6acd60e Compare October 21, 2019 07:17
…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
```
@koic koic force-pushed the fix_incorrect_autocorrect_for_style_multiline_when_then branch from 6acd60e to dbbf620 Compare October 21, 2019 16:12
@bbatsov bbatsov merged commit 12f07bb into rubocop:master Oct 21, 2019
@bbatsov
Copy link
Collaborator

bbatsov commented Oct 21, 2019

🚀

@koic koic deleted the fix_incorrect_autocorrect_for_style_multiline_when_then branch October 21, 2019 17:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Style/MultilineWhenThen autocorrect generates invalid Ruby for certain case statements
2 participants