Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AliasNode: {left, right} -> {new_name, old_name} #370

Open
wants to merge 1 commit into
base: structure
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/syntax_tree/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 AndNode node.
Expand Down
4 changes: 2 additions & 2 deletions lib/syntax_tree/field_visitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions lib/syntax_tree/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
)
Expand Down
5 changes: 4 additions & 1 deletion lib/syntax_tree/mutation_visitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 AndNode node.
Expand Down
42 changes: 26 additions & 16 deletions lib/syntax_tree/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
)

Expand All @@ -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

Expand Down
22 changes: 11 additions & 11 deletions lib/syntax_tree/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -3912,14 +3912,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

Expand Down
2 changes: 1 addition & 1 deletion lib/syntax_tree/translation/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions lib/syntax_tree/yarv/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down