diff options
author | Kevin Newton <kddnewton@gmail.com> | 2024-03-05 07:21:05 -0500 |
---|---|---|
committer | git <svn-admin@ruby-lang.org> | 2024-03-06 16:42:40 +0000 |
commit | 139fe4fc63d8488fe44f3794d4fcd0bc5e0f862a (patch) | |
tree | 76d88859bf72b2b12329894103309f3eca3d2464 /lib/prism/translation/ripper.rb | |
parent | 70764bd0bef3cb95f12779e0237a45751d9f57ee (diff) |
[ruby/prism] Implement case/when for ripper translation
https://github.com/ruby/prism/commit/dfa499d2e9
Diffstat (limited to 'lib/prism/translation/ripper.rb')
-rw-r--r-- | lib/prism/translation/ripper.rb | 23 |
1 files changed, 21 insertions, 2 deletions
diff --git a/lib/prism/translation/ripper.rb b/lib/prism/translation/ripper.rb index fbd90f8690..3670c330bc 100644 --- a/lib/prism/translation/ripper.rb +++ b/lib/prism/translation/ripper.rb @@ -526,7 +526,14 @@ module Prism # case foo; when bar; end # ^^^^^^^^^^^^^^^^^^^^^^^ def visit_case_node(node) - raise NoMethodError, __method__ + predicate = visit(node.predicate) + clauses = + node.conditions.reverse_each.inject(nil) do |consequent, condition| + on_when(*visit(condition), consequent) + end + + bounds(node.location) + on_case(predicate, clauses) end # case foo; in bar; end @@ -1846,7 +1853,19 @@ module Prism # case foo; when bar; end # ^^^^^^^^^^^^^ def visit_when_node(node) - raise NoMethodError, __method__ + # This is a special case where we're not going to call on_when directly + # because we don't have access to the consequent. Instead, we'll return + # the component parts and let the parent node handle it. + conditions = visit_array_node_elements(node.conditions) + statements = + if node.statements.nil? + bounds(node.location) + on_stmts_add(on_stmts_new, on_void_stmt) + else + visit(node.statements) + end + + [conditions, statements] end # while foo; bar end |