Skip to content

Commit

Permalink
Merge pull request #7622 from koic/fix_an_incorrect_autocorrect_for_m…
Browse files Browse the repository at this point in the history
…ultiline_when_then

[Fix #7616] Fix an incorrect autocorrect for `Style/MultilineWhenThen`
  • Loading branch information
koic authored Jan 4, 2020
2 parents 81c93c6 + 0ac1450 commit c3235a6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* [#7607](https://github.com/rubocop-hq/rubocop/issues/7607): Fix `Style/FrozenStringLiteralComment` infinite loop when magic comments are newline-separated. ([@pirj][])
* [#7602](https://github.com/rubocop-hq/rubocop/pull/7602): Ensure proper handling of Ruby 2.7 syntax. ([@drenmi][])
* [#7620](https://github.com/rubocop-hq/rubocop/issues/7620): Fix a false positive for `Migration/DepartmentName` when a disable comment contains a plain comment. ([@koic][])
* [#7616](https://github.com/rubocop-hq/rubocop/issues/7616): Fix an incorrect autocorrect for `Style/MultilineWhenThen` for when statement with then is an array or a hash. ([@koic][])

### Changes

Expand Down
6 changes: 5 additions & 1 deletion lib/rubocop/cop/style/multiline_when_then.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def on_when(node)
return if !node.children.last.nil? && !node.multiline? && node.then?

# With more than one statements after then, there's not offense
return if node.children.last&.begin_type?
return if accept_node_type?(node.body)

add_offense(node, location: :begin)
end
Expand All @@ -49,6 +49,10 @@ def autocorrect(node)
)
end
end

def accept_node_type?(node)
node&.begin_type? || node&.array_type? || node&.hash_type?
end
end
end
end
Expand Down
20 changes: 20 additions & 0 deletions spec/rubocop/cop/style/multiline_when_then_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,26 @@
RUBY
end

it 'does not register an offense for hash when statement with then' do
expect_no_offenses(<<~RUBY)
case condition
when foo then {
key: 'value'
}
end
RUBY
end

it 'does not register an offense for array when statement with then' do
expect_no_offenses(<<~RUBY)
case condition
when foo then [
'element'
]
end
RUBY
end

it 'autocorrects then in empty when' do
new_source = autocorrect_source(<<~RUBY)
case foo
Expand Down

0 comments on commit c3235a6

Please sign in to comment.