mirror of
https://gitea.invidious.io/iv-org/shard-ameba.git
synced 2024-08-15 00:53:29 +00:00
6475c2bb25
* AST::Visitor -> AST::NodeVisitor * Scope & ScopeVisitor * Useless assignment rule * Instance vars and useless assignments * Multiple assigns one by one * Support outer scope * Variable used in the useless assignment * Support OpAssign & MultiAssign * Captured by block * Variable, Assignment, Reference & Refactoring * Variable has references, Assignment can be referenced * Branch entity * Handle useless assignments in branches * Handle assignments in a loop * Handle branch equality * Handle special var `$?` assignment * Improve captured by block stuff * Avoid assignments in property definitions (UselessAssign rule reports an warning) * Support MacroIf and MacroFor branches * Handle assignments with shadowed vars in inner scopes * Add method arguments as scope variables * Handle case if branch is blank * Top level scope * Handle case when branch is nop?
90 lines
2.6 KiB
Crystal
90 lines
2.6 KiB
Crystal
require "../../../spec_helper"
|
|
|
|
module Ameba::AST
|
|
describe Assignment do
|
|
node = Crystal::NilLiteral.new
|
|
scope = Scope.new as_node "foo = 1"
|
|
variable = Variable.new(Crystal::Var.new("foo"), scope)
|
|
|
|
describe "#initialize" do
|
|
it "creates a new assignment with node and var" do
|
|
assignment = Assignment.new(node, variable)
|
|
assignment.node.should_not be_nil
|
|
end
|
|
end
|
|
|
|
describe "#reference=" do
|
|
it "creates a new reference" do
|
|
assignment = Assignment.new(node, variable)
|
|
assignment.referenced = true
|
|
assignment.referenced?.should be_true
|
|
end
|
|
end
|
|
|
|
describe "delegation" do
|
|
it "delegates location" do
|
|
assignment = Assignment.new(node, variable)
|
|
assignment.location.should eq node.location
|
|
end
|
|
|
|
it "delegates to_s" do
|
|
assignment = Assignment.new(node, variable)
|
|
assignment.to_s.should eq node.to_s
|
|
end
|
|
|
|
it "delegates scope" do
|
|
assignment = Assignment.new(node, variable)
|
|
assignment.scope.should eq variable.scope
|
|
end
|
|
end
|
|
|
|
describe "#branch" do
|
|
it "returns the branch of the assignment" do
|
|
nodes = as_nodes %(
|
|
def method(a)
|
|
if a
|
|
a = 3 # --> Crystal::Expressions
|
|
puts a
|
|
end
|
|
end
|
|
)
|
|
|
|
scope = Scope.new nodes.def_nodes.first
|
|
variable = Variable.new(nodes.var_nodes.first, scope)
|
|
assignment = Assignment.new(nodes.assign_nodes.first, variable)
|
|
assignment.branch.should_not be_nil
|
|
assignment.branch.not_nil!.node.class.should eq Crystal::Expressions
|
|
end
|
|
|
|
it "returns inner branch" do
|
|
nodes = as_nodes %(
|
|
def method(a, b)
|
|
if a
|
|
if b
|
|
a = 3 # --> Crystal::Assign
|
|
end
|
|
end
|
|
end
|
|
)
|
|
scope = Scope.new nodes.def_nodes.first
|
|
variable = Variable.new(nodes.var_nodes.first, scope)
|
|
assignment = Assignment.new(nodes.assign_nodes.first, variable)
|
|
assignment.branch.should_not be_nil
|
|
assignment.branch.not_nil!.node.class.should eq Crystal::Assign
|
|
end
|
|
|
|
it "returns nil if assignment does not have a branch" do
|
|
nodes = as_nodes %(
|
|
def method(a)
|
|
a = 2
|
|
end
|
|
)
|
|
|
|
scope = Scope.new nodes.def_nodes.first
|
|
variable = Variable.new(nodes.var_nodes.first, scope)
|
|
assignment = Assignment.new(nodes.assign_nodes.first, variable)
|
|
assignment.branch.should be_nil
|
|
end
|
|
end
|
|
end
|
|
end
|