From 450c7029805d6c438068a143b95a8cb24fdf7a43 Mon Sep 17 00:00:00 2001 From: David Wessman Date: Tue, 2 May 2023 06:47:45 +0200 Subject: [PATCH] Args -> ArgumentsNode - Fixes #351 - Changed all use of `parts` to `arguments` as well. --- lib/syntax_tree/dsl.rb | 6 +- lib/syntax_tree/field_visitor.rb | 6 +- lib/syntax_tree/index.rb | 2 +- lib/syntax_tree/mutation_visitor.rb | 6 +- lib/syntax_tree/node.rb | 110 +++++++++++++------------- lib/syntax_tree/parser.rb | 73 +++++++++-------- lib/syntax_tree/reflection.rb | 4 +- lib/syntax_tree/translation/parser.rb | 78 +++++++++++------- lib/syntax_tree/visitor.rb | 4 +- lib/syntax_tree/yarv/compiler.rb | 39 ++++----- lib/syntax_tree/yarv/decompiler.rb | 26 +++--- tasks/sorbet.rake | 18 ++--- test/node_test.rb | 14 ++-- 13 files changed, 209 insertions(+), 177 deletions(-) diff --git a/lib/syntax_tree/dsl.rb b/lib/syntax_tree/dsl.rb index 4506aa04..165dce74 100644 --- a/lib/syntax_tree/dsl.rb +++ b/lib/syntax_tree/dsl.rb @@ -55,9 +55,9 @@ def ArgParen(arguments) ArgParen.new(arguments: arguments, location: Location.default) end - # Create a new Args node. - def Args(parts) - Args.new(parts: parts, location: Location.default) + # Create a new ArgumentsNode node. + def ArgumentsNode(arguments) + ArgumentsNode.new(arguments: arguments, location: Location.default) end # Create a new ArgBlock node. diff --git a/lib/syntax_tree/field_visitor.rb b/lib/syntax_tree/field_visitor.rb index f5607c67..2c76edf9 100644 --- a/lib/syntax_tree/field_visitor.rb +++ b/lib/syntax_tree/field_visitor.rb @@ -94,9 +94,9 @@ def visit_arg_star(node) end end - def visit_args(node) - node(node, "args") do - list("parts", node.parts) + def visit_arguments_node(node) + node(node, "arguments_node") do + list("arguments", node.arguments) comments(node) end end diff --git a/lib/syntax_tree/index.rb b/lib/syntax_tree/index.rb index 0280749f..4f27d496 100644 --- a/lib/syntax_tree/index.rb +++ b/lib/syntax_tree/index.rb @@ -554,7 +554,7 @@ def visit_command(node) node.location.start_column ) - node.arguments.parts.each do |argument| + node.arguments.arguments.each do |argument| next unless argument.is_a?(SymbolLiteral) name = argument.value.value.to_sym diff --git a/lib/syntax_tree/mutation_visitor.rb b/lib/syntax_tree/mutation_visitor.rb index 0b4b9357..1765d439 100644 --- a/lib/syntax_tree/mutation_visitor.rb +++ b/lib/syntax_tree/mutation_visitor.rb @@ -80,9 +80,9 @@ def visit_arg_paren(node) node.copy(arguments: visit(node.arguments)) end - # Visit a Args node. - def visit_args(node) - node.copy(parts: visit_all(node.parts)) + # Visit a ArgumentsNode node. + def visit_arguments_node(node) + node.copy(arguments: visit_all(node.arguments)) end # Visit a ArgBlock node. diff --git a/lib/syntax_tree/node.rb b/lib/syntax_tree/node.rb index 54d132e6..dc7184fc 100644 --- a/lib/syntax_tree/node.rb +++ b/lib/syntax_tree/node.rb @@ -568,7 +568,7 @@ class ARef < Node # [Node] the value being indexed attr_reader :collection - # [nil | Args] the value being passed within the brackets + # [nil | ArgumentsNode] the value being passed within the brackets attr_reader :index # [Array[ Comment | EmbDoc ]] the comments attached to this node @@ -646,7 +646,7 @@ class ARefField < Node # [Node] the value being indexed attr_reader :collection - # [nil | Args] the value being passed within the brackets + # [nil | ArgumentsNode] the value being passed within the brackets attr_reader :index # [Array[ Comment | EmbDoc ]] the comments attached to this node @@ -718,14 +718,14 @@ def ===(other) # # method(argument) # - # In the example above, there would be an ArgParen node around the Args node + # In the example above, there would be an ArgParen node around the ArgumentsNode node # that represents the set of arguments being sent to the method method. The # argument child node can be +nil+ if no arguments were passed, as in: # # method() # class ArgParen < Node - # [nil | Args | ArgsForward] the arguments inside the + # [nil | ArgumentsNode | ArgsForward] the arguments inside the # parentheses attr_reader :arguments @@ -793,9 +793,9 @@ def arity def trailing_comma? arguments = self.arguments - return false unless arguments.is_a?(Args) + return false unless arguments.is_a?(ArgumentsNode) - parts = arguments.parts + parts = arguments.arguments if parts.last.is_a?(ArgBlock) # If the last argument is a block, then we can't put a trailing comma # after it without resulting in a syntax error. @@ -813,36 +813,36 @@ def trailing_comma? end end - # Args represents a list of arguments being passed to a method call or array + # ArgumentsNode represents a list of arguments being passed to a method call or array # literal. # # method(first, second, third) # - class Args < Node + class ArgumentsNode < Node # [Array[ Node ]] the arguments that this node wraps - attr_reader :parts + attr_reader :arguments # [Array[ Comment | EmbDoc ]] the comments attached to this node attr_reader :comments - def initialize(parts:, location:) - @parts = parts + def initialize(arguments:, location:) + @arguments = arguments @location = location @comments = [] end def accept(visitor) - visitor.visit_args(self) + visitor.visit_arguments_node(self) end def child_nodes - parts + arguments end - def copy(parts: nil, location: nil) + def copy(arguments: nil, location: nil) node = - Args.new( - parts: parts || self.parts, + ArgumentsNode.new( + arguments: arguments || self.arguments, location: location || self.location ) @@ -853,24 +853,24 @@ def copy(parts: nil, location: nil) alias deconstruct child_nodes def deconstruct_keys(_keys) - { parts: parts, location: location, comments: comments } + { arguments: arguments, location: location, comments: comments } end def format(q) - q.seplist(parts) { |part| q.format(part) } + q.seplist(arguments) { |argument| q.format(argument) } end def ===(other) - other.is_a?(Args) && ArrayMatch.call(parts, other.parts) + other.is_a?(ArgumentsNode) && ArrayMatch.call(arguments, other.arguments) end def arity - parts.sum do |part| - case part + arguments.sum do |argument| + case argument when ArgStar, ArgsForward Float::INFINITY when BareAssocHash - part.assocs.sum do |assoc| + argument.assocs.sum do |assoc| assoc.is_a?(AssocSplat) ? Float::INFINITY : 1 end else @@ -1064,7 +1064,7 @@ def call(q) # Formats an array of multiple simple string literals into the %w syntax. class QWordsFormatter - # [Args] the contents of the array + # [ArgumentsNode] the contents of the array attr_reader :contents def initialize(contents) @@ -1076,7 +1076,7 @@ def format(q) q.group do q.indent do q.breakable_empty - q.seplist(contents.parts, BREAKABLE_SPACE_SEPARATOR) do |part| + q.seplist(contents.arguments, BREAKABLE_SPACE_SEPARATOR) do |part| if part.is_a?(StringLiteral) q.format(part.parts.first) else @@ -1092,7 +1092,7 @@ def format(q) # Formats an array of multiple simple symbol literals into the %i syntax. class QSymbolsFormatter - # [Args] the contents of the array + # [ArgumentsNode] the contents of the array attr_reader :contents def initialize(contents) @@ -1104,7 +1104,7 @@ def format(q) q.group do q.indent do q.breakable_empty - q.seplist(contents.parts, BREAKABLE_SPACE_SEPARATOR) do |part| + q.seplist(contents.arguments, BREAKABLE_SPACE_SEPARATOR) do |part| q.format(part.value) end end @@ -1144,7 +1144,7 @@ def format(q) # bracket that opens this array attr_reader :lbracket - # [nil | Args] the contents of the array + # [nil | ArgumentsNode] the contents of the array attr_reader :contents # [Array[ Comment | EmbDoc ]] the comments attached to this node @@ -1193,7 +1193,7 @@ def format(q) contents = self.contents if lbracket.is_a?(LBracket) && lbracket.comments.empty? && contents && - contents.comments.empty? && contents.parts.length > 1 + contents.comments.empty? && contents.arguments.length > 1 if qwords? QWordsFormatter.new(contents).format(q) return @@ -1234,7 +1234,7 @@ def ===(other) private def qwords? - contents.parts.all? do |part| + contents.arguments.all? do |part| case part when StringLiteral part.comments.empty? && part.parts.length == 1 && @@ -1249,7 +1249,7 @@ def qwords? end def qsymbols? - contents.parts.all? do |part| + contents.arguments.all? do |part| part.is_a?(SymbolLiteral) && part.comments.empty? end end @@ -2458,7 +2458,7 @@ def format(q) q.group do q.text(keyword) - parts = node.arguments.parts + parts = node.arguments.arguments length = parts.length if length == 0 @@ -2487,7 +2487,7 @@ def format(q) if statement.is_a?(ArrayLiteral) contents = statement.contents - if contents && contents.parts.length >= 2 + if contents && contents.arguments.length >= 2 # Here we have a single argument that is a set of parentheses # wrapping an array literal that has at least 2 elements. # We're going to print the contents of the array directly. @@ -2545,7 +2545,7 @@ def format(q) when ArrayLiteral contents = part.contents - if contents && contents.parts.length >= 2 + if contents && contents.arguments.length >= 2 # Here there is a single argument that is an array literal with at # least two elements. We skip directly into the array literal's # elements in order to print the contents. This would be like if @@ -2632,7 +2632,7 @@ def skip_parens?(node) # break 1 # class Break < Node - # [Args] the arguments being sent to the keyword + # [ArgumentsNode] the arguments being sent to the keyword attr_reader :arguments # [Array[ Comment | EmbDoc ]] the comments attached to this node @@ -2950,7 +2950,7 @@ class CallNode < Node # [:call | Backtick | Const | Ident | Op] the message being sent attr_reader :message - # [nil | ArgParen | Args] the arguments to the method call + # [nil | ArgParen | ArgumentsNode] the arguments to the method call attr_reader :arguments # [Array[ Comment | EmbDoc ]] the comments attached to this node @@ -3063,7 +3063,7 @@ def format_arguments(q) case arguments when ArgParen q.format(arguments) - when Args + when ArgumentsNode q.text(" ") q.format(arguments) end @@ -3449,7 +3449,7 @@ class Command < Node # [Const | Ident] the message being sent to the implicit receiver attr_reader :message - # [Args] the arguments being sent with the message + # [ArgumentsNode] the arguments being sent with the message attr_reader :arguments # [nil | BlockNode] the optional block being passed to the method @@ -3520,15 +3520,15 @@ def arity private def align(q, node, &block) - arguments = node.arguments + argument_node = node.arguments - if arguments.is_a?(Args) - parts = arguments.parts + if argument_node.is_a?(ArgumentsNode) + arguments = argument_node.arguments - if parts.size == 1 - part = parts.first + if arguments.size == 1 + argument = arguments.first - case part + case argument when DefNode q.text(" ") yield @@ -3536,7 +3536,7 @@ def align(q, node, &block) q.if_flat { q.text(" ") } yield when Command - align(q, part, &block) + align(q, argument, &block) else q.text(" ") q.nest(message.value.length + 1) { yield } @@ -3567,7 +3567,7 @@ class CommandCall < Node # [:call | Const | Ident | Op] the message being send attr_reader :message - # [nil | Args | ArgParen] the arguments going along with the message + # [nil | ArgumentsNode | ArgParen] the arguments going along with the message attr_reader :arguments # [nil | BlockNode] the block associated with this method call @@ -3665,7 +3665,7 @@ def format(q) # Format the arguments for this command call here. If there are no # arguments, then print nothing. if arguments - parts = arguments.parts + parts = arguments.arguments if parts.length == 1 && parts.first.is_a?(IfOp) q.if_flat { q.text(" ") } @@ -7947,7 +7947,7 @@ def ===(other) # next(value) # class Next < Node - # [Args] the arguments passed to the next keyword + # [ArgumentsNode] the arguments passed to the next keyword attr_reader :arguments # [Array[ Comment | EmbDoc ]] the comments attached to this node @@ -8170,7 +8170,7 @@ def skip_indent? # keeping the correct semantic meaning. module Parentheses NODES = [ - Args, + ArgumentsNode, Assign, Assoc, Binary, @@ -9717,7 +9717,7 @@ def ===(other) # return value # class ReturnNode < Node - # [nil | Args] the arguments being passed to the keyword + # [nil | ArgumentsNode] the arguments being passed to the keyword attr_reader :arguments # [Array[ Comment | EmbDoc ]] the comments attached to this node @@ -10428,7 +10428,7 @@ def ===(other) # super(value) # class Super < Node - # [ArgParen | Args] the arguments to the keyword + # [ArgParen | ArgumentsNode] the arguments to the keyword attr_reader :arguments # [Array[ Comment | EmbDoc ]] the comments attached to this node @@ -11833,7 +11833,7 @@ def ===(other) # end # class When < Node - # [Args] the arguments to the when clause + # [ArgumentsNode] the arguments to the when clause attr_reader :arguments # [Statements] the expressions to be executed @@ -11912,13 +11912,15 @@ def format(q) if arguments.comments.any? q.format(arguments) else - q.seplist(arguments.parts, SEPARATOR) { |part| q.format(part) } + q.seplist(arguments.arguments, SEPARATOR) do |part| + q.format(part) + end end # Very special case here. If you're inside of a when clause and the # last argument to the predicate is and endless range, then you are # forced to use the "then" keyword to make it parse properly. - last = arguments.parts.last + last = arguments.arguments.last q.text(" then") if last.is_a?(RangeNode) && !last.right end end @@ -12287,7 +12289,7 @@ def ===(other) # yield value # class YieldNode < Node - # [nil | Args | Paren] the arguments passed to the yield + # [nil | ArgumentsNode | Paren] the arguments passed to the yield attr_reader :arguments # [Array[ Comment | EmbDoc ]] the comments attached to this node diff --git a/lib/syntax_tree/parser.rb b/lib/syntax_tree/parser.rb index 825cd90e..0c56f46d 100644 --- a/lib/syntax_tree/parser.rb +++ b/lib/syntax_tree/parser.rb @@ -461,7 +461,7 @@ def on_alias(left, right) end # :call-seq: - # on_aref: (untyped collection, (nil | Args) index) -> ARef + # on_aref: (untyped collection, (nil | ArgumentsNode) index) -> ARef def on_aref(collection, index) consume_token(LBracket) rbracket = consume_token(RBracket) @@ -476,7 +476,7 @@ def on_aref(collection, index) # :call-seq: # on_aref_field: ( # untyped collection, - # (nil | Args) index + # (nil | ArgumentsNode) index # ) -> ARefField def on_aref_field(collection, index) consume_token(LBracket) @@ -495,7 +495,7 @@ def on_aref_field(collection, index) # :call-seq: # on_arg_paren: ( - # (nil | Args | ArgsForward) arguments + # (nil | ArgumentsNode | ArgsForward) arguments # ) -> ArgParen def on_arg_paren(arguments) lparen = consume_token(LParen) @@ -518,18 +518,18 @@ def on_arg_paren(arguments) end # :call-seq: - # on_args_add: (Args arguments, untyped argument) -> Args + # on_args_add: (ArgumentsNode arguments, untyped argument) -> ArgumentsNode def on_args_add(arguments, argument) - if arguments.parts.empty? + if arguments.arguments.empty? # If this is the first argument being passed into the list of arguments, # then we're going to use the bounds of the argument to override the # parent node's location since this will be more accurate. - Args.new(parts: [argument], location: argument.location) + ArgumentsNode.new(arguments: [argument], location: argument.location) else # Otherwise we're going to update the existing list with the argument # being added as well as the new end bounds. - Args.new( - parts: arguments.parts << argument, + ArgumentsNode.new( + arguments: arguments.arguments << argument, location: arguments.location.to(argument.location) ) end @@ -537,11 +537,11 @@ def on_args_add(arguments, argument) # :call-seq: # on_args_add_block: ( - # Args arguments, + # ArgumentsNode arguments, # (false | untyped) block - # ) -> Args + # ) -> ArgumentsNode def on_args_add_block(arguments, block) - end_char = arguments.parts.any? && arguments.location.end_char + end_char = arguments.arguments.any? && arguments.location.end_char # First, see if there is an & operator that could potentially be # associated with the block part of this args_add_block. If there is not, @@ -570,20 +570,20 @@ def on_args_add_block(arguments, block) # block, which could be missing because it could be a bare & since 3.1.0). arg_block = ArgBlock.new(value: block, location: location) - Args.new( - parts: arguments.parts << arg_block, + ArgumentsNode.new( + arguments: arguments.arguments << arg_block, location: arguments.location.to(location) ) end # :call-seq: - # on_args_add_star: (Args arguments, untyped star) -> Args + # on_args_add_star: (ArgumentsNode arguments, untyped star) -> ArgumentsNode def on_args_add_star(arguments, argument) beginning = consume_operator(:*) ending = argument || beginning location = - if arguments.parts.empty? + if arguments.arguments.empty? ending.location else arguments.location.to(ending.location) @@ -595,7 +595,10 @@ def on_args_add_star(arguments, argument) location: beginning.location.to(ending.location) ) - Args.new(parts: arguments.parts << arg_star, location: location) + ArgumentsNode.new( + arguments: arguments.arguments << arg_star, + location: location + ) end # :call-seq: @@ -607,20 +610,20 @@ def on_args_forward end # :call-seq: - # on_args_new: () -> Args + # on_args_new: () -> ArgumentsNode def on_args_new - Args.new( - parts: [], + ArgumentsNode.new( + arguments: [], location: Location.fixed(line: lineno, column: current_column, char: char_pos) ) end # :call-seq: - # on_array: ((nil | Args) contents) -> + # on_array: ((nil | ArgumentsNode) contents) -> # ArrayLiteral | QSymbols | QWords | Symbols | Words def on_array(contents) - if !contents || contents.is_a?(Args) + if !contents || contents.is_a?(ArgumentsNode) lbracket = consume_token(LBracket) rbracket = consume_token(RBracket) @@ -1021,12 +1024,12 @@ def on_brace_block(block_var, statements) end # :call-seq: - # on_break: (Args arguments) -> Break + # on_break: (ArgumentsNode arguments) -> Break def on_break(arguments) keyword = consume_keyword(:break) location = keyword.location - location = location.to(arguments.location) if arguments.parts.any? + location = location.to(arguments.location) if arguments.arguments.any? Break.new(arguments: arguments, location: location) end @@ -1135,7 +1138,7 @@ def on_comma(value) end # :call-seq: - # on_command: ((Const | Ident) message, Args arguments) -> Command + # on_command: ((Const | Ident) message, ArgumentsNode arguments) -> Command def on_command(message, arguments) Command.new( message: message, @@ -1150,7 +1153,7 @@ def on_command(message, arguments) # untyped receiver, # (:"::" | Op | Period) operator, # (Const | Ident | Op) message, - # (nil | Args) arguments + # (nil | ArgumentsNode) arguments # ) -> CommandCall def on_command_call(receiver, operator, message, arguments) ending = arguments || message @@ -2504,7 +2507,7 @@ def on_massign(target, value) # :call-seq: # on_method_add_arg: ( # CallNode call, - # (ArgParen | Args) arguments + # (ArgParen | ArgumentsNode) arguments # ) -> CallNode def on_method_add_arg(call, arguments) location = call.location @@ -2529,7 +2532,7 @@ def on_method_add_block(call, block) case call when Break, Next, ReturnNode - parts = call.arguments.parts + parts = call.arguments.arguments node = parts.pop copied = @@ -2677,18 +2680,18 @@ def on_mrhs_add_star(mrhs, value) end # :call-seq: - # on_mrhs_new_from_args: (Args arguments) -> MRHS + # on_mrhs_new_from_args: (ArgumentsNode arguments) -> MRHS def on_mrhs_new_from_args(arguments) - MRHS.new(parts: arguments.parts, location: arguments.location) + MRHS.new(parts: arguments.arguments, location: arguments.location) end # :call-seq: - # on_next: (Args arguments) -> Next + # on_next: (ArgumentsNode arguments) -> Next def on_next(arguments) keyword = consume_keyword(:next) location = keyword.location - location = location.to(arguments.location) if arguments.parts.any? + location = location.to(arguments.location) if arguments.arguments.any? Next.new(arguments: arguments, location: location) end @@ -3306,7 +3309,7 @@ def on_retry end # :call-seq: - # on_return: (Args arguments) -> ReturnNode + # on_return: (ArgumentsNode arguments) -> ReturnNode def on_return(arguments) keyword = consume_keyword(:return) @@ -3540,7 +3543,7 @@ def on_string_literal(string) end # :call-seq: - # on_super: ((ArgParen | Args) arguments) -> Super + # on_super: ((ArgParen | ArgumentsNode) arguments) -> Super def on_super(arguments) keyword = consume_keyword(:super) @@ -3953,7 +3956,7 @@ def on_void_stmt # :call-seq: # on_when: ( - # Args arguments, + # ArgumentsNode arguments, # Statements statements, # (nil | Else | When) consequent # ) -> When @@ -4149,7 +4152,7 @@ def on_xstring_literal(xstring) end # :call-seq: - # on_yield: ((Args | Paren) arguments) -> YieldNode + # on_yield: ((ArgumentsNode | Paren) arguments) -> YieldNode def on_yield(arguments) keyword = consume_keyword(:yield) diff --git a/lib/syntax_tree/reflection.rb b/lib/syntax_tree/reflection.rb index aa7b85b6..6a68e815 100644 --- a/lib/syntax_tree/reflection.rb +++ b/lib/syntax_tree/reflection.rb @@ -213,13 +213,13 @@ def parse_comments(statements, index) # The arguments to the command are the attributes that we're defining. # We want to ensure that we're only defining one at a time. - if statement.arguments.parts.length != 1 + if statement.arguments.arguments.length != 1 raise "Declaring more than one attribute at a time is not permitted" end attribute = Attribute.new( - statement.arguments.parts.first.value.value.to_sym, + statement.arguments.arguments.first.value.value.to_sym, "#{parse_comments(statements, statement_index).join("\n")}\n" ) diff --git a/lib/syntax_tree/translation/parser.rb b/lib/syntax_tree/translation/parser.rb index 8be4fc79..eb97615a 100644 --- a/lib/syntax_tree/translation/parser.rb +++ b/lib/syntax_tree/translation/parser.rb @@ -118,7 +118,9 @@ def visit_aref(node) else s( :index, - [visit(node.collection)].concat(visit_all(node.index.parts)), + [visit(node.collection)].concat( + visit_all(node.index.arguments) + ), smap_index( srange_find_between(node.collection, node.index, "["), srange_length(node.end_char, -1), @@ -172,7 +174,9 @@ def visit_aref_field(node) else s( :indexasgn, - [visit(node.collection)].concat(visit_all(node.index.parts)), + [visit(node.collection)].concat( + visit_all(node.index.arguments) + ), smap_index( srange_find_between(node.collection, node.index, "["), srange_length(node.end_char, -1), @@ -254,7 +258,7 @@ def visit_args_forward(node) def visit_array(node) s( :array, - node.contents ? visit_all(node.contents.parts) : [], + node.contents ? visit_all(node.contents.arguments) : [], if node.lbracket.nil? smap_collection_bare(srange_node(node)) else @@ -647,7 +651,7 @@ def visit_bodystmt(node) def visit_break(node) s( :break, - visit_all(node.arguments.parts), + visit_all(node.arguments.arguments), smap_keyword_bare( srange_length(node.start_char, 5), srange_node(node) @@ -755,8 +759,8 @@ def visit_command_call(node) end_token = nil case node.arguments - when Args - children += visit_all(node.arguments.parts) + when ArgumentsNode + children += visit_all(node.arguments.arguments) when ArgParen case node.arguments.arguments when nil @@ -764,7 +768,7 @@ def visit_command_call(node) when ArgsForward children << visit(node.arguments.arguments) else - children += visit_all(node.arguments.arguments.parts) + children += visit_all(node.arguments.arguments.arguments) end begin_token = srange_length(node.arguments.start_char, 1) @@ -783,8 +787,9 @@ def visit_command_call(node) expression = if node.arguments.is_a?(ArgParen) srange(node.start_char, node.arguments.end_char) - elsif node.arguments.is_a?(Args) && node.arguments.parts.any? - last_part = node.arguments.parts.last + elsif node.arguments.is_a?(ArgumentsNode) && + node.arguments.arguments.any? + last_part = node.arguments.arguments.last end_char = if last_part.is_a?(Heredoc) last_part.beginning.end_char @@ -1683,7 +1688,11 @@ def visit_mrhs(node) visit_array( ArrayLiteral.new( lbracket: nil, - contents: Args.new(parts: node.parts, location: node.location), + contents: + ArgumentsNode.new( + arguments: node.parts, + location: node.location + ), location: node.location ) ) @@ -1693,7 +1702,7 @@ def visit_mrhs(node) def visit_next(node) s( :next, - visit_all(node.arguments.parts), + visit_all(node.arguments.arguments), smap_keyword_bare( srange_length(node.start_char, 4), srange_node(node) @@ -1952,7 +1961,8 @@ def visit_qsymbols(node) visit_array( ArrayLiteral.new( lbracket: node.beginning, - contents: Args.new(parts: parts, location: node.location), + contents: + ArgumentsNode.new(arguments: parts, location: node.location), location: node.location ) ) @@ -1963,7 +1973,11 @@ def visit_qwords(node) visit_array( ArrayLiteral.new( lbracket: node.beginning, - contents: Args.new(parts: node.elements, location: node.location), + contents: + ArgumentsNode.new( + arguments: node.elements, + location: node.location + ), location: node.location ) ) @@ -2058,8 +2072,8 @@ def visit_rescue(node) ArrayLiteral.new( lbracket: nil, contents: - Args.new( - parts: node.exception.exceptions.parts, + ArgumentsNode.new( + arguments: node.exception.exceptions.parts, location: node.exception.exceptions.location ), location: node.exception.exceptions.location @@ -2070,8 +2084,8 @@ def visit_rescue(node) ArrayLiteral.new( lbracket: nil, contents: - Args.new( - parts: [node.exception.exceptions], + ArgumentsNode.new( + arguments: [node.exception.exceptions], location: node.exception.exceptions.location ), location: node.exception.exceptions.location @@ -2169,7 +2183,7 @@ def visit_retry(node) def visit_return(node) s( :return, - node.arguments ? visit_all(node.arguments.parts) : [], + node.arguments ? visit_all(node.arguments.arguments) : [], smap_keyword_bare( srange_length(node.start_char, 6), srange_node(node) @@ -2267,10 +2281,10 @@ def visit_string_literal(node) # Visit a Super node. def visit_super(node) - if node.arguments.is_a?(Args) + if node.arguments.is_a?(ArgumentsNode) s( :super, - visit_all(node.arguments.parts), + visit_all(node.arguments.arguments), smap_keyword_bare( srange_length(node.start_char, 5), srange_node(node) @@ -2303,7 +2317,7 @@ def visit_super(node) else s( :super, - visit_all(node.arguments.arguments.parts), + visit_all(node.arguments.arguments.arguments), smap_keyword( srange_length(node.start_char, 5), srange_find(node.start_char + 5, node.end_char, "("), @@ -2349,7 +2363,8 @@ def visit_symbols(node) visit_array( ArrayLiteral.new( lbracket: node.beginning, - contents: Args.new(parts: parts, location: node.location), + contents: + ArgumentsNode.new(arguments: parts, location: node.location), location: node.location ) ) @@ -2618,7 +2633,7 @@ def visit_when(node) s( :when, - visit_all(node.arguments.parts) + [visit(node.statements)], + visit_all(node.arguments.arguments) + [visit(node.statements)], smap_keyword( keyword, begin_token, @@ -2666,7 +2681,11 @@ def visit_words(node) visit_array( ArrayLiteral.new( lbracket: node.beginning, - contents: Args.new(parts: node.elements, location: node.location), + contents: + ArgumentsNode.new( + arguments: node.elements, + location: node.location + ), location: node.location ) ) @@ -2699,10 +2718,10 @@ def visit_yield(node) srange_node(node) ) ) - when Args + when ArgumentsNode s( :yield, - visit_all(node.arguments.parts), + visit_all(node.arguments.arguments), smap_keyword_bare( srange_length(node.start_char, 5), srange_node(node) @@ -2711,7 +2730,7 @@ def visit_yield(node) else s( :yield, - visit_all(node.arguments.contents.parts), + visit_all(node.arguments.contents.arguments), smap_keyword( srange_length(node.start_char, 5), srange_length(node.arguments.start_char, 1), @@ -2822,7 +2841,10 @@ def canonical_binary(node) operator: nil, message: Op.new(value: operator, location: op_location), arguments: - Args.new(parts: [node.right], location: node.right.location), + ArgumentsNode.new( + arguments: [node.right], + location: node.right.location + ), block: nil, location: node.location ) diff --git a/lib/syntax_tree/visitor.rb b/lib/syntax_tree/visitor.rb index eb57acd2..89023fb6 100644 --- a/lib/syntax_tree/visitor.rb +++ b/lib/syntax_tree/visitor.rb @@ -23,8 +23,8 @@ class Visitor < BasicVisitor # Visit an ArgStar node. alias visit_arg_star visit_child_nodes - # Visit an Args node. - alias visit_args visit_child_nodes + # Visit an ArgumentsNode node. + alias visit_arguments_node visit_child_nodes # Visit an ArgsForward node. alias visit_args_forward visit_child_nodes diff --git a/lib/syntax_tree/yarv/compiler.rb b/lib/syntax_tree/yarv/compiler.rb index 0f7e7372..4a0cf688 100644 --- a/lib/syntax_tree/yarv/compiler.rb +++ b/lib/syntax_tree/yarv/compiler.rb @@ -126,7 +126,7 @@ def self.compile(node) visit_methods do def visit_array(node) - node.contents ? visit_all(node.contents.parts) : [] + node.contents ? visit_all(node.contents.arguments) : [] end def visit_bare_assoc_hash(node) @@ -349,8 +349,9 @@ def visit_aref(node) visit(node.collection) if !options.frozen_string_literal? && - options.specialized_instruction? && (node.index.parts.length == 1) - arg = node.index.parts.first + options.specialized_instruction? && + (node.index.arguments.length == 1) + arg = node.index.arguments.first if arg.is_a?(StringLiteral) && (arg.parts.length == 1) string_part = arg.parts.first @@ -379,26 +380,26 @@ def visit_arg_star(node) iseq.splatarray(false) end - def visit_args(node) - visit_all(node.parts) + def visit_arguments_node(node) + visit_all(node.arguments) end def visit_array(node) if (compiled = RubyVisitor.compile(node)) iseq.duparray(compiled) - elsif node.contents && node.contents.parts.length == 1 && - node.contents.parts.first.is_a?(BareAssocHash) && - node.contents.parts.first.assocs.length == 1 && - node.contents.parts.first.assocs.first.is_a?(AssocSplat) + elsif node.contents && node.contents.arguments.length == 1 && + node.contents.arguments.first.is_a?(BareAssocHash) && + node.contents.arguments.first.assocs.length == 1 && + node.contents.arguments.first.assocs.first.is_a?(AssocSplat) iseq.putspecialobject(PutSpecialObject::OBJECT_VMCORE) iseq.newhash(0) - visit(node.contents.parts.first) + visit(node.contents.arguments.first) iseq.send(YARV.calldata(:"core#hash_merge_kwd", 2)) iseq.newarraykwsplat(1) else length = 0 - node.contents.parts.each do |part| + node.contents.arguments.each do |part| if part.is_a?(ArgStar) if length > 0 iseq.newarray(length) @@ -414,7 +415,9 @@ def visit_array(node) end iseq.newarray(length) if length > 0 - iseq.concatarray if length > 0 && length != node.contents.parts.length + if length > 0 && length != node.contents.arguments.length + iseq.concatarray + end end end @@ -428,8 +431,8 @@ def visit_assign(node) if !options.frozen_string_literal? && options.specialized_instruction? && - (node.target.index.parts.length == 1) - arg = node.target.index.parts.first + (node.target.index.arguments.length == 1) + arg = node.target.index.arguments.first if arg.is_a?(StringLiteral) && (arg.parts.length == 1) string_part = arg.parts.first @@ -1888,16 +1891,16 @@ def argument_parts(node) case node when nil [] - when Args - node.parts + when ArgumentsNode + node.arguments when ArgParen if node.arguments.is_a?(ArgsForward) [node.arguments] else - node.arguments.parts + node.arguments.arguments end when Paren - node.contents.parts + node.contents.arguments end end diff --git a/lib/syntax_tree/yarv/decompiler.rb b/lib/syntax_tree/yarv/decompiler.rb index 4ea99e3a..43f50d0c 100644 --- a/lib/syntax_tree/yarv/decompiler.rb +++ b/lib/syntax_tree/yarv/decompiler.rb @@ -67,14 +67,14 @@ def decompile(iseq) when BranchIf body = [ Assign(block_label.field, node_for(insn.label.name)), - Next(Args([])) + Next(ArgumentsNode([])) ] clause << UnlessNode(clause.pop, Statements(body), nil) when BranchUnless body = [ Assign(block_label.field, node_for(insn.label.name)), - Next(Args([])) + Next(ArgumentsNode([])) ] clause << IfNode(clause.pop, Statements(body), nil) @@ -94,9 +94,9 @@ def decompile(iseq) clause << VarRef(Ident(local.name.to_s)) when Jump clause << Assign(block_label.field, node_for(insn.label.name)) - clause << Next(Args([])) + clause << Next(ArgumentsNode([])) when Leave - value = Args([clause.pop]) + value = ArgumentsNode([clause.pop]) clause << (iseq.type != :top ? Break(value) : ReturnNode(value)) when OptAnd, OptDiv, OptEq, OptGE, OptGT, OptLE, OptLT, OptLTLT, OptMinus, OptMod, OptMult, OptOr, OptPlus @@ -104,20 +104,20 @@ def decompile(iseq) clause << Binary(left, insn.calldata.method, right) when OptAref collection, arg = clause.pop(2) - clause << ARef(collection, Args([arg])) + clause << ARef(collection, ArgumentsNode([arg])) when OptAset collection, arg, value = clause.pop(3) clause << if value.is_a?(Binary) && value.left.is_a?(ARef) && collection === value.left.collection && - arg === value.left.index.parts[0] + arg === value.left.index.arguments[0] OpAssign( - ARefField(collection, Args([arg])), + ARefField(collection, ArgumentsNode([arg])), Op("#{value.operator}="), value.right ) else - Assign(ARefField(collection, Args([arg])), value) + Assign(ARefField(collection, ArgumentsNode([arg])), value) end when OptNEq left, right = clause.pop(2) @@ -129,7 +129,7 @@ def decompile(iseq) if insn.calldata.flag?(CallData::CALL_FCALL) if argc == 0 clause.pop - clause << CallNode(nil, nil, Ident(method), Args([])) + clause << CallNode(nil, nil, Ident(method), ArgumentsNode([])) elsif argc == 1 && method.end_with?("=") _receiver, argument = clause.pop(2) clause << Assign( @@ -142,7 +142,7 @@ def decompile(iseq) nil, nil, Ident(method), - ArgParen(Args(arguments)) + ArgParen(ArgumentsNode(arguments)) ) end else @@ -160,7 +160,7 @@ def decompile(iseq) receiver, Period("."), Ident(method), - ArgParen(Args(arguments)) + ArgParen(ArgumentsNode(arguments)) ) end end @@ -215,7 +215,7 @@ def decompile(iseq) clauses.reverse_each do |current_label, current_clause| current = When( - Args([node_for(current_label)]), + ArgumentsNode([node_for(current_label)]), Statements(current_clause), current ) @@ -243,7 +243,7 @@ def decompile(iseq) # statement. stack << Assign(block_label.field, node_for(:label_0)) stack << MethodAddBlock( - CallNode(nil, nil, Ident("loop"), Args([])), + CallNode(nil, nil, Ident("loop"), ArgumentsNode([])), BlockNode( Kw("do"), nil, diff --git a/tasks/sorbet.rake b/tasks/sorbet.rake index 05f48874..e0abda7b 100644 --- a/tasks/sorbet.rake +++ b/tasks/sorbet.rake @@ -61,7 +61,7 @@ module SyntaxTree node_body << Command( Ident("attr_reader"), - Args([SymbolLiteral(Ident("location"))]), + ArgumentsNode([SymbolLiteral(Ident("location"))]), nil, location ) @@ -102,7 +102,7 @@ module SyntaxTree node_body << Command( Ident("attr_reader"), - Args([SymbolLiteral(Ident(attribute.name.to_s))]), + ArgumentsNode([SymbolLiteral(Ident(attribute.name.to_s))]), nil, location ) @@ -121,7 +121,7 @@ module SyntaxTree Period("."), Ident("returns"), ArgParen( - Args( + ArgumentsNode( [CallNode(VarRef(Const("T")), Period("."), Ident("untyped"), nil)] ) ) @@ -214,7 +214,7 @@ module SyntaxTree VarRef(Const("T")), Period("."), Ident("unsafe"), - ArgParen(Args([VarRef(Kw("nil"))])) + ArgParen(ArgumentsNode([VarRef(Kw("nil"))])) ) ] else @@ -304,11 +304,11 @@ module SyntaxTree end def sig_params - CallNode(nil, nil, Ident("params"), ArgParen(Args([yield]))) + CallNode(nil, nil, Ident("params"), ArgParen(ArgumentsNode([yield]))) end def sig_returns - CallNode(nil, nil, Ident("returns"), ArgParen(Args([yield]))) + CallNode(nil, nil, Ident("returns"), ArgParen(ArgumentsNode([yield]))) end def sig_type_for(type) @@ -319,7 +319,7 @@ module SyntaxTree sig_type_for(type.type) ) when Reflection::Type::TupleType - ArrayLiteral(LBracket("["), Args(type.types.map { sig_type_for(_1) })) + ArrayLiteral(LBracket("["), ArgumentsNode(type.types.map { sig_type_for(_1) })) when Reflection::Type::UnionType if type.types.include?(NilClass) selected = type.types.reject { _1 == NilClass } @@ -334,14 +334,14 @@ module SyntaxTree VarRef(Const("T")), Period("."), Ident("nilable"), - ArgParen(Args([sig_type_for(subtype)])) + ArgParen(ArgumentsNode([sig_type_for(subtype)])) ) else CallNode( VarRef(Const("T")), Period("."), Ident("any"), - ArgParen(Args(type.types.map { sig_type_for(_1) })) + ArgParen(ArgumentsNode(type.types.map { sig_type_for(_1) })) ) end when Symbol diff --git a/test/node_test.rb b/test/node_test.rb index 19fbeed2..6fcf99b2 100644 --- a/test/node_test.rb +++ b/test/node_test.rb @@ -68,7 +68,9 @@ def test_args source = "method(first, second, third)" at = location(chars: 7..27) - assert_node(Args, source, at: at) { |node| node.arguments.arguments } + assert_node(ArgumentsNode, source, at: at) do |node| + node.arguments.arguments + end end def test_arg_block @@ -76,7 +78,7 @@ def test_arg_block at = location(chars: 17..23) assert_node(ArgBlock, source, at: at) do |node| - node.arguments.arguments.parts[1] + node.arguments.arguments.arguments[1] end end @@ -90,7 +92,7 @@ def method(&) at = location(lines: 2..2, chars: 29..30) assert_node(ArgBlock, source, at: at) do |node| - node.bodystmt.statements.body.first.arguments.arguments.parts[0] + node.bodystmt.statements.body.first.arguments.arguments.arguments[0] end end end @@ -100,7 +102,7 @@ def test_arg_star at = location(chars: 15..25) assert_node(ArgStar, source, at: at) do |node| - node.arguments.arguments.parts[1] + node.arguments.arguments.arguments[1] end end @@ -114,7 +116,7 @@ def get(...) at = location(lines: 2..2, chars: 29..32) assert_node(ArgsForward, source, at: at) do |node| - node.bodystmt.statements.body.first.arguments.arguments.parts.last + node.bodystmt.statements.body.first.arguments.arguments.arguments.last end end end @@ -176,7 +178,7 @@ def test_bare_assoc_hash at = location(chars: 7..33) assert_node(BareAssocHash, source, at: at) do |node| - node.arguments.arguments.parts.first + node.arguments.arguments.arguments.first end end