Skip to content

Commit

Permalink
Validate variable types
Browse files Browse the repository at this point in the history
  • Loading branch information
ksss committed Jan 15, 2025
1 parent bb9fe0d commit 4199fa1
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/rbs/cli/validate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ def validate_class_module_definition
end
InvalidTypeApplicationError.check!(type_name: member.name, params: params, args: member.args, location: member.location)
when AST::Members::Var
@validator.validate_variable(member)
void_type_context_validator(member.type)
if member.is_a?(AST::Members::ClassVariable)
no_self_type_validator(member.type)
Expand Down
4 changes: 4 additions & 0 deletions lib/rbs/validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ def validate_type_params(params, type_name: , method_name: nil, location:)
end
end

def validate_variable(var)
validate_type(var.type, context: nil)
end

def validate_class_alias(entry:)
case env.normalize_module_name?(entry.decl.new_name)
when nil
Expand Down
4 changes: 4 additions & 0 deletions sig/validator.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ module RBS
#
def validate_class_alias: (entry: Environment::ClassAliasEntry | Environment::ModuleAliasEntry) -> void

# Validates instance/class-instance/class variables.
#
def validate_variable: (AST::Members::Var) -> void

private

# Resolves relative type names to absolute type names in given context.
Expand Down
56 changes: 56 additions & 0 deletions test/validator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -391,4 +391,60 @@ def foo: () -> A
end
end
end

def test_validate_type__variable
SignatureManager.new do |manager|
manager.add_file("foo.rbs", <<-EOF)
class Foo
@foo: Nothing
@bar: Integer
self.@foo: Nothing
self.@bar: Integer
@@foo: Nothing
@@bar: Integer
end
EOF

manager.build do |env|
root = nil

resolver = RBS::Resolver::TypeNameResolver.new(env)
validator = RBS::Validator.new(env: env, resolver: resolver)

env.class_decls[RBS::TypeName.parse("::Foo")].decls.first.decl.members.tap do |members|
members[0].tap do |member|
assert_raises(RBS::NoTypeFoundError) do
validator.validate_variable(member)
end
end

members[1].tap do |member|
validator.validate_variable(member)
end

members[2].tap do |member|
assert_raises(RBS::NoTypeFoundError) do
validator.validate_variable(member)
end
end

members[3].tap do |member|
validator.validate_variable(member)
end

members[4].tap do |member|
assert_raises(RBS::NoTypeFoundError) do
validator.validate_variable(member)
end
end

members[5].tap do |member|
validator.validate_variable(member)
end
end
end
end
end
end

0 comments on commit 4199fa1

Please sign in to comment.