Class: Gloss::TypeChecker

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

Defined Under Namespace

Modules: Strictness

Instance Method Summary collapse

Constructor Details

#initialize(src_dir) ⇒ TypeChecker

Returns a new instance of TypeChecker.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/gloss/type_checker.rb', line 14

def initialize(src_dir)
  options = Steep::Project::Options.new
case Config.type_checking_strictness
    when Strictness::Strict
      options.apply_strict_typing_options!
    when Strictness::Lenient
      options.apply_lenient_typing_options!
    else
      options.apply_default_typing_options!
  end
  @steep_target = Steep::Project::Target.new(name: "gloss", options: options, 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(src_dir)
.realpath)
  project.targets
.<<(@steep_target)
  loader = Steep::Project::FileLoader.new(project: project)
end

Instance Method Details

#check_types(filepath, rb_str) ⇒ Object



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
107
108
109
110
111
112
113
114
115
# File 'lib/gloss/type_checker.rb', line 78

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



116
117
118
119
120
121
# File 'lib/gloss/type_checker.rb', line 116

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



71
72
73
74
75
76
77
# File 'lib/gloss/type_checker.rb', line 71

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



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
62
63
64
65
66
67
68
69
70
# File 'lib/gloss/type_checker.rb', line 35

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}"
      end
    }
.join("\n")
    throw(:"error", errors)
  end
true
end