[ruby/prism] Handle trailing commas in method calls for ripper translation

fe10b5f49f
This commit is contained in:
Kevin Newton 2024-03-06 00:08:10 -05:00 committed by git
parent dad21783ac
commit 409a49c832
2 changed files with 11 additions and 17 deletions

View file

@ -554,7 +554,7 @@ module Prism
case node.name case node.name
when :[] when :[]
receiver = visit(node.receiver) 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) bounds(node.location)
call = on_aref(receiver, arguments) call = on_aref(receiver, arguments)
@ -612,7 +612,7 @@ module Prism
if node.variable_call? if node.variable_call?
on_vcall(message) on_vcall(message)
else 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 = call =
if node.opening_loc.nil? && (arguments&.any? || block.nil?) if node.opening_loc.nil? && (arguments&.any? || block.nil?)
bounds(node.location) bounds(node.location)
@ -653,7 +653,7 @@ module Prism
bounds(node.location) bounds(node.location)
on_assign(on_field(receiver, call_operator, message), value) on_assign(on_field(receiver, call_operator, message), value)
else 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 = call =
if node.opening_loc.nil? if node.opening_loc.nil?
bounds(node.location) bounds(node.location)
@ -683,7 +683,7 @@ module Prism
# Visit the arguments and block of a call node and return the arguments # Visit the arguments and block of a call node and return the arguments
# and block as they should be used. # 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 || [] arguments = arguments_node&.arguments || []
block = block_node block = block_node
@ -698,7 +698,7 @@ module Prism
elsif arguments.any? elsif arguments.any?
args = visit_arguments(arguments) 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 args
else else
bounds(arguments.first.location) bounds(arguments.first.location)
@ -2479,7 +2479,7 @@ module Prism
# super(foo) # super(foo)
# ^^^^^^^^^^ # ^^^^^^^^^^
def visit_super_node(node) 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? if !node.lparen_loc.nil?
bounds(node.lparen_loc) bounds(node.lparen_loc)
@ -2696,6 +2696,11 @@ module Prism
# Helpers # 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. # Returns true if there is a semicolon between the two locations.
def void_stmt?(left, right) def void_stmt?(left, right)
source.byteslice(left.end_offset...right.start_offset).match?(/[;#]/) source.byteslice(left.end_offset...right.start_offset).match?(/[;#]/)

View file

@ -51,13 +51,9 @@ module Prism
seattlerb/block_command_operation_dot.txt seattlerb/block_command_operation_dot.txt
seattlerb/block_next.txt seattlerb/block_next.txt
seattlerb/block_return.txt seattlerb/block_return.txt
seattlerb/bug_hash_args_trailing_comma.txt
seattlerb/bug_hash_interp_array.txt seattlerb/bug_hash_interp_array.txt
seattlerb/call_args_assoc_trailing_comma.txt
seattlerb/call_args_command.txt seattlerb/call_args_command.txt
seattlerb/call_array_lambda_block_call.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/defn_oneliner_eq2.txt
seattlerb/defs_oneliner_eq2.txt seattlerb/defs_oneliner_eq2.txt
seattlerb/difficult3_5.txt seattlerb/difficult3_5.txt
@ -76,13 +72,9 @@ module Prism
seattlerb/lambda_do_vs_brace.txt seattlerb/lambda_do_vs_brace.txt
seattlerb/masgn_arg_colon_arg.txt seattlerb/masgn_arg_colon_arg.txt
seattlerb/masgn_double_paren.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_escaped_newline.txt
seattlerb/parse_line_dstr_soft_newline.txt seattlerb/parse_line_dstr_soft_newline.txt
seattlerb/parse_line_evstr_after_break.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_051.txt
seattlerb/parse_pattern_058.txt seattlerb/parse_pattern_058.txt
seattlerb/return_call_assocs.txt seattlerb/return_call_assocs.txt
@ -117,9 +109,6 @@ module Prism
until.txt until.txt
variables.txt variables.txt
while.txt while.txt
whitequark/args_args_assocs_comma.txt
whitequark/args_args_comma.txt
whitequark/args_assocs_comma.txt
whitequark/args_cmd.txt whitequark/args_cmd.txt
whitequark/asgn_mrhs.txt whitequark/asgn_mrhs.txt
whitequark/bug_480.txt whitequark/bug_480.txt