Class: Steep::Drivers::Check

Inherits:
Object
  • Object
show all
Includes:
Utils::EachSignature
Defined in:
lib/steep/drivers/check.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::EachSignature

#each_file_in_dir, #each_ruby_file, #each_ruby_source, #each_signature

Constructor Details

#initialize(source_paths:, signature_dirs:, stdout:, stderr:) ⇒ Check

Returns a new instance of Check.



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/steep/drivers/check.rb', line 18

def initialize(source_paths:, signature_dirs:, stdout:, stderr:)
  @source_paths = source_paths
  @signature_dirs = signature_dirs
  @stdout = stdout
  @stderr = stderr

  self.verbose = false
  self.accept_implicit_any = false
  self.dump_all_types = false
  self.fallback_any_is_error = false

  @labeling = ASTUtils::Labeling.new
end

Instance Attribute Details

#accept_implicit_anyObject

Returns the value of attribute accept_implicit_any.



10
11
12
# File 'lib/steep/drivers/check.rb', line 10

def accept_implicit_any
  @accept_implicit_any
end

#dump_all_typesObject

Returns the value of attribute dump_all_types.



11
12
13
# File 'lib/steep/drivers/check.rb', line 11

def dump_all_types
  @dump_all_types
end

#fallback_any_is_errorObject

Returns the value of attribute fallback_any_is_error.



12
13
14
# File 'lib/steep/drivers/check.rb', line 12

def fallback_any_is_error
  @fallback_any_is_error
end

#labelingObject (readonly)

Returns the value of attribute labeling.



14
15
16
# File 'lib/steep/drivers/check.rb', line 14

def labeling
  @labeling
end

#signature_dirsObject (readonly)

Returns the value of attribute signature_dirs.



5
6
7
# File 'lib/steep/drivers/check.rb', line 5

def signature_dirs
  @signature_dirs
end

#source_pathsObject (readonly)

Returns the value of attribute source_paths.



4
5
6
# File 'lib/steep/drivers/check.rb', line 4

def source_paths
  @source_paths
end

#stderrObject (readonly)

Returns the value of attribute stderr.



7
8
9
# File 'lib/steep/drivers/check.rb', line 7

def stderr
  @stderr
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



6
7
8
# File 'lib/steep/drivers/check.rb', line 6

def stdout
  @stdout
end

#verboseObject

Returns the value of attribute verbose.



9
10
11
# File 'lib/steep/drivers/check.rb', line 9

def verbose
  @verbose
end

Instance Method Details

#runObject



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
62
63
64
65
66
67
68
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
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/steep/drivers/check.rb', line 32

def run
  Steep.logger.level = Logger::DEBUG if verbose

  env = AST::Signature::Env.new

  each_signature(signature_dirs, verbose) do |signature|
    env.add signature
  end

  builder = Interface::Builder.new(signatures: env)
  check = Subtyping::Check.new(builder: builder)

  validator = Utils::Validator.new(stdout: stdout, stderr: stderr, verbose: verbose)

  validated = validator.run(env: env, builder: builder, check: check) do |sig|
    stderr.puts "Validating #{sig.name} (#{sig.location.name}:#{sig.location.start_line})..." if verbose
  end

  unless validated
    return 1
  end

  sources = []
  each_ruby_source(source_paths, verbose) do |source|
    sources << source
  end

  typing = Typing.new

  sources.each do |source|
    Steep.logger.tagged source.path do
      Steep.logger.debug "Typechecking..."
      annotations = source.annotations(block: source.node) || []

      pp annotations if verbose

      const_env = TypeInference::ConstantEnv.new(builder: check.builder, current_namespace: nil)
      type_env = TypeInference::TypeEnv.build(annotations: annotations,
                                              subtyping: check,
                                              const_env: const_env,
                                              signatures: check.builder.signatures)

      construction = TypeConstruction.new(
        checker: check,
        annotations: annotations,
        source: source,
        self_type: AST::Types::Name.new_instance(name: "::Object"),
        block_context: nil,
        module_context: TypeConstruction::ModuleContext.new(
          instance_type: nil,
          module_type: nil,
          implement_name: nil,
          current_namespace: nil,
          const_env: const_env
        ),
        method_context: nil,
        typing: typing,
        break_context: nil,
        type_env: type_env
        )
      construction.synthesize(source.node)
    end
  end

  if dump_all_types
    lines = []

    typing.nodes.each_value do |node|
      begin
        type = typing.type_of(node: node)
        lines << [node.loc.expression.source_buffer.name, [node.loc.last_line,node.loc.last_column], [node.loc.first_line, node.loc.column], node, type]
      rescue
        lines << [node.loc.expression.source_buffer.name, [node.loc.last_line,node.loc.last_column], [node.loc.first_line, node.loc.column], node, nil]
      end
    end

    lines.sort {|x,y| y <=> x }.reverse_each do |line|
      source = line[3].loc.expression.source
      stdout.puts "#{line[0]}:(#{line[2].join(",")}):(#{line[1].join(",")}):\t#{line[3].type}:\t#{line[4]}\t(#{source.split(/\n/).first})"
    end
  end

  typing.errors.each do |error|
    next if error.is_a?(Errors::FallbackAny) && !fallback_any_is_error
    error.print_to stdout
  end
end