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 #8158] Fix Style/MultilineWhenThen to better handle multiline body #8571

Merged
merged 1 commit into from Aug 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -20,6 +20,7 @@
* [#8527](https://github.com/rubocop-hq/rubocop/pull/8527): Prevent an incorrect auto-correction for `Style/CaseEquality` cop when comparing with `===` against a regular expression receiver. ([@koic][])
* [#8524](https://github.com/rubocop-hq/rubocop/issues/8524): Fix `Layout/EmptyLinesAroundClassBody` and `Layout/EmptyLinesAroundModuleBody` to correctly handle an access modifier as a first child. ([@dsavochkin][])
* [#8518](https://github.com/rubocop-hq/rubocop/issues/8518): Fix `Lint/ConstantResolution` cop reporting offense for `module` and `class` definitions. ([@tejasbubane][])
* [#8158](https://github.com/rubocop-hq/rubocop/issues/8158): Fix `Style/MultilineWhenThen` cop to correctly handle cases with multiline body. ([@dsavochkin][])

### Changes

Expand Down
4 changes: 2 additions & 2 deletions lib/rubocop/cop/style/multiline_when_then.rb
Expand Up @@ -44,7 +44,7 @@ def on_when(node)
# Requires `then` for write `when` and its body on the same line.
return if require_then?(node)

# With more than one statements after then, there's not offense
# For arrays and hashes there's no offense
return if accept_node_type?(node.body)

range = node.loc.begin
Expand All @@ -64,7 +64,7 @@ def require_then?(when_node)
end

def accept_node_type?(node)
node&.begin_type? || node&.array_type? || node&.hash_type?
node&.array_type? || node&.hash_type?
end
end
end
Expand Down
21 changes: 20 additions & 1 deletion spec/rubocop/cop/style/multiline_when_then_spec.rb
Expand Up @@ -18,7 +18,7 @@
RUBY
end

it 'registers an offense for multiline when statement with then' do
it 'registers an offense for multiline (one line in a body) when statement with then' do
expect_offense(<<~RUBY)
case foo
when bar then
Expand All @@ -35,6 +35,25 @@
RUBY
end

it 'registers an offense for multiline (two lines in a body) when statement with then' do
expect_offense(<<~RUBY)
case foo
when bar then
^^^^ Do not use `then` for multiline `when` statement.
do_something1
do_something2
end
RUBY

expect_correction(<<~RUBY)
case foo
when bar
do_something1
do_something2
end
RUBY
end

it "doesn't register an offense for singleline when statement with then" do
expect_no_offenses(<<~RUBY)
case foo
Expand Down