mirror of
https://gitea.invidious.io/iv-org/shard-ameba.git
synced 2024-08-15 00:53:29 +00:00
Address feedback, add tests
This commit is contained in:
parent
14a9ec3a75
commit
6b2ddcb1d9
3 changed files with 35 additions and 11 deletions
30
spec/ameba/ast/variabling/type_def_variable_spec.cr
Normal file
30
spec/ameba/ast/variabling/type_def_variable_spec.cr
Normal file
|
@ -0,0 +1,30 @@
|
|||
require "../../../spec_helper"
|
||||
|
||||
module Ameba::AST
|
||||
describe TypeDecVariable do
|
||||
var = Crystal::Var.new("foo")
|
||||
declared_type = Crystal::Path.new("String")
|
||||
type_dec = Crystal::TypeDeclaration.new(var, declared_type)
|
||||
|
||||
describe "#initialize" do
|
||||
it "creates a new type dec variable" do
|
||||
variable = TypeDecVariable.new(type_dec)
|
||||
variable.node.should_not be_nil
|
||||
end
|
||||
end
|
||||
|
||||
describe "#name" do
|
||||
it "returns var name" do
|
||||
variable = TypeDecVariable.new(type_dec)
|
||||
variable.name.should eq var.name
|
||||
end
|
||||
|
||||
it "raises if type declaration is incorrect" do
|
||||
type_dec = Crystal::TypeDeclaration.new(declared_type, declared_type)
|
||||
expect_raises(Exception, "unsupported var node type: Crystal::Path") do
|
||||
TypeDecVariable.new(type_dec).name
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -135,13 +135,13 @@ module Ameba::AST
|
|||
|
||||
# Returns `true` if instance variable is assigned in this scope.
|
||||
def assigns_ivar?(name)
|
||||
arguments.find(&.name.== name) &&
|
||||
ivariables.find(&.name.== "@#{name}")
|
||||
arguments.any?(&.name.== name) &&
|
||||
ivariables.any?(&.name.== "@#{name}")
|
||||
end
|
||||
|
||||
# Returns `true` if type declaration variable is assigned in this scope.
|
||||
def assigns_type_dec?(name)
|
||||
type_dec_variables.find(&.name.== name) || outer_scope.try(&.assigns_type_dec?(name))
|
||||
type_dec_variables.any?(&.name.== name) || outer_scope.try(&.assigns_type_dec?(name))
|
||||
end
|
||||
|
||||
# Returns `true` if and only if current scope represents some
|
||||
|
|
|
@ -13,16 +13,10 @@ module Ameba::AST
|
|||
var = @node.var
|
||||
|
||||
case var
|
||||
when Crystal::Var
|
||||
var.name
|
||||
when Crystal::InstanceVar
|
||||
var.name
|
||||
when Crystal::ClassVar
|
||||
var.name
|
||||
when Crystal::Global
|
||||
when Crystal::Var, Crystal::InstanceVar, Crystal::ClassVar, Crystal::Global
|
||||
var.name
|
||||
else
|
||||
raise "unsupported type declaration var node"
|
||||
raise "unsupported var node type: #{var.class}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue