From e6073f04d821b2df1260f4163fc0cfcd4545c5bf Mon Sep 17 00:00:00 2001 From: David Wessman Date: Thu, 4 May 2023 20:36:19 +0200 Subject: [PATCH] AliasNode: {left, right} -> {new_name, old_name} - Fixes #348 except for the `keyword_loc` --- lib/syntax_tree/dsl.rb | 8 +++-- lib/syntax_tree/field_visitor.rb | 4 +-- lib/syntax_tree/index.rb | 5 ++-- lib/syntax_tree/mutation_visitor.rb | 5 +++- lib/syntax_tree/node.rb | 42 +++++++++++++++++---------- lib/syntax_tree/parser.rb | 22 +++++++------- lib/syntax_tree/translation/parser.rb | 2 +- lib/syntax_tree/yarv/compiler.rb | 4 +-- 8 files changed, 55 insertions(+), 37 deletions(-) diff --git a/lib/syntax_tree/dsl.rb b/lib/syntax_tree/dsl.rb index 4506aa04..4b372266 100644 --- a/lib/syntax_tree/dsl.rb +++ b/lib/syntax_tree/dsl.rb @@ -32,8 +32,12 @@ def EndContent(value) end # Create a new AliasNode node. - def AliasNode(left, right) - AliasNode.new(left: left, right: right, location: Location.default) + def AliasNode(new_name, old_name) + AliasNode.new( + new_name: new_name, + old_name: old_name, + location: Location.default + ) end # Create a new ARef node. diff --git a/lib/syntax_tree/field_visitor.rb b/lib/syntax_tree/field_visitor.rb index f5607c67..01e7dbd3 100644 --- a/lib/syntax_tree/field_visitor.rb +++ b/lib/syntax_tree/field_visitor.rb @@ -67,8 +67,8 @@ def visit_aref_field(node) def visit_alias(node) node(node, "alias") do - field("left", node.left) - field("right", node.right) + field("new_name", node.new_name) + field("old_name", node.old_name) comments(node) end end diff --git a/lib/syntax_tree/index.rb b/lib/syntax_tree/index.rb index 0280749f..3020358c 100644 --- a/lib/syntax_tree/index.rb +++ b/lib/syntax_tree/index.rb @@ -475,7 +475,8 @@ def initialize visit_methods do def visit_alias(node) - if node.left.is_a?(SymbolLiteral) && node.right.is_a?(SymbolLiteral) + if node.new_name.is_a?(SymbolLiteral) && + node.old_name.is_a?(SymbolLiteral) location = Location.new( node.location.start_line, @@ -484,7 +485,7 @@ def visit_alias(node) results << AliasMethodDefinition.new( nesting.dup, - node.left.value.value.to_sym, + node.new_name.value.value.to_sym, location, comments_for(node) ) diff --git a/lib/syntax_tree/mutation_visitor.rb b/lib/syntax_tree/mutation_visitor.rb index 0b4b9357..5d04dcd1 100644 --- a/lib/syntax_tree/mutation_visitor.rb +++ b/lib/syntax_tree/mutation_visitor.rb @@ -62,7 +62,10 @@ def visit___end__(node) # Visit a AliasNode node. def visit_alias(node) - node.copy(left: visit(node.left), right: visit(node.right)) + node.copy( + new_name: visit(node.new_name), + old_name: visit(node.old_name) + ) end # Visit a ARef node. diff --git a/lib/syntax_tree/node.rb b/lib/syntax_tree/node.rb index a2c78677..7db0cf3f 100644 --- a/lib/syntax_tree/node.rb +++ b/lib/syntax_tree/node.rb @@ -485,17 +485,17 @@ def format(q) end # [DynaSymbol | GVar | SymbolLiteral] the new name of the method - attr_reader :left + attr_reader :new_name # [Backref | DynaSymbol | GVar | SymbolLiteral] the old name of the method - attr_reader :right + attr_reader :old_name # [Array[ Comment | EmbDoc ]] the comments attached to this node attr_reader :comments - def initialize(left:, right:, location:) - @left = left - @right = right + def initialize(new_name:, old_name:, location:) + @new_name = new_name + @old_name = old_name @location = location @comments = [] end @@ -505,14 +505,14 @@ def accept(visitor) end def child_nodes - [left, right] + [new_name, old_name] end - def copy(left: nil, right: nil, location: nil) + def copy(new_name: nil, old_name: nil, location: nil) node = AliasNode.new( - left: left || self.left, - right: right || self.right, + new_name: new_name || self.new_name, + old_name: old_name || self.old_name, location: location || self.location ) @@ -523,31 +523,41 @@ def copy(left: nil, right: nil, location: nil) alias deconstruct child_nodes def deconstruct_keys(_keys) - { left: left, right: right, location: location, comments: comments } + { + new_name: new_name, + old_name: old_name, + location: location, + comments: comments + } end def format(q) keyword = "alias " - left_argument = AliasArgumentFormatter.new(left) + new_name_argument = AliasArgumentFormatter.new(new_name) q.group do q.text(keyword) - q.format(left_argument, stackable: false) + q.format(new_name_argument, stackable: false) q.group do q.nest(keyword.length) do - left_argument.comments.any? ? q.breakable_force : q.breakable_space - q.format(AliasArgumentFormatter.new(right), stackable: false) + if new_name_argument.comments.any? + q.breakable_force + else + q.breakable_space + end + q.format(AliasArgumentFormatter.new(old_name), stackable: false) end end end end def ===(other) - other.is_a?(AliasNode) && left === other.left && right === other.right + other.is_a?(AliasNode) && new_name === other.new_name && + old_name === other.old_name end def var_alias? - left.is_a?(GVar) + new_name.is_a?(GVar) end end diff --git a/lib/syntax_tree/parser.rb b/lib/syntax_tree/parser.rb index 825cd90e..12a2b181 100644 --- a/lib/syntax_tree/parser.rb +++ b/lib/syntax_tree/parser.rb @@ -447,16 +447,16 @@ def on___end__(value) # :call-seq: # on_alias: ( - # (DynaSymbol | SymbolLiteral) left, - # (DynaSymbol | SymbolLiteral) right + # (DynaSymbol | SymbolLiteral) new_name, + # (DynaSymbol | SymbolLiteral) old_name # ) -> AliasNode - def on_alias(left, right) + def on_alias(new_name, old_name) keyword = consume_keyword(:alias) AliasNode.new( - left: left, - right: right, - location: keyword.location.to(right.location) + new_name: new_name, + old_name: old_name, + location: keyword.location.to(old_name.location) ) end @@ -3902,14 +3902,14 @@ def on_until_mod(predicate, statement) end # :call-seq: - # on_var_alias: (GVar left, (Backref | GVar) right) -> AliasNode - def on_var_alias(left, right) + # on_var_alias: (GVar new_name, (Backref | GVar) old_name) -> AliasNode + def on_var_alias(new_name, old_name) keyword = consume_keyword(:alias) AliasNode.new( - left: left, - right: right, - location: keyword.location.to(right.location) + new_name: new_name, + old_name: old_name, + location: keyword.location.to(old_name.location) ) end diff --git a/lib/syntax_tree/translation/parser.rb b/lib/syntax_tree/translation/parser.rb index 8be4fc79..5a41fede 100644 --- a/lib/syntax_tree/translation/parser.rb +++ b/lib/syntax_tree/translation/parser.rb @@ -94,7 +94,7 @@ def visit(node) def visit_alias(node) s( :alias, - [visit(node.left), visit(node.right)], + [visit(node.new_name), visit(node.old_name)], smap_keyword_bare( srange_length(node.start_char, 5), srange_node(node) diff --git a/lib/syntax_tree/yarv/compiler.rb b/lib/syntax_tree/yarv/compiler.rb index 0f7e7372..9ea6a0f8 100644 --- a/lib/syntax_tree/yarv/compiler.rb +++ b/lib/syntax_tree/yarv/compiler.rb @@ -339,8 +339,8 @@ def visit_END(node) def visit_alias(node) iseq.putspecialobject(PutSpecialObject::OBJECT_VMCORE) iseq.putspecialobject(PutSpecialObject::OBJECT_CBASE) - visit(node.left) - visit(node.right) + visit(node.new_name) + visit(node.old_name) iseq.send(YARV.calldata(:"core#set_method_alias", 3)) end