Skip to content

Commit

Permalink
Merge pull request #484 from viralpraxis/fix-performance-case-when-sp…
Browse files Browse the repository at this point in the history
…lat-cop-error-on-when-without-body

Fix `Performance/CaseWhenSplat` cop error on `when` node without body
  • Loading branch information
koic authored Jan 22, 2025
2 parents 4e147b0 + a1a7c35 commit d178a01
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#484](https://github.com/rubocop/rubocop-performance/pull/484): Fix `Performance/CaseWhenSplat` cop error on `when` node without body. ([@viralpraxis][])
26 changes: 20 additions & 6 deletions lib/rubocop/cop/performance/case_when_splat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ module Performance
class CaseWhenSplat < Base
include Alignment
include RangeHelp
include CommentsHelp
extend AutoCorrector

MSG = 'Reordering `when` conditions with a splat to the end of the `when` branches can improve performance.'
Expand Down Expand Up @@ -116,11 +117,18 @@ def reorder_condition(corrector, when_node)
def reordering_correction(when_node)
new_condition = replacement(when_node.conditions)

if same_line?(when_node, when_node.body)
new_condition_with_then(when_node, new_condition)
else
new_branch_without_then(when_node, new_condition)
end
condition =
if same_line?(when_node, when_node.body)
new_condition_with_then(when_node, new_condition)
else
new_branch_without_then(when_node, new_condition)
end

condition_comments = comments_in_range(when_node).map do |comment_node|
"#{indent_for(comment_node)}#{comment_node.source}"
end.join("\n")

"#{condition}#{condition_comments}"
end

def when_branch_range(when_node)
Expand All @@ -134,7 +142,13 @@ def new_condition_with_then(node, new_condition)
end

def new_branch_without_then(node, new_condition)
"\n#{indent_for(node)}when #{new_condition}\n#{indent_for(node.body)}#{node.body.source}"
new_branch = "\n#{indent_for(node)}when #{new_condition}\n"

if node.body
"#{new_branch}#{indent_for(node.body)}#{node.body.source}"
else
new_branch
end
end

def indent_for(node)
Expand Down
Binary file added rubocop-performance-1.23.0.gem
Binary file not shown.
25 changes: 25 additions & 0 deletions spec/rubocop/cop/performance/case_when_splat_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,31 @@
RUBY
end

it 'registers an offense for case when with a splat in the first condition with branch without body' do
expect_offense(<<~RUBY)
case foo
when *cond
^^^^^^^^^^ Reordering `when` conditions with a splat to the end of the `when` branches can improve performance.
# bar
# bar
# bar
when 3
baz
end
RUBY

expect_correction(<<~RUBY)
case foo
when 3
baz
when *cond
# bar
# bar
# bar
end
RUBY
end

it 'registers an offense for case when with a splat without an else' do
expect_offense(<<~RUBY)
case foo
Expand Down

0 comments on commit d178a01

Please sign in to comment.