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.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/gloss/type_checker.rb', line 11

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: ["**/*.rb"], ignore_patterns:       Array.new, signature_patterns: ["sig"])
  @top_level_decls = Set.new
  @rbs_gem_dir = Utils.gem_path_for("rbs")
  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)
end

Instance Method Details

#check_types(filepath, rb_str) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/gloss/type_checker.rb', line 69

def check_types(filepath, rb_str)
  @steep_target.add_source(filepath, rb_str)
  ready_for_checking!
  @steep_target.type_check
  (if @steep_target.status
.is_a?(Steep::Project::Target::SignatureErrorStatus)
    throw(:"error", @steep_target.status
.errors
.map() { |e|
      msg = case e
        when Steep::Diagnostic::Signature::UnknownTypeName
          "Unknown type name: #{e.name
.name} (#{e.location
.source
.[](/^.*$/)})"
        when Steep::Diagnostic::Signature::InvalidTypeApplication
          "Invalid type application: #{e.header_line}"
        when Steep::Diagnostic::Signature::DuplicatedMethodDefinition
          "Duplicated method: #{e.header_line}"
        else
          e.header_line
      end
"  SignatureSyntaxError:\n    Location: #{e.location}\n    Message: \"#{msg}\""        }
.join("\n"))
  end)
  @steep_target.source_files
.each() { |path, f|
    (if f.status
.is_a?(Steep::Project::SourceFile::ParseErrorStatus)
      e = f.status
.error
      throw(:"error", "#{e.class}: #{e.message}")
    end)
  }
@steep_target.status
.is_a?(Steep::Project::Target::TypeCheckStatus) && @steep_target.no_error? && @steep_target.errors
.empty?
end

#load_sig_path(path) ⇒ Object



107
108
109
110
111
112
# File 'lib/gloss/type_checker.rb', line 107

def load_sig_path(path)
  Gloss.logger
.debug("Loading signature file for #{path}")
  @steep_target.add_signature(path, File.open(path)
.read)
end

#ready_for_checking!Object



62
63
64
65
66
67
68
# File 'lib/gloss/type_checker.rb', line 62

def ready_for_checking!()
  @top_level_decls.each() { |decl|
    @env.<<(decl)
  }
  @env = @env.resolve_type_names
  @steep_target.instance_variable_set("@environment", @env)
end

#run(filepath, rb_str) ⇒ Object



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
56
57
58
59
60
61
# File 'lib/gloss/type_checker.rb', line 26

def run(filepath, rb_str)
  begin
    valid_types = check_types(filepath, rb_str)
  rescue ParseError => e
    throw(:"error", "")
  rescue  => e
    throw(:"error", "Type checking Error: #{e.message} (#{e.class})")
  end
  unless valid_types
    errors = @steep_target.errors
.map() { |e|
case e
        when Steep::Diagnostic::Ruby::NoMethod
          "Unknown method :#{e.method}, location: #{e.type
.location
.inspect}"
        when Steep::Diagnostic::Ruby::MethodBodyTypeMismatch
          "Invalid method body type - expected: #{e.expected}, actual: #{e.actual}"
        when Steep::Diagnostic::Ruby::IncompatibleArguments
          "Invalid argmuents - method type: #{e.method_types
.first}\nmethod name: #{e.method_name}"
        when Steep::Diagnostic::Ruby::ReturnTypeMismatch
          "Invalid return type - expected: #{e.expected}, actual: #{e.actual}"
        when Steep::Diagnostic::Ruby::IncompatibleAssignment
          "Invalid assignment - cannot assign #{e.rhs_type} to type #{e.lhs_type}"
        when Steep::Diagnostic::Ruby::UnexpectedBlockGiven
          "Unexpected block given"
        else
          "#{e.header_line}\n#{e.inspect}"
      end
    }
.join("\n")
    throw(:"error", errors)
  end
true
end