diff options
author | Kevin Newton <kddnewton@gmail.com> | 2024-03-06 00:08:10 -0500 |
---|---|---|
committer | git <svn-admin@ruby-lang.org> | 2024-03-06 16:42:57 +0000 |
commit | 409a49c832fce971cf506228b46d8ca1af35f81d (patch) | |
tree | 030aa8bad6a9fbf6c8ace430ced103607244bbde | |
parent | dad21783ac1bb7de4138bc18f93507abc64b2a0b (diff) |
[ruby/prism] Handle trailing commas in method calls for ripper translation
https://github.com/ruby/prism/commit/fe10b5f49f
-rw-r--r-- | lib/prism/translation/ripper.rb | 17 | ||||
-rw-r--r-- | test/prism/ripper_test.rb | 11 |
2 files changed, 11 insertions, 17 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?(/[;#]/) diff --git a/test/prism/ripper_test.rb b/test/prism/ripper_test.rb index 9095ab3f99..9d5eaf2b91 100644 --- a/test/prism/ripper_test.rb +++ b/test/prism/ripper_test.rb @@ -51,13 +51,9 @@ module Prism 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 @@ -76,13 +72,9 @@ module Prism 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 @@ -117,9 +109,6 @@ module Prism 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 |