Skip to content

Commit

Permalink
[Fix rubocop#4283] Style/EmptyCaseCondition autocorrect bugfix
Browse files Browse the repository at this point in the history
When first `when` condition of an empty case included comma-delimited
alternatives, autocorrect did not replace the comma with `||`.
This commit fixes the issue.
  • Loading branch information
Ilan Shamir committed Apr 22, 2017
1 parent d1b6bed commit 2be8ccf
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* [#4241](https://github.com/bbatsov/rubocop/issues/4241): Prevent `Rails/Blank` and `Rails/Present` from breaking when there is no explicit receiver. ([@rrosenblum][])
* [#4249](https://github.com/bbatsov/rubocop/issues/4249): Handle multiple assignment in `Rails/RelativeDateConstant`. ([@bbatsov][])
* [#4250](https://github.com/bbatsov/rubocop/issues/4250): Improve a bit the Ruby code detection config. ([@bbatsov][])
* [#4283](https://github.com/bbatsov/rubocop/issues/4283): Fix `Style/EmptyCaseCondition` autocorrect bug - when first `when` branch includes comma-delimited alternatives. ([@ilansh][])
* [#4268](https://github.com/bbatsov/rubocop/issues/4268): Handle end-of-line comments when autocorrecting Style/EmptyLinesAroundAccessModifier. ([@vergenzt][])
* [#4275](https://github.com/bbatsov/rubocop/issues/4275): Prevent `Style/MethodCallWithArgsParentheses` from blowing up on `yield`. ([@drenmi][])
* [#3969](https://github.com/bbatsov/rubocop/issues/3969): Handle multiline method call alignment for arguments to methods. ([@jonas054][])
Expand Down Expand Up @@ -2755,3 +2756,4 @@
[@sadovnik]: https://github.com/sadovnik
[@cjlarose]: https://github.com/cjlarose
[@alpaca-tc]: https://github.com/alpaca-tc
[@ilansh]: https://github.com/ilansh
4 changes: 2 additions & 2 deletions lib/rubocop/cop/style/empty_case_condition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ def autocorrect(case_node)
end

def correct_case_when(corrector, case_node, when_nodes)
case_range = case_node.loc.keyword.join(when_nodes.shift.loc.keyword)
case_range = case_node.loc.keyword.join(when_nodes.first.loc.keyword)

corrector.replace(case_range, 'if')

when_nodes.each do |when_node|
when_nodes[1..-1].each do |when_node|
corrector.replace(when_node.loc.keyword, 'elsif')
end
end
Expand Down
24 changes: 24 additions & 0 deletions spec/rubocop/cop/style/empty_case_condition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,5 +167,29 @@

it_behaves_like 'detect/correct empty case, accept non-empty case'
end

context 'with first when branch including comma-delimited alternatives' do
let(:source) do
<<-END.strip_indent
case
when my.foo?, my.bar?
something
when my.baz?
something_else
end
END
end
let(:corrected_source) do
<<-END.strip_indent
if my.foo? || my.bar?
something
elsif my.baz?
something_else
end
END
end

it_behaves_like 'detect/correct empty case, accept non-empty case'
end
end
end

0 comments on commit 2be8ccf

Please sign in to comment.