Skip to content

Commit afa9f88

Browse files
committed
AliasNode: {left, right} -> {new_name, old_name}
- Fixes #348 except for the `keyword_loc`
1 parent d08d23b commit afa9f88

File tree

8 files changed

+55
-37
lines changed

8 files changed

+55
-37
lines changed

Diff for: lib/syntax_tree/dsl.rb

+6-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,12 @@ def EndContent(value)
3232
end
3333

3434
# Create a new AliasNode node.
35-
def AliasNode(left, right)
36-
AliasNode.new(left: left, right: right, location: Location.default)
35+
def AliasNode(new_name, old_name)
36+
AliasNode.new(
37+
new_name: new_name,
38+
old_name: old_name,
39+
location: Location.default
40+
)
3741
end
3842

3943
# Create a new ARef node.

Diff for: lib/syntax_tree/field_visitor.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ def visit_aref_field(node)
6767

6868
def visit_alias(node)
6969
node(node, "alias") do
70-
field("left", node.left)
71-
field("right", node.right)
70+
field("new_name", node.new_name)
71+
field("old_name", node.old_name)
7272
comments(node)
7373
end
7474
end

Diff for: lib/syntax_tree/index.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,8 @@ def initialize
475475

476476
visit_methods do
477477
def visit_alias(node)
478-
if node.left.is_a?(SymbolLiteral) && node.right.is_a?(SymbolLiteral)
478+
if node.new_name.is_a?(SymbolLiteral) &&
479+
node.old_name.is_a?(SymbolLiteral)
479480
location =
480481
Location.new(
481482
node.location.start_line,
@@ -484,7 +485,7 @@ def visit_alias(node)
484485

485486
results << AliasMethodDefinition.new(
486487
nesting.dup,
487-
node.left.value.value.to_sym,
488+
node.new_name.value.value.to_sym,
488489
location,
489490
comments_for(node)
490491
)

Diff for: lib/syntax_tree/mutation_visitor.rb

+4-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,10 @@ def visit___end__(node)
6262

6363
# Visit a AliasNode node.
6464
def visit_alias(node)
65-
node.copy(left: visit(node.left), right: visit(node.right))
65+
node.copy(
66+
new_name: visit(node.new_name),
67+
old_name: visit(node.old_name)
68+
)
6669
end
6770

6871
# Visit a ARef node.

Diff for: lib/syntax_tree/node.rb

+26-16
Original file line numberDiff line numberDiff line change
@@ -485,17 +485,17 @@ def format(q)
485485
end
486486

487487
# [DynaSymbol | GVar | SymbolLiteral] the new name of the method
488-
attr_reader :left
488+
attr_reader :new_name
489489

490490
# [Backref | DynaSymbol | GVar | SymbolLiteral] the old name of the method
491-
attr_reader :right
491+
attr_reader :old_name
492492

493493
# [Array[ Comment | EmbDoc ]] the comments attached to this node
494494
attr_reader :comments
495495

496-
def initialize(left:, right:, location:)
497-
@left = left
498-
@right = right
496+
def initialize(new_name:, old_name:, location:)
497+
@new_name = new_name
498+
@old_name = old_name
499499
@location = location
500500
@comments = []
501501
end
@@ -505,14 +505,14 @@ def accept(visitor)
505505
end
506506

507507
def child_nodes
508-
[left, right]
508+
[new_name, old_name]
509509
end
510510

511-
def copy(left: nil, right: nil, location: nil)
511+
def copy(new_name: nil, old_name: nil, location: nil)
512512
node =
513513
AliasNode.new(
514-
left: left || self.left,
515-
right: right || self.right,
514+
new_name: new_name || self.new_name,
515+
old_name: old_name || self.old_name,
516516
location: location || self.location
517517
)
518518

@@ -523,31 +523,41 @@ def copy(left: nil, right: nil, location: nil)
523523
alias deconstruct child_nodes
524524

525525
def deconstruct_keys(_keys)
526-
{ left: left, right: right, location: location, comments: comments }
526+
{
527+
new_name: new_name,
528+
old_name: old_name,
529+
location: location,
530+
comments: comments
531+
}
527532
end
528533

529534
def format(q)
530535
keyword = "alias "
531-
left_argument = AliasArgumentFormatter.new(left)
536+
new_name_argument = AliasArgumentFormatter.new(new_name)
532537

533538
q.group do
534539
q.text(keyword)
535-
q.format(left_argument, stackable: false)
540+
q.format(new_name_argument, stackable: false)
536541
q.group do
537542
q.nest(keyword.length) do
538-
left_argument.comments.any? ? q.breakable_force : q.breakable_space
539-
q.format(AliasArgumentFormatter.new(right), stackable: false)
543+
if new_name_argument.comments.any?
544+
q.breakable_force
545+
else
546+
q.breakable_space
547+
end
548+
q.format(AliasArgumentFormatter.new(old_name), stackable: false)
540549
end
541550
end
542551
end
543552
end
544553

545554
def ===(other)
546-
other.is_a?(AliasNode) && left === other.left && right === other.right
555+
other.is_a?(AliasNode) && new_name === other.new_name &&
556+
old_name === other.old_name
547557
end
548558

549559
def var_alias?
550-
left.is_a?(GVar)
560+
new_name.is_a?(GVar)
551561
end
552562
end
553563

Diff for: lib/syntax_tree/parser.rb

+11-11
Original file line numberDiff line numberDiff line change
@@ -447,16 +447,16 @@ def on___end__(value)
447447

448448
# :call-seq:
449449
# on_alias: (
450-
# (DynaSymbol | SymbolLiteral) left,
451-
# (DynaSymbol | SymbolLiteral) right
450+
# (DynaSymbol | SymbolLiteral) new_name,
451+
# (DynaSymbol | SymbolLiteral) old_name
452452
# ) -> AliasNode
453-
def on_alias(left, right)
453+
def on_alias(new_name, old_name)
454454
keyword = consume_keyword(:alias)
455455

456456
AliasNode.new(
457-
left: left,
458-
right: right,
459-
location: keyword.location.to(right.location)
457+
new_name: new_name,
458+
old_name: old_name,
459+
location: keyword.location.to(old_name.location)
460460
)
461461
end
462462

@@ -3902,14 +3902,14 @@ def on_until_mod(predicate, statement)
39023902
end
39033903

39043904
# :call-seq:
3905-
# on_var_alias: (GVar left, (Backref | GVar) right) -> AliasNode
3906-
def on_var_alias(left, right)
3905+
# on_var_alias: (GVar new_name, (Backref | GVar) old_name) -> AliasNode
3906+
def on_var_alias(new_name, old_name)
39073907
keyword = consume_keyword(:alias)
39083908

39093909
AliasNode.new(
3910-
left: left,
3911-
right: right,
3912-
location: keyword.location.to(right.location)
3910+
new_name: new_name,
3911+
old_name: old_name,
3912+
location: keyword.location.to(old_name.location)
39133913
)
39143914
end
39153915

Diff for: lib/syntax_tree/translation/parser.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def visit(node)
9494
def visit_alias(node)
9595
s(
9696
:alias,
97-
[visit(node.left), visit(node.right)],
97+
[visit(node.new_name), visit(node.old_name)],
9898
smap_keyword_bare(
9999
srange_length(node.start_char, 5),
100100
srange_node(node)

Diff for: lib/syntax_tree/yarv/compiler.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,8 @@ def visit_END(node)
339339
def visit_alias(node)
340340
iseq.putspecialobject(PutSpecialObject::OBJECT_VMCORE)
341341
iseq.putspecialobject(PutSpecialObject::OBJECT_CBASE)
342-
visit(node.left)
343-
visit(node.right)
342+
visit(node.new_name)
343+
visit(node.old_name)
344344
iseq.send(YARV.calldata(:"core#set_method_alias", 3))
345345
end
346346

0 commit comments

Comments
 (0)