Class: Steep::Drivers::Check

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

Instance Attribute Summary collapse

Attributes included from Utils::DriverHelper

#steepfile

Instance Method Summary collapse

Methods included from Utils::DriverHelper

#load_config, #type_check

Constructor Details

#initialize(stdout:, stderr:) ⇒ Check

Returns a new instance of Check.



12
13
14
15
16
17
18
# File 'lib/steep/drivers/check.rb', line 12

def initialize(stdout:, stderr:)
  @stdout = stdout
  @stderr = stderr
  @command_line_patterns = []

  self.dump_all_types = false
end

Instance Attribute Details

#command_line_patternsObject (readonly)

Returns the value of attribute command_line_patterns.



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

def command_line_patterns
  @command_line_patterns
end

#dump_all_typesObject

Returns the value of attribute dump_all_types.



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

def dump_all_types
  @dump_all_types
end

#stderrObject (readonly)

Returns the value of attribute stderr.



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

def stderr
  @stderr
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



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

def stdout
  @stdout
end

Instance Method Details

#output_types(typing) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/steep/drivers/check.rb', line 70

def output_types(typing)
  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

#runObject



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
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/steep/drivers/check.rb', line 20

def run
  project = load_config()

  loader = Project::FileLoader.new(project: project)
  loader.load_sources(command_line_patterns)
  loader.load_signatures()

  type_check(project)

  if self.dump_all_types
    project.targets.each do |target|
      case (status = target.status)
      when Project::Target::TypeCheckStatus
        target.source_files.each_value do |file|
          case (file_status = file.status)
          when Project::SourceFile::TypeCheckStatus
            output_types(file_status.typing)
          end
        end
      end
    end
  end

  project.targets.each do |target|
    Steep.logger.tagged "target=#{target.name}" do
      case (status = target.status)
      when Project::Target::SignatureSyntaxErrorStatus
        printer = SignatureErrorPrinter.new(stdout: stdout, stderr: stderr)
        printer.print_syntax_errors(status.errors)
      when Project::Target::SignatureValidationErrorStatus
        printer = SignatureErrorPrinter.new(stdout: stdout, stderr: stderr)
        printer.print_semantic_errors(status.errors)
      when Project::Target::TypeCheckStatus
        status.type_check_sources.each do |source_file|
          source_file.errors.each do |error|
            error.print_to stdout
          end
        end
      end
    end
  end

  if project.targets.all? {|target| target.status.is_a?(Project::Target::TypeCheckStatus) && target.errors.empty? }
    Steep.logger.info "No type error found"
    return 0
  end

  1
end