Class: Gloss::TypeChecker

Inherits:
Object
  • Object
show all
Defined in:
lib/gloss/type_checker.rb

Defined Under Namespace

Classes: Project

Instance Method Summary collapse

Constructor Details

#initializeTypeChecker

Returns a new instance of TypeChecker.



10
11
12
13
14
15
16
# File 'lib/gloss/type_checker.rb', line 10

def initialize()
  @steep_target = Steep::Project::Target.new(name: "gloss", options:       Steep::Project::Options.new
.tap() { |o|
    o.allow_unknown_constant_assignment=(true)
  }, source_patterns: ["gloss.rb"], ignore_patterns:       Array.new, signature_patterns: ["sig"])
  @top_level_decls = {}
end

Instance Method Details

#check_types(rb_str) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/gloss/type_checker.rb', line 45

def check_types(rb_str)
  env_loader = RBS::EnvironmentLoader.new
  env = RBS::Environment.from_loader(env_loader)
  project = Steep::Project.new(steepfile_path:       Pathname.new(Config.src_dir)
.realpath)
  project.targets
.<<(@steep_target)
  loader = Steep::Project::FileLoader.new(project: project)
  loader.load_signatures
  @steep_target.add_source("gloss.rb", rb_str)
  @top_level_decls.each() { |_, decl|
    env.<<(decl)
  }
  env = env.resolve_type_names
  @steep_target.instance_variable_set("@environment", env)
  @steep_target.type_check
@steep_target.status
.is_a?(Steep::Project::Target::TypeCheckStatus) && @steep_target.no_error? && @steep_target.errors
.empty?
end

#run(rb_str) ⇒ Object



17
18
19
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
# File 'lib/gloss/type_checker.rb', line 17

def run(rb_str)
  unless       check_types(rb_str)
    raise(Errors::TypeError, @steep_target.errors
.map() { |e|
case e
        when Steep::Errors::NoMethod
          "Unknown method :#{e.method}, location: #{e.type
.location
.inspect}"
        when Steep::Errors::MethodBodyTypeMismatch
          "Invalid method body type - expected: #{e.expected}, actual: #{e.actual}"
        when Steep::Errors::IncompatibleArguments
          "  Invalid argmuents - method type: #{e.method_type}\n                      method name: #{e.method_type
.method_decls
.first
.method_name}"
        when Steep::Errors::ReturnTypeMismatch
          "Invalid return type - expected: #{e.expected}, actual: #{e.actual}"
        when Steep::Errors::IncompatibleAssignment
          "Invalid assignment - cannot assign #{e.rhs_type} to type #{e.lhs_type}"
        else
          e.inspect
      end
    }
.join("\n"))
  end
true
end