From 5e47d8eb67e9904615659405d8ad0aa4ce45128f Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Tue, 5 Mar 2024 08:01:15 -0500 Subject: [PATCH] Add constant path ripper translation --- lib/prism/translation/ripper.rb | 62 ++++++++++++++++++++++++++------- test/prism/ripper_test.rb | 8 ----- 2 files changed, 50 insertions(+), 20 deletions(-) diff --git a/lib/prism/translation/ripper.rb b/lib/prism/translation/ripper.rb index 6bbc8e286c6..e3cd3004460 100644 --- a/lib/prism/translation/ripper.rb +++ b/lib/prism/translation/ripper.rb @@ -738,35 +738,72 @@ def visit_constant_path_node(node) # Foo::Foo, Bar::Bar = 1 # ^^^^^^^^ ^^^^^^^^ def visit_constant_path_write_node(node) - parent = visit(node.target.parent) - - bounds(node.target.child.location) - child = on_const(node.target.child.name.to_s) - - bounds(node.target.location) - target = on_const_path_field(parent, child) + target = visit_constant_path_write_node_target(node.target) value = visit(node.value) bounds(node.location) on_assign(target, value) end + # Visit a constant path that is part of a write node. + private def visit_constant_path_write_node_target(node) + if node.parent.nil? + bounds(node.child.location) + child = on_const(node.child.name.to_s) + + bounds(node.location) + on_top_const_field(child) + else + parent = visit(node.parent) + + bounds(node.child.location) + child = on_const(node.child.name.to_s) + + bounds(node.location) + on_const_path_field(parent, child) + end + end + # Foo::Bar += baz # ^^^^^^^^^^^^^^^ def visit_constant_path_operator_write_node(node) - raise NoMethodError, __method__ + target = visit_constant_path_write_node_target(node.target) + value = visit(node.value) + + bounds(node.operator_loc) + operator = on_op("#{node.operator}=") + value = visit(node.value) + + bounds(node.location) + on_opassign(target, operator, value) end # Foo::Bar &&= baz # ^^^^^^^^^^^^^^^^ def visit_constant_path_and_write_node(node) - raise NoMethodError, __method__ + target = visit_constant_path_write_node_target(node.target) + value = visit(node.value) + + bounds(node.operator_loc) + operator = on_op("&&=") + value = visit(node.value) + + bounds(node.location) + on_opassign(target, operator, value) end # Foo::Bar ||= baz # ^^^^^^^^^^^^^^^^ def visit_constant_path_or_write_node(node) - raise NoMethodError, __method__ + target = visit_constant_path_write_node_target(node.target) + value = visit(node.value) + + bounds(node.operator_loc) + operator = on_op("||=") + value = visit(node.value) + + bounds(node.location) + on_opassign(target, operator, value) end # Foo::Bar, = baz @@ -925,7 +962,8 @@ def visit_forwarding_arguments_node(node) # def foo(...); end # ^^^ def visit_forwarding_parameter_node(node) - raise NoMethodError, __method__ + bounds(node.location) + on_args_forward end # super @@ -1522,7 +1560,7 @@ def visit_parameters_node(node) posts = visit_all(node.posts) if node.posts.any? keywords = visit_all(node.keywords) if node.keywords.any? keyword_rest = visit(node.keyword_rest) - block = visit(node.block) + block = node.keyword_rest.is_a?(ForwardingParameterNode) ? :& : visit(node.block) bounds(node.location) on_params(requireds, optionals, rest, posts, keywords, keyword_rest, block) diff --git a/test/prism/ripper_test.rb b/test/prism/ripper_test.rb index 9e19c5aaa62..a0b7d3a25f7 100644 --- a/test/prism/ripper_test.rb +++ b/test/prism/ripper_test.rb @@ -394,11 +394,6 @@ class RipperFixturesTest < RipperTestCase seattlerb/case_in_multiple.txt seattlerb/case_in_or.txt seattlerb/class_comments.txt - seattlerb/const_2_op_asgn_or2.txt - seattlerb/const_3_op_asgn_or.txt - seattlerb/const_op_asgn_and1.txt - seattlerb/const_op_asgn_and2.txt - seattlerb/const_op_asgn_or.txt seattlerb/defn_arg_forward_args.txt seattlerb/defn_args_forward_args.txt seattlerb/defn_endless_command.txt @@ -497,7 +492,6 @@ class RipperFixturesTest < RipperTestCase seattlerb/op_asgn_command_call.txt seattlerb/op_asgn_dot_ident_command_call.txt seattlerb/op_asgn_index_command_call.txt - seattlerb/op_asgn_primary_colon_const_command_call.txt seattlerb/op_asgn_primary_colon_identifier1.txt seattlerb/op_asgn_primary_colon_identifier_command_call.txt seattlerb/op_asgn_val_dot_ident_command_call.txt @@ -670,12 +664,10 @@ class RipperFixturesTest < RipperTestCase whitequark/bug_while_not_parens_do.txt whitequark/case_cond_else.txt whitequark/case_expr_else.txt - whitequark/casgn_toplevel.txt whitequark/character.txt whitequark/class_definition_in_while_cond.txt whitequark/cond_begin_masgn.txt whitequark/cond_match_current_line.txt - whitequark/const_op_asgn.txt whitequark/dedenting_heredoc.txt whitequark/dedenting_interpolating_heredoc_fake_line_continuation.txt whitequark/dedenting_non_interpolating_heredoc_line_continuation.txt