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_ruby_file, #each_ruby_source, #each_signature

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

def run
  if type_name
    type = Parser.parse_type(type_name)

    env = AST::Signature::Env.new

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

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

      interface = check.resolve(type, with_initialize: true)

      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
    rescue Steep::Subtyping::Check::CannotResolveError
      stderr.puts "🤔 #{Rainbow(type.to_s).red} cannot be resolved to interface"
    end
  else
    stderr.puts "Pass a type name to command line"
  end
end