From 93d06436f76af97cc8bc03cb223db063d2964b43 Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Tue, 5 Mar 2024 07:13:05 -0500 Subject: [PATCH] Implement module for ripper translation --- lib/prism/translation/ripper.rb | 48 +++++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 11 deletions(-) diff --git a/lib/prism/translation/ripper.rb b/lib/prism/translation/ripper.rb index 98dde077149..d6846ed8b57 100644 --- a/lib/prism/translation/ripper.rb +++ b/lib/prism/translation/ripper.rb @@ -969,20 +969,18 @@ def visit_global_variable_target_node(node) # {} # ^^ def visit_hash_node(node) - elements = visit_hash_node_elements(node.elements) unless node.elements.empty? + elements = + if node.elements.any? + args = visit_all(node.elements) + + bounds(node.elements.first.location) + on_assoclist_from_args(args) + end bounds(node.location) on_hash(elements) end - # Visit the elements of a hash node. - private def visit_hash_node_elements(elements) - args = visit_all(elements) - - bounds(elements.first.location) - on_assoclist_from_args(args) - end - # foo => {} # ^^ def visit_hash_pattern_node(node) @@ -1196,7 +1194,10 @@ def visit_it_parameters_node(node) # foo(bar: baz) # ^^^^^^^^ def visit_keyword_hash_node(node) - raise NoMethodError, __method__ + elements = visit_all(node.elements) + + bounds(node.location) + on_bare_assoc_hash(elements) end # def foo(**bar); end @@ -1333,7 +1334,32 @@ def visit_missing_node(node) # module Foo; end # ^^^^^^^^^^^^^^^ def visit_module_node(node) - raise NoMethodError, __method__ + constant_path = + if node.constant_path.is_a?(ConstantReadNode) + bounds(node.constant_path.location) + on_const_ref(on_const(node.constant_path.name.to_s)) + else + visit(node.constant_path) + end + + bodystmt = + case node.body + when nil + bounds(node.location) + on_bodystmt(visit_statements_node_body([nil]), nil, nil, nil) + when StatementsNode + body = visit(node.body) + + bounds(node.body.location) + on_bodystmt(body, nil, nil, nil) + when BeginNode + visit_begin_node_clauses(node.body) + else + raise + end + + bounds(node.location) + on_module(constant_path, bodystmt) end # foo, bar = baz