Skip to content

Commit 5e47d8e

Browse files
committed
Add constant path ripper translation
1 parent a3c8c27 commit 5e47d8e

File tree

2 files changed

+50
-20
lines changed

2 files changed

+50
-20
lines changed

lib/prism/translation/ripper.rb

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -738,35 +738,72 @@ def visit_constant_path_node(node)
738738
# Foo::Foo, Bar::Bar = 1
739739
# ^^^^^^^^ ^^^^^^^^
740740
def visit_constant_path_write_node(node)
741-
parent = visit(node.target.parent)
742-
743-
bounds(node.target.child.location)
744-
child = on_const(node.target.child.name.to_s)
745-
746-
bounds(node.target.location)
747-
target = on_const_path_field(parent, child)
741+
target = visit_constant_path_write_node_target(node.target)
748742
value = visit(node.value)
749743

750744
bounds(node.location)
751745
on_assign(target, value)
752746
end
753747

748+
# Visit a constant path that is part of a write node.
749+
private def visit_constant_path_write_node_target(node)
750+
if node.parent.nil?
751+
bounds(node.child.location)
752+
child = on_const(node.child.name.to_s)
753+
754+
bounds(node.location)
755+
on_top_const_field(child)
756+
else
757+
parent = visit(node.parent)
758+
759+
bounds(node.child.location)
760+
child = on_const(node.child.name.to_s)
761+
762+
bounds(node.location)
763+
on_const_path_field(parent, child)
764+
end
765+
end
766+
754767
# Foo::Bar += baz
755768
# ^^^^^^^^^^^^^^^
756769
def visit_constant_path_operator_write_node(node)
757-
raise NoMethodError, __method__
770+
target = visit_constant_path_write_node_target(node.target)
771+
value = visit(node.value)
772+
773+
bounds(node.operator_loc)
774+
operator = on_op("#{node.operator}=")
775+
value = visit(node.value)
776+
777+
bounds(node.location)
778+
on_opassign(target, operator, value)
758779
end
759780

760781
# Foo::Bar &&= baz
761782
# ^^^^^^^^^^^^^^^^
762783
def visit_constant_path_and_write_node(node)
763-
raise NoMethodError, __method__
784+
target = visit_constant_path_write_node_target(node.target)
785+
value = visit(node.value)
786+
787+
bounds(node.operator_loc)
788+
operator = on_op("&&=")
789+
value = visit(node.value)
790+
791+
bounds(node.location)
792+
on_opassign(target, operator, value)
764793
end
765794

766795
# Foo::Bar ||= baz
767796
# ^^^^^^^^^^^^^^^^
768797
def visit_constant_path_or_write_node(node)
769-
raise NoMethodError, __method__
798+
target = visit_constant_path_write_node_target(node.target)
799+
value = visit(node.value)
800+
801+
bounds(node.operator_loc)
802+
operator = on_op("||=")
803+
value = visit(node.value)
804+
805+
bounds(node.location)
806+
on_opassign(target, operator, value)
770807
end
771808

772809
# Foo::Bar, = baz
@@ -925,7 +962,8 @@ def visit_forwarding_arguments_node(node)
925962
# def foo(...); end
926963
# ^^^
927964
def visit_forwarding_parameter_node(node)
928-
raise NoMethodError, __method__
965+
bounds(node.location)
966+
on_args_forward
929967
end
930968

931969
# super
@@ -1522,7 +1560,7 @@ def visit_parameters_node(node)
15221560
posts = visit_all(node.posts) if node.posts.any?
15231561
keywords = visit_all(node.keywords) if node.keywords.any?
15241562
keyword_rest = visit(node.keyword_rest)
1525-
block = visit(node.block)
1563+
block = node.keyword_rest.is_a?(ForwardingParameterNode) ? :& : visit(node.block)
15261564

15271565
bounds(node.location)
15281566
on_params(requireds, optionals, rest, posts, keywords, keyword_rest, block)

test/prism/ripper_test.rb

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -394,11 +394,6 @@ class RipperFixturesTest < RipperTestCase
394394
seattlerb/case_in_multiple.txt
395395
seattlerb/case_in_or.txt
396396
seattlerb/class_comments.txt
397-
seattlerb/const_2_op_asgn_or2.txt
398-
seattlerb/const_3_op_asgn_or.txt
399-
seattlerb/const_op_asgn_and1.txt
400-
seattlerb/const_op_asgn_and2.txt
401-
seattlerb/const_op_asgn_or.txt
402397
seattlerb/defn_arg_forward_args.txt
403398
seattlerb/defn_args_forward_args.txt
404399
seattlerb/defn_endless_command.txt
@@ -497,7 +492,6 @@ class RipperFixturesTest < RipperTestCase
497492
seattlerb/op_asgn_command_call.txt
498493
seattlerb/op_asgn_dot_ident_command_call.txt
499494
seattlerb/op_asgn_index_command_call.txt
500-
seattlerb/op_asgn_primary_colon_const_command_call.txt
501495
seattlerb/op_asgn_primary_colon_identifier1.txt
502496
seattlerb/op_asgn_primary_colon_identifier_command_call.txt
503497
seattlerb/op_asgn_val_dot_ident_command_call.txt
@@ -670,12 +664,10 @@ class RipperFixturesTest < RipperTestCase
670664
whitequark/bug_while_not_parens_do.txt
671665
whitequark/case_cond_else.txt
672666
whitequark/case_expr_else.txt
673-
whitequark/casgn_toplevel.txt
674667
whitequark/character.txt
675668
whitequark/class_definition_in_while_cond.txt
676669
whitequark/cond_begin_masgn.txt
677670
whitequark/cond_match_current_line.txt
678-
whitequark/const_op_asgn.txt
679671
whitequark/dedenting_heredoc.txt
680672
whitequark/dedenting_interpolating_heredoc_fake_line_continuation.txt
681673
whitequark/dedenting_non_interpolating_heredoc_line_continuation.txt

0 commit comments

Comments
 (0)