summaryrefslogtreecommitdiff
path: root/lib/prism/translation/ripper.rb
diff options
context:
space:
mode:
authorKevin Newton <kddnewton@gmail.com>2024-03-06 00:08:10 -0500
committergit <svn-admin@ruby-lang.org>2024-03-06 16:42:57 +0000
commit409a49c832fce971cf506228b46d8ca1af35f81d (patch)
tree030aa8bad6a9fbf6c8ace430ced103607244bbde /lib/prism/translation/ripper.rb
parentdad21783ac1bb7de4138bc18f93507abc64b2a0b (diff)
[ruby/prism] Handle trailing commas in method calls for ripper translation
https://github.com/ruby/prism/commit/fe10b5f49f
Diffstat (limited to 'lib/prism/translation/ripper.rb')
-rw-r--r--lib/prism/translation/ripper.rb17
1 files changed, 11 insertions, 6 deletions
diff --git a/lib/prism/translation/ripper.rb b/lib/prism/translation/ripper.rb
index addeb38fdd..d96df18010 100644
--- a/lib/prism/translation/ripper.rb
+++ b/lib/prism/translation/ripper.rb
@@ -554,7 +554,7 @@ module Prism
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)
@@ -612,7 +612,7 @@ module Prism
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)
@@ -653,7 +653,7 @@ module Prism
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)
@@ -683,7 +683,7 @@ module Prism
# 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
@@ -698,7 +698,7 @@ module Prism
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)
@@ -2479,7 +2479,7 @@ module Prism
# 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)
@@ -2696,6 +2696,11 @@ module Prism
# 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?(/[;#]/)