Skip to content

Commit fe10b5f

Browse files
committed
Handle trailing commas in method calls for ripper translation
1 parent 4639803 commit fe10b5f

File tree

2 files changed

+11
-17
lines changed

2 files changed

+11
-17
lines changed

lib/prism/translation/ripper.rb

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ def visit_call_node(node)
554554
case node.name
555555
when :[]
556556
receiver = visit(node.receiver)
557-
arguments, block = visit_call_node_arguments(node.arguments, node.block)
557+
arguments, block = visit_call_node_arguments(node.arguments, node.block, trailing_comma?(node.arguments&.location || node.location, node.closing_loc))
558558

559559
bounds(node.location)
560560
call = on_aref(receiver, arguments)
@@ -612,7 +612,7 @@ def visit_call_node(node)
612612
if node.variable_call?
613613
on_vcall(message)
614614
else
615-
arguments, block = visit_call_node_arguments(node.arguments, node.block)
615+
arguments, block = visit_call_node_arguments(node.arguments, node.block, trailing_comma?(node.arguments&.location || node.location, node.closing_loc || node.location))
616616
call =
617617
if node.opening_loc.nil? && (arguments&.any? || block.nil?)
618618
bounds(node.location)
@@ -653,7 +653,7 @@ def visit_call_node(node)
653653
bounds(node.location)
654654
on_assign(on_field(receiver, call_operator, message), value)
655655
else
656-
arguments, block = visit_call_node_arguments(node.arguments, node.block)
656+
arguments, block = visit_call_node_arguments(node.arguments, node.block, trailing_comma?(node.arguments&.location || node.location, node.closing_loc || node.location))
657657
call =
658658
if node.opening_loc.nil?
659659
bounds(node.location)
@@ -683,7 +683,7 @@ def visit_call_node(node)
683683

684684
# Visit the arguments and block of a call node and return the arguments
685685
# and block as they should be used.
686-
private def visit_call_node_arguments(arguments_node, block_node)
686+
private def visit_call_node_arguments(arguments_node, block_node, trailing_comma)
687687
arguments = arguments_node&.arguments || []
688688
block = block_node
689689

@@ -698,7 +698,7 @@ def visit_call_node(node)
698698
elsif arguments.any?
699699
args = visit_arguments(arguments)
700700

701-
if block_node.is_a?(BlockArgumentNode) || arguments.last.is_a?(ForwardingArgumentsNode)
701+
if block_node.is_a?(BlockArgumentNode) || arguments.last.is_a?(ForwardingArgumentsNode) || trailing_comma
702702
args
703703
else
704704
bounds(arguments.first.location)
@@ -2479,7 +2479,7 @@ def visit_string_node(node)
24792479
# super(foo)
24802480
# ^^^^^^^^^^
24812481
def visit_super_node(node)
2482-
arguments, block = visit_call_node_arguments(node.arguments, node.block)
2482+
arguments, block = visit_call_node_arguments(node.arguments, node.block, trailing_comma?(node.arguments&.location || node.location, node.rparen_loc || node.location))
24832483

24842484
if !node.lparen_loc.nil?
24852485
bounds(node.lparen_loc)
@@ -2696,6 +2696,11 @@ def result
26962696
# Helpers
26972697
##########################################################################
26982698

2699+
# Returns true if there is a comma between the two locations.
2700+
def trailing_comma?(left, right)
2701+
source.byteslice(left.end_offset...right.start_offset).include?(",")
2702+
end
2703+
26992704
# Returns true if there is a semicolon between the two locations.
27002705
def void_stmt?(left, right)
27012706
source.byteslice(left.end_offset...right.start_offset).match?(/[;#]/)

test/prism/ripper_test.rb

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,9 @@ class RipperTest < TestCase
5151
seattlerb/block_command_operation_dot.txt
5252
seattlerb/block_next.txt
5353
seattlerb/block_return.txt
54-
seattlerb/bug_hash_args_trailing_comma.txt
5554
seattlerb/bug_hash_interp_array.txt
56-
seattlerb/call_args_assoc_trailing_comma.txt
5755
seattlerb/call_args_command.txt
5856
seattlerb/call_array_lambda_block_call.txt
59-
seattlerb/call_assoc_trailing_comma.txt
60-
seattlerb/call_trailing_comma.txt
6157
seattlerb/defn_oneliner_eq2.txt
6258
seattlerb/defs_oneliner_eq2.txt
6359
seattlerb/difficult3_5.txt
@@ -76,13 +72,9 @@ class RipperTest < TestCase
7672
seattlerb/lambda_do_vs_brace.txt
7773
seattlerb/masgn_arg_colon_arg.txt
7874
seattlerb/masgn_double_paren.txt
79-
seattlerb/method_call_assoc_trailing_comma.txt
80-
seattlerb/method_call_trailing_comma.txt
8175
seattlerb/parse_line_dstr_escaped_newline.txt
8276
seattlerb/parse_line_dstr_soft_newline.txt
8377
seattlerb/parse_line_evstr_after_break.txt
84-
seattlerb/parse_opt_call_args_assocs_comma.txt
85-
seattlerb/parse_opt_call_args_lit_comma.txt
8678
seattlerb/parse_pattern_051.txt
8779
seattlerb/parse_pattern_058.txt
8880
seattlerb/return_call_assocs.txt
@@ -117,9 +109,6 @@ class RipperTest < TestCase
117109
until.txt
118110
variables.txt
119111
while.txt
120-
whitequark/args_args_assocs_comma.txt
121-
whitequark/args_args_comma.txt
122-
whitequark/args_assocs_comma.txt
123112
whitequark/args_cmd.txt
124113
whitequark/asgn_mrhs.txt
125114
whitequark/bug_480.txt

0 commit comments

Comments
 (0)