Skip to content

Commit

Permalink
Handle combined conditions, else clauses in case statements (#746)
Browse files Browse the repository at this point in the history
  • Loading branch information
apiology authored Feb 20, 2025
1 parent 968bccb commit 3fa5602
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
6 changes: 4 additions & 2 deletions lib/solargraph/parser/legacy/node_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,11 @@ def get_return_nodes node
node.children[1..-1].each do |cc|
if cc.nil?
result.push NIL_NODE
else
result.concat reduce_to_value_nodes(cc.children[1..-2]) unless cc.children.length < 1
elsif cc.type == :when
result.concat reduce_to_value_nodes([cc.children.last])
else
# else clause in case
result.concat reduce_to_value_nodes([cc])
end
end
else
Expand Down
17 changes: 14 additions & 3 deletions spec/parser/node_methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,25 @@
it 'handles return nodes from case statements with else' do
node = Solargraph::Parser.parse(%(
case x
when 100
when 100, 125
true
else
when 500
73
when 23
false
when 12
nil
else
if 1 == 2
false
else
true
end
end
))
returns = Solargraph::Parser::NodeMethods.returns_from(node)
expect(returns.length).to eq(2)
expect(returns.length).to eq(6)
expect(returns.map(&:to_s)).to eq(['(true)', '(int 73)', '(false)', '(nil)', '(false)', '(true)'])
end

it 'handles return nodes from case statements with boolean conditions' do
Expand Down

0 comments on commit 3fa5602

Please sign in to comment.