Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 672abb2

Browse files
committed
Fold VarAlias into Alias
1 parent b97b9cb commit 672abb2

File tree

6 files changed

+13
-66
lines changed

6 files changed

+13
-66
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
1111
- Nodes no longer have a `comments:` keyword on their initializers. By default, they initialize to an empty array. If you were previously passing comments into the initializer, you should now create the node first, then call `node.comments.concat` to add your comments.
1212
- `IfMod` and `UnlessMod` are no longer nodes. Instead, they have been folded into `If` and `Unless`, respectively. The `If` and `Unless` nodes now have a `modifier?` method to tell you if they were original found in the modifier form.
1313
- `WhileMod` and `UntilMod` are no longer nodes. Instead, they have been folded into `While` and `Until`, respectively. The `While` and `Until` nodes now have a `modifier?` method to tell you if they were originally found in the modifier form.
14+
- `VarAlias` is no longer a node. Instead it has been folded into the `Alias` node. The `Alias` node now has a `var_alias?` method to tell you if it is aliasing a global variable.
1415

1516
## [4.3.0] - 2022-10-28
1617

lib/syntax_tree/node.rb

Lines changed: 9 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ class Alias < Node
359359
# Formats an argument to the alias keyword. For symbol literals it uses the
360360
# value of the symbol directly to look like bare words.
361361
class AliasArgumentFormatter
362-
# [DynaSymbol | SymbolLiteral] the argument being passed to alias
362+
# [Backref | DynaSymbol | GVar | SymbolLiteral] the argument being passed to alias
363363
attr_reader :argument
364364

365365
def initialize(argument)
@@ -383,10 +383,10 @@ def format(q)
383383
end
384384
end
385385

386-
# [DynaSymbol | SymbolLiteral] the new name of the method
386+
# [DynaSymbol | GVar | SymbolLiteral] the new name of the method
387387
attr_reader :left
388388

389-
# [DynaSymbol | SymbolLiteral] the old name of the method
389+
# [Backref | DynaSymbol | GVar | SymbolLiteral] the old name of the method
390390
attr_reader :right
391391

392392
# [Array[ Comment | EmbDoc ]] the comments attached to this node
@@ -428,6 +428,10 @@ def format(q)
428428
end
429429
end
430430
end
431+
432+
def var_alias?
433+
left.is_a?(GVar)
434+
end
431435
end
432436

433437
# ARef represents when you're pulling a value out of a collection at a
@@ -5399,8 +5403,7 @@ def ternaryable?(statement)
53995403
case statement
54005404
when Alias, Assign, Break, Command, CommandCall, Heredoc, If, IfOp,
54015405
Lambda, MAssign, Next, OpAssign, RescueMod, Return, Return0, Super,
5402-
Undef, Unless, Until, VarAlias, VoidStmt, While, Yield, Yield0,
5403-
ZSuper
5406+
Undef, Unless, Until, VoidStmt, While, Yield, Yield0, ZSuper
54045407
# This is a list of nodes that should not be allowed to be a part of a
54055408
# ternary clause.
54065409
false
@@ -5674,7 +5677,7 @@ def format(q)
56745677
force_flat = [
56755678
Alias, Assign, Break, Command, CommandCall, Heredoc, If, IfOp, Lambda,
56765679
MAssign, Next, OpAssign, RescueMod, Return, Return0, Super, Undef,
5677-
Unless, VarAlias, VoidStmt, Yield, Yield0, ZSuper
5680+
Unless, VoidStmt, Yield, Yield0, ZSuper
56785681
]
56795682

56805683
if q.parent.is_a?(Paren) || force_flat.include?(truthy.class) ||
@@ -9505,52 +9508,6 @@ def modifier?
95059508
end
95069509
end
95079510

9508-
# VarAlias represents when you're using the +alias+ keyword with global
9509-
# variable arguments.
9510-
#
9511-
# alias $new $old
9512-
#
9513-
class VarAlias < Node
9514-
# [GVar] the new alias of the variable
9515-
attr_reader :left
9516-
9517-
# [Backref | GVar] the current name of the variable to be aliased
9518-
attr_reader :right
9519-
9520-
# [Array[ Comment | EmbDoc ]] the comments attached to this node
9521-
attr_reader :comments
9522-
9523-
def initialize(left:, right:, location:)
9524-
@left = left
9525-
@right = right
9526-
@location = location
9527-
@comments = []
9528-
end
9529-
9530-
def accept(visitor)
9531-
visitor.visit_var_alias(self)
9532-
end
9533-
9534-
def child_nodes
9535-
[left, right]
9536-
end
9537-
9538-
alias deconstruct child_nodes
9539-
9540-
def deconstruct_keys(_keys)
9541-
{ left: left, right: right, location: location, comments: comments }
9542-
end
9543-
9544-
def format(q)
9545-
keyword = "alias "
9546-
9547-
q.text(keyword)
9548-
q.format(left)
9549-
q.text(" ")
9550-
q.format(right)
9551-
end
9552-
end
9553-
95549511
# VarField represents a variable that is being assigned a value. As such, it
95559512
# is always a child of an assignment type node.
95569513
#

lib/syntax_tree/parser.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3642,11 +3642,11 @@ def on_until_mod(predicate, statement)
36423642
end
36433643

36443644
# :call-seq:
3645-
# on_var_alias: (GVar left, (Backref | GVar) right) -> VarAlias
3645+
# on_var_alias: (GVar left, (Backref | GVar) right) -> Alias
36463646
def on_var_alias(left, right)
36473647
keyword = consume_keyword(:alias)
36483648

3649-
VarAlias.new(
3649+
Alias.new(
36503650
left: left,
36513651
right: right,
36523652
location: keyword.location.to(right.location)

lib/syntax_tree/visitor.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -431,9 +431,6 @@ class Visitor < BasicVisitor
431431
# Visit an Until node.
432432
alias visit_until visit_child_nodes
433433

434-
# Visit a VarAlias node.
435-
alias visit_var_alias visit_child_nodes
436-
437434
# Visit a VarField node.
438435
alias visit_var_field visit_child_nodes
439436

lib/syntax_tree/visitor/field_visitor.rb

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -982,14 +982,6 @@ def visit_until(node)
982982
end
983983
end
984984

985-
def visit_var_alias(node)
986-
node(node, "var_alias") do
987-
field("left", node.left)
988-
field("right", node.right)
989-
comments(node)
990-
end
991-
end
992-
993985
def visit_var_field(node)
994986
node(node, "var_field") do
995987
field("value", node.value)

test/node_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -935,7 +935,7 @@ def test_until_mod
935935
end
936936

937937
def test_var_alias
938-
assert_node(VarAlias, "alias $new $old")
938+
assert_node(Alias, "alias $new $old")
939939
end
940940

941941
def test_var_field

0 commit comments

Comments
 (0)