Class: Steep::Drivers::PrintInterface

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::EachSignature

#each_file_in_dir, #each_file_in_path

Constructor Details

#initialize(type_name:, signature_dirs:, stdout:, stderr:) ⇒ PrintInterface

Returns a new instance of PrintInterface.



11
12
13
14
15
16
# File 'lib/steep/drivers/print_interface.rb', line 11

def initialize(type_name:, signature_dirs:, stdout:, stderr:)
  @type_name = type_name
  @signature_dirs = signature_dirs
  @stdout = stdout
  @stderr = stderr
end

Instance Attribute Details

#signature_dirsObject (readonly)

Returns the value of attribute signature_dirs.



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

def signature_dirs
  @signature_dirs
end

#stderrObject (readonly)

Returns the value of attribute stderr.



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

def stderr
  @stderr
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



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

def stdout
  @stdout
end

#type_nameObject (readonly)

Returns the value of attribute type_name.



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

def type_name
  @type_name
end

Instance Method Details

#runObject



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
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
# File 'lib/steep/drivers/print_interface.rb', line 18

def run
  if type_name
    type = Parser.parse_type(type_name)
    project = Project.new()

    signature_dirs.each do |path|
      each_file_in_path(".rbi", path) do |file_path|
        file = Project::SignatureFile.new(path: file_path)
        file.content = file_path.read
        project.signature_files[file_path] = file
      end
    end

    project.reload_signature

    case sig = project.signature
    when Project::SignatureLoaded
      begin
        check = sig.check
        interface = check.resolve(type)

        stdout.puts "#{type}"
        stdout.puts "- Instance variables:"
        interface.ivars.each do |name, type|
          puts "  - #{name}: #{type}"
        end
        stdout.puts "- Methods:"
        interface.methods.each do |name, method|
          puts "  - #{Rainbow(name).blue}:"
          method.types.each do |method_type|
            loc = if method_type.location
                    "#{method_type.location.buffer.name}:#{method_type.location.to_s}"
                  else
                    "no location"
                  end
            puts "    - #{Rainbow(method_type.to_s).red} (#{loc})"
          end
        end
        0
      rescue Steep::Subtyping::Check::CannotResolveError
        stderr.puts "🤔 #{Rainbow(type.to_s).red} cannot be resolved to interface"
        1
      end

    when Project::SignatureHasError
      sig.errors.each do |error|
        case error
        when Interface::Instantiated::InvalidMethodOverrideError
          stdout.puts "😱 #{error.message}"
          error.result.trace.each do |s, t|
            case s
            when Interface::Method
              stdout.puts "  #{s.name}(#{s.type_name}) <: #{t.name}(#{t.type_name})"
            when Interface::MethodType
              stdout.puts "  #{s} <: #{t} (#{s.location&.name||"?"}:#{s.location&.start_line||"?"})"
            else
              stdout.puts "  #{s} <: #{t}"
            end
          end
          stdout.puts "  🚨 #{error.result.error.message}"
        when Interface::Instantiated::InvalidIvarOverrideError
          stdout.puts "😱 #{error.message}"
        else
          stdout.puts "😱 #{error.inspect}"
        end
      end

      1
    end
  else
    stderr.puts "Pass a type name to command line"
    1
  end
end