diff options
author | Kevin Newton <kddnewton@gmail.com> | 2024-03-05 16:38:48 -0500 |
---|---|---|
committer | git <svn-admin@ruby-lang.org> | 2024-03-06 16:42:45 +0000 |
commit | 47daf71ac0c321072083b2bfd64b851cbe0d1d71 (patch) | |
tree | 3d21dee1f0ce025b3ea6777b698a1d46d9b416c4 /lib/prism/translation/ripper.rb | |
parent | 53efae0d632480d420f85dabfdc6ea5293536ef5 (diff) |
[ruby/prism] Implement super for ripper translation
https://github.com/ruby/prism/commit/18dea6ce64
Diffstat (limited to 'lib/prism/translation/ripper.rb')
-rw-r--r-- | lib/prism/translation/ripper.rb | 159 |
1 files changed, 111 insertions, 48 deletions
diff --git a/lib/prism/translation/ripper.rb b/lib/prism/translation/ripper.rb index 8ab7b4a6aa..6b824255a0 100644 --- a/lib/prism/translation/ripper.rb +++ b/lib/prism/translation/ripper.rb @@ -239,17 +239,28 @@ module Prism # [] # ^^ def visit_array_node(node) - elements = visit_array_node_elements(node.elements) unless node.elements.empty? + elements = visit_arguments(node.elements) unless node.elements.empty? bounds(node.location) on_array(elements) end # Visit a list of elements, like the elements of an array or arguments. - private def visit_array_node_elements(elements) + private def visit_arguments(elements) bounds(elements.first.location) + elements.inject(on_args_new) do |args, element| - on_args_add(args, visit(element)) + arg = visit(element) + bounds(element.location) + + case element + when BlockArgumentNode + on_args_add_block(args, arg) + when SplatNode + on_args_add_star(args, arg) + else + on_args_add(args, arg) + end end end @@ -277,7 +288,7 @@ module Prism # ^^^ def visit_arguments_node(node) bounds(node.location) - on_args_add_block(visit_array_node_elements(node.arguments), false) + on_args_add_block(visit_arguments(node.arguments), false) end # { a: 1 } @@ -349,7 +360,7 @@ module Prism # foo(&bar) # ^^^^ def visit_block_argument_node(node) - raise NoMethodError, __method__ + visit(node.expression) end # foo { |; bar| } @@ -442,7 +453,7 @@ module Prism bounds(node.location) on_break(on_args_new) else - arguments = visit_array_node_elements(node.arguments.arguments) + arguments = visit_arguments(node.arguments.arguments) bounds(node.location) on_break(on_args_add_block(arguments, false)) @@ -492,7 +503,22 @@ module Prism # foo.bar += baz # ^^^^^^^^^^^^^^^ def visit_call_operator_write_node(node) - raise NoMethodError, __method__ + receiver = visit(node.receiver) + + bounds(node.call_operator_loc) + call_operator = visit_token(node.call_operator) + + bounds(node.message_loc) + message = visit_token(node.message) + + bounds(node.location) + target = on_field(receiver, call_operator, message) + + bounds(node.operator_loc) + operator = on_op("#{node.operator}=") + + value = visit(node.value) + on_opassign(target, operator, value) end # foo.bar &&= baz @@ -540,7 +566,16 @@ module Prism # foo.bar, = 1 # ^^^^^^^ def visit_call_target_node(node) - raise NoMethodError, __method__ + receiver = visit(node.receiver) + + bounds(node.call_operator_loc) + call_operator = visit_token(node.call_operator) + + bounds(node.message_loc) + message = visit_token(node.message) + + bounds(node.location) + on_field(receiver, call_operator, message) end # foo => bar => baz @@ -1709,7 +1744,7 @@ module Prism bounds(node.location) on_next(on_args_new) else - arguments = visit_array_node_elements(node.arguments.arguments) + arguments = visit_arguments(node.arguments.arguments) bounds(node.location) on_next(on_args_add_block(arguments, false)) @@ -1922,42 +1957,41 @@ module Prism # begin; rescue; end # ^^^^^^^ def visit_rescue_node(node) - consequent_val = nil - if node.consequent - consequent_val = visit(node.consequent) - end - - if node.statements - stmts_val = visit(node.statements) - else - stmts_val = on_stmts_add(on_stmts_new, on_void_stmt) - end - - if node.reference - raise NoMethodError, __method__ unless node.reference.is_a?(LocalVariableTargetNode) - bounds(node.reference.location) - ref_val = on_var_field(on_ident(node.reference.name.to_s)) - else - ref_val = nil - end + exceptions = + case node.exceptions.length + when 0 + nil + when 1 + [visit(node.exceptions.first)] + else + bounds(node.location) + length = node.exceptions.length - # No exception(s) - if !node.exceptions || node.exceptions.empty? - return on_rescue(nil, ref_val, stmts_val, consequent_val) - end + node.exceptions.each_with_index.inject(on_args_new) do |mrhs, (exception, index)| + arg = visit(exception) + bounds(exception.location) - exc_vals = node.exceptions.map { |exc| visit(exc) } + if index == length - 1 + on_mrhs_add(on_mrhs_new_from_args(mrhs), arg) + else + on_args_add(mrhs, arg) + end + end + end - if node.exceptions.length == 1 - return on_rescue(exc_vals, ref_val, stmts_val, consequent_val) - end + reference = visit(node.reference) + statements = + if node.statements.nil? + bounds(node.location) + on_stmts_add(on_stmts_new, on_void_stmt) + else + visit(node.statements) + end - inner_vals = exc_vals[0..-2].inject(on_args_new) do |output, exc_val| - on_args_add(output, exc_val) - end - exc_vals = on_mrhs_add(on_mrhs_new_from_args(inner_vals), exc_vals[-1]) + consequent = visit(node.consequent) - on_rescue(exc_vals, ref_val, stmts_val, consequent_val) + bounds(node.location) + on_rescue(exceptions, reference, statements, consequent) end # def foo(*bar); end @@ -2060,7 +2094,7 @@ module Prism # def foo(*); bar(*); end # ^ def visit_splat_node(node) - raise NoMethodError, __method__ + visit(node.expression) end # A list of statements. @@ -2092,11 +2126,40 @@ module Prism # super(foo) # ^^^^^^^^^^ def visit_super_node(node) - bounds(node.arguments.location) - arguments = on_args_add_block(visit_array_node_elements(node.arguments.arguments), node.block.nil? ? false : visit(node.block)) + arguments = node.arguments&.arguments || [] + block = node.block - bounds(node.location) - on_super(arguments) + if node.block.is_a?(BlockArgumentNode) + arguments << block + block = nil + end + + arguments = + if arguments.any? + args = visit_arguments(arguments) + + if node.block.is_a?(BlockArgumentNode) + args + else + bounds(arguments.first.location) + on_args_add_block(args, false) + end + end + + if !node.lparen_loc.nil? + bounds(node.lparen_loc) + arguments = on_arg_paren(arguments) + end + + if block.nil? + bounds(node.location) + on_super(arguments) + else + block = visit(block) + + bounds(node.location) + on_method_add_block(on_super(arguments), block) + end end # :foo @@ -2199,7 +2262,7 @@ module Prism # This is a special case where we're not going to call on_when directly # because we don't have access to the consequent. Instead, we'll return # the component parts and let the parent node handle it. - conditions = visit_array_node_elements(node.conditions) + conditions = visit_arguments(node.conditions) statements = if node.statements.nil? bounds(node.location) @@ -2320,7 +2383,7 @@ module Prism args = if node.arguments.nil? on_args_new else - on_args_add_block(visit_array_node_elements(node.arguments.arguments)) + on_args_add_block(visit_arguments(node.arguments.arguments)) end method_args_val = on_method_add_arg(on_fcall(ident_val), args) return on_method_add_block(method_args_val, block_val) @@ -2328,7 +2391,7 @@ module Prism if node.arguments.nil? return on_command(ident_val, nil) else - args = on_args_add_block(visit_array_node_elements(node.arguments.arguments), false) + args = on_args_add_block(visit_arguments(node.arguments.arguments), false) return on_command(ident_val, args) end end |