Skip to content

Commit

Permalink
Handle trailing commas in method calls for ripper translation
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton committed Mar 6, 2024
1 parent 4639803 commit fe10b5f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 17 deletions.
17 changes: 11 additions & 6 deletions lib/prism/translation/ripper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ def visit_call_node(node)
case node.name
when :[]
receiver = visit(node.receiver)
arguments, block = visit_call_node_arguments(node.arguments, node.block)
arguments, block = visit_call_node_arguments(node.arguments, node.block, trailing_comma?(node.arguments&.location || node.location, node.closing_loc))

bounds(node.location)
call = on_aref(receiver, arguments)
Expand Down Expand Up @@ -612,7 +612,7 @@ def visit_call_node(node)
if node.variable_call?
on_vcall(message)
else
arguments, block = visit_call_node_arguments(node.arguments, node.block)
arguments, block = visit_call_node_arguments(node.arguments, node.block, trailing_comma?(node.arguments&.location || node.location, node.closing_loc || node.location))
call =
if node.opening_loc.nil? && (arguments&.any? || block.nil?)
bounds(node.location)
Expand Down Expand Up @@ -653,7 +653,7 @@ def visit_call_node(node)
bounds(node.location)
on_assign(on_field(receiver, call_operator, message), value)
else
arguments, block = visit_call_node_arguments(node.arguments, node.block)
arguments, block = visit_call_node_arguments(node.arguments, node.block, trailing_comma?(node.arguments&.location || node.location, node.closing_loc || node.location))
call =
if node.opening_loc.nil?
bounds(node.location)
Expand Down Expand Up @@ -683,7 +683,7 @@ def visit_call_node(node)

# Visit the arguments and block of a call node and return the arguments
# and block as they should be used.
private def visit_call_node_arguments(arguments_node, block_node)
private def visit_call_node_arguments(arguments_node, block_node, trailing_comma)
arguments = arguments_node&.arguments || []
block = block_node

Expand All @@ -698,7 +698,7 @@ def visit_call_node(node)
elsif arguments.any?
args = visit_arguments(arguments)

if block_node.is_a?(BlockArgumentNode) || arguments.last.is_a?(ForwardingArgumentsNode)
if block_node.is_a?(BlockArgumentNode) || arguments.last.is_a?(ForwardingArgumentsNode) || trailing_comma
args
else
bounds(arguments.first.location)
Expand Down Expand Up @@ -2479,7 +2479,7 @@ def visit_string_node(node)
# super(foo)
# ^^^^^^^^^^
def visit_super_node(node)
arguments, block = visit_call_node_arguments(node.arguments, node.block)
arguments, block = visit_call_node_arguments(node.arguments, node.block, trailing_comma?(node.arguments&.location || node.location, node.rparen_loc || node.location))

if !node.lparen_loc.nil?
bounds(node.lparen_loc)
Expand Down Expand Up @@ -2696,6 +2696,11 @@ def result
# Helpers
##########################################################################

# Returns true if there is a comma between the two locations.
def trailing_comma?(left, right)
source.byteslice(left.end_offset...right.start_offset).include?(",")
end

# Returns true if there is a semicolon between the two locations.
def void_stmt?(left, right)
source.byteslice(left.end_offset...right.start_offset).match?(/[;#]/)
Expand Down
11 changes: 0 additions & 11 deletions test/prism/ripper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,9 @@ class RipperTest < TestCase
seattlerb/block_command_operation_dot.txt
seattlerb/block_next.txt
seattlerb/block_return.txt
seattlerb/bug_hash_args_trailing_comma.txt
seattlerb/bug_hash_interp_array.txt
seattlerb/call_args_assoc_trailing_comma.txt
seattlerb/call_args_command.txt
seattlerb/call_array_lambda_block_call.txt
seattlerb/call_assoc_trailing_comma.txt
seattlerb/call_trailing_comma.txt
seattlerb/defn_oneliner_eq2.txt
seattlerb/defs_oneliner_eq2.txt
seattlerb/difficult3_5.txt
Expand All @@ -76,13 +72,9 @@ class RipperTest < TestCase
seattlerb/lambda_do_vs_brace.txt
seattlerb/masgn_arg_colon_arg.txt
seattlerb/masgn_double_paren.txt
seattlerb/method_call_assoc_trailing_comma.txt
seattlerb/method_call_trailing_comma.txt
seattlerb/parse_line_dstr_escaped_newline.txt
seattlerb/parse_line_dstr_soft_newline.txt
seattlerb/parse_line_evstr_after_break.txt
seattlerb/parse_opt_call_args_assocs_comma.txt
seattlerb/parse_opt_call_args_lit_comma.txt
seattlerb/parse_pattern_051.txt
seattlerb/parse_pattern_058.txt
seattlerb/return_call_assocs.txt
Expand Down Expand Up @@ -117,9 +109,6 @@ class RipperTest < TestCase
until.txt
variables.txt
while.txt
whitequark/args_args_assocs_comma.txt
whitequark/args_args_comma.txt
whitequark/args_assocs_comma.txt
whitequark/args_cmd.txt
whitequark/asgn_mrhs.txt
whitequark/bug_480.txt
Expand Down

0 comments on commit fe10b5f

Please sign in to comment.