Class: RBI::TypePrinter

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/rbi/rbs_printer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTypePrinter

Returns a new instance of TypePrinter.



787
788
789
# File 'lib/rbi/rbs_printer.rb', line 787

def initialize
  @string = T.let(String.new, String)
end

Instance Attribute Details

#stringObject (readonly)

Returns the value of attribute string.



784
785
786
# File 'lib/rbi/rbs_printer.rb', line 784

def string
  @string
end

Instance Method Details

#visit(node) ⇒ Object



792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
# File 'lib/rbi/rbs_printer.rb', line 792

def visit(node)
  case node
  when Type::Simple
    visit_simple(node)
  when Type::Boolean
    visit_boolean(node)
  when Type::Generic
    visit_generic(node)
  when Type::Anything
    visit_anything(node)
  when Type::Void
    visit_void(node)
  when Type::NoReturn
    visit_no_return(node)
  when Type::Untyped
    visit_untyped(node)
  when Type::SelfType
    visit_self_type(node)
  when Type::AttachedClass
    visit_attached_class(node)
  when Type::Nilable
    visit_nilable(node)
  when Type::ClassOf
    visit_class_of(node)
  when Type::All
    visit_all(node)
  when Type::Any
    visit_any(node)
  when Type::Tuple
    visit_tuple(node)
  when Type::Shape
    visit_shape(node)
  when Type::Proc
    visit_proc(node)
  when Type::TypeParameter
    visit_type_parameter(node)
  when Type::Class
    visit_class(node)
  else
    raise Error, "Unhandled node: #{node.class}"
  end
end

#visit_all(type) ⇒ Object



900
901
902
903
904
905
906
907
# File 'lib/rbi/rbs_printer.rb', line 900

def visit_all(type)
  @string << "("
  type.types.each_with_index do |arg, index|
    visit(arg)
    @string << " & " if index < type.types.size - 1
  end
  @string << ")"
end

#visit_any(type) ⇒ Object



910
911
912
913
914
915
916
917
# File 'lib/rbi/rbs_printer.rb', line 910

def visit_any(type)
  @string << "("
  type.types.each_with_index do |arg, index|
    visit(arg)
    @string << " | " if index < type.types.size - 1
  end
  @string << ")"
end

#visit_anything(type) ⇒ Object



857
858
859
# File 'lib/rbi/rbs_printer.rb', line 857

def visit_anything(type)
  @string << "top"
end

#visit_attached_class(type) ⇒ Object



882
883
884
# File 'lib/rbi/rbs_printer.rb', line 882

def visit_attached_class(type)
  @string << "instance"
end

#visit_boolean(type) ⇒ Object



841
842
843
# File 'lib/rbi/rbs_printer.rb', line 841

def visit_boolean(type)
  @string << "bool"
end

#visit_class(type) ⇒ Object



962
963
964
965
966
# File 'lib/rbi/rbs_printer.rb', line 962

def visit_class(type)
  @string << "singleton("
  visit(type.type)
  @string << ")"
end

#visit_class_of(type) ⇒ Object



893
894
895
896
897
# File 'lib/rbi/rbs_printer.rb', line 893

def visit_class_of(type)
  @string << "singleton("
  visit(type.type)
  @string << ")"
end

#visit_generic(type) ⇒ Object



846
847
848
849
850
851
852
853
854
# File 'lib/rbi/rbs_printer.rb', line 846

def visit_generic(type)
  @string << translate_t_type(type.name)
  @string << "["
  type.params.each_with_index do |arg, index|
    visit(arg)
    @string << ", " if index < type.params.size - 1
  end
  @string << "]"
end

#visit_nilable(type) ⇒ Object



887
888
889
890
# File 'lib/rbi/rbs_printer.rb', line 887

def visit_nilable(type)
  visit(type.type)
  @string << "?"
end

#visit_no_return(type) ⇒ Object



867
868
869
# File 'lib/rbi/rbs_printer.rb', line 867

def visit_no_return(type)
  @string << "bot"
end

#visit_proc(type) ⇒ Object



941
942
943
944
945
946
947
948
949
950
951
952
953
954
# File 'lib/rbi/rbs_printer.rb', line 941

def visit_proc(type)
  @string << "^"
  if type.proc_params.any?
    @string << "("
    type.proc_params.each_with_index do |(key, value), index|
      visit(value)
      @string << " #{key}"
      @string << ", " if index < type.proc_params.size - 1
    end
    @string << ") "
  end
  @string << "-> "
  visit(type.proc_returns)
end

#visit_self_type(type) ⇒ Object



877
878
879
# File 'lib/rbi/rbs_printer.rb', line 877

def visit_self_type(type)
  @string << "self"
end

#visit_shape(type) ⇒ Object



930
931
932
933
934
935
936
937
938
# File 'lib/rbi/rbs_printer.rb', line 930

def visit_shape(type)
  @string << "{"
  type.types.each_with_index do |(key, value), index|
    @string << "#{key}: "
    visit(value)
    @string << ", " if index < type.types.size - 1
  end
  @string << "}"
end

#visit_simple(type) ⇒ Object



836
837
838
# File 'lib/rbi/rbs_printer.rb', line 836

def visit_simple(type)
  @string << translate_t_type(type.name)
end

#visit_tuple(type) ⇒ Object



920
921
922
923
924
925
926
927
# File 'lib/rbi/rbs_printer.rb', line 920

def visit_tuple(type)
  @string << "["
  type.types.each_with_index do |arg, index|
    visit(arg)
    @string << ", " if index < type.types.size - 1
  end
  @string << "]"
end

#visit_type_parameter(type) ⇒ Object



957
958
959
# File 'lib/rbi/rbs_printer.rb', line 957

def visit_type_parameter(type)
  @string << "TYPE_#{type.name}"
end

#visit_untyped(type) ⇒ Object



872
873
874
# File 'lib/rbi/rbs_printer.rb', line 872

def visit_untyped(type)
  @string << "untyped"
end

#visit_void(type) ⇒ Object



862
863
864
# File 'lib/rbi/rbs_printer.rb', line 862

def visit_void(type)
  @string << "void"
end