Address feedback, add tests

This commit is contained in:
Vitalii Elenhaupt 2023-02-07 17:19:04 +02:00
parent 14a9ec3a75
commit 6b2ddcb1d9
No known key found for this signature in database
GPG key ID: CD0BF17825928BC0
3 changed files with 35 additions and 11 deletions

View 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

View file

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

View file

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