Skip to content

Commit

Permalink
Add constant path ripper translation
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton committed Mar 5, 2024
1 parent a3c8c27 commit 5e47d8e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 20 deletions.
62 changes: 50 additions & 12 deletions lib/prism/translation/ripper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 0 additions & 8 deletions test/prism/ripper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5e47d8e

Please sign in to comment.