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?
72 lines
1.9 KiB
Crystal
72 lines
1.9 KiB
Crystal
require "../../spec_helper"
|
|
|
|
module Ameba::AST
|
|
describe Scope do
|
|
describe "#initialize" do
|
|
source = "a = 2"
|
|
|
|
it "assigns outer scope" do
|
|
root = Scope.new as_node(source)
|
|
child = Scope.new as_node(source), root
|
|
child.outer_scope.should_not be_nil
|
|
end
|
|
|
|
it "assigns node" do
|
|
scope = Scope.new as_node(source)
|
|
scope.node.should_not be_nil
|
|
end
|
|
end
|
|
end
|
|
|
|
describe "#add_variable" do
|
|
it "adds a new variable to the scope" do
|
|
scope = Scope.new as_node("")
|
|
scope.add_variable(Crystal::Var.new "foo")
|
|
scope.variables.any?.should be_true
|
|
end
|
|
end
|
|
|
|
describe "#find_variable" do
|
|
it "returns the variable in the scope by name" do
|
|
scope = Scope.new as_node("foo = 1")
|
|
scope.add_variable Crystal::Var.new "foo"
|
|
scope.find_variable("foo").should_not be_nil
|
|
end
|
|
|
|
it "returns nil if variable not exist in this scope" do
|
|
scope = Scope.new as_node("foo = 1")
|
|
scope.find_variable("bar").should be_nil
|
|
end
|
|
end
|
|
|
|
describe "#assign_variable" do
|
|
it "creates a new assignment" do
|
|
scope = Scope.new as_node("foo = 1")
|
|
scope.add_variable Crystal::Var.new "foo"
|
|
scope.assign_variable(Crystal::Var.new "foo")
|
|
scope.find_variable("foo").not_nil!.assignments.size.should eq 1
|
|
end
|
|
|
|
it "does not create the assignment if variable is wrong" do
|
|
scope = Scope.new as_node("foo = 1")
|
|
scope.add_variable Crystal::Var.new "foo"
|
|
scope.assign_variable(Crystal::Var.new "bar")
|
|
scope.find_variable("foo").not_nil!.assignments.size.should eq 0
|
|
end
|
|
end
|
|
|
|
describe "#block?" do
|
|
it "returns true if Crystal::Block" do
|
|
nodes = as_nodes %(
|
|
3.times {}
|
|
)
|
|
scope = Scope.new nodes.block_nodes.first
|
|
scope.block?.should be_true
|
|
end
|
|
|
|
it "returns false otherwise" do
|
|
scope = Scope.new as_node "a = 1"
|
|
scope.block?.should be_false
|
|
end
|
|
end
|
|
end
|