Class: RBS::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/rbs/validator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env:, resolver:) ⇒ Validator

Returns a new instance of Validator.



7
8
9
10
11
# File 'lib/rbs/validator.rb', line 7

def initialize(env:, resolver:)
  @env = env
  @resolver = resolver
  @definition_builder = DefinitionBuilder.new(env: env)
end

Instance Attribute Details

#definition_builderObject (readonly)

Returns the value of attribute definition_builder.



5
6
7
# File 'lib/rbs/validator.rb', line 5

def definition_builder
  @definition_builder
end

#envObject (readonly)

Returns the value of attribute env.



3
4
5
# File 'lib/rbs/validator.rb', line 3

def env
  @env
end

#resolverObject (readonly)

Returns the value of attribute resolver.



4
5
6
# File 'lib/rbs/validator.rb', line 4

def resolver
  @resolver
end

Instance Method Details

#absolute_type(type, context:) ⇒ Object



13
14
15
16
17
# File 'lib/rbs/validator.rb', line 13

def absolute_type(type, context:)
  type.map_type_name do |type_name, _, type|
    resolver.resolve(type_name, context: context) || yield(type)
  end
end

#type_alias_dependencyObject



86
87
88
# File 'lib/rbs/validator.rb', line 86

def type_alias_dependency
  @type_alias_dependency ||= TypeAliasDependency.new(env: env)
end

#type_alias_regularityObject



90
91
92
# File 'lib/rbs/validator.rb', line 90

def type_alias_regularity
  @type_alias_regularity ||= TypeAliasRegularity.validate(env: env)
end

#validate_type(type, context:) ⇒ Object

Validates presence of the relative type, and application arity match.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rbs/validator.rb', line 20

def validate_type(type, context:)
  case type
  when Types::ClassInstance, Types::Interface, Types::Alias
    # @type var type: Types::ClassInstance | Types::Interface | Types::Alias
    if type.name.namespace.relative?
      type = _ = absolute_type(type, context: context) do |_|
        NoTypeFoundError.check!(type.name.absolute!, env: env, location: type.location)
      end
    end

    definition_builder.validate_type_name(type.name, type.location)

    type_params = case type
                  when Types::ClassInstance
                    env.class_decls[type.name].type_params
                  when Types::Interface
                    env.interface_decls[type.name].decl.type_params
                  when Types::Alias
                    env.alias_decls[type.name].decl.type_params
                  end

    InvalidTypeApplicationError.check!(
      type_name: type.name,
      args: type.args,
      params: type_params.each.map(&:name),
      location: type.location
    )

  when Types::ClassSingleton
    definition_builder.validate_type_presence(type)
  end

  type.each_type do |type|
    validate_type(type, context: context)
  end
end

#validate_type_alias(entry:) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/rbs/validator.rb', line 57

def validate_type_alias(entry:)
  type_name = entry.decl.name

  if type_alias_dependency.circular_definition?(type_name)
    location = entry.decl.location or raise
    raise RecursiveTypeAliasError.new(alias_names: [type_name], location: location)
  end

  if diagnostic = type_alias_regularity.nonregular?(type_name)
    location = entry.decl.location or raise
    raise NonregularTypeAliasError.new(diagnostic: diagnostic, location: location)
  end

  unless entry.decl.type_params.empty?
    calculator = VarianceCalculator.new(builder: definition_builder)
    result = calculator.in_type_alias(name: type_name)
    if set = result.incompatible?(entry.decl.type_params)
      set.each do |param_name|
        param = entry.decl.type_params[param_name] or raise
        raise InvalidVarianceAnnotationError.new(
          type_name: type_name,
          param: param,
          location: entry.decl.type.location
        )
      end
    end
  end
end