Class: RBI::TypePrinter
- Inherits:
-
Object
- Object
- RBI::TypePrinter
- Defined in:
- lib/rbi/rbs_printer.rb
Instance Attribute Summary collapse
-
#string ⇒ Object
readonly
: String.
Instance Method Summary collapse
-
#initialize ⇒ TypePrinter
constructor
: -> void.
-
#visit(node) ⇒ Object
: (Type node) -> void.
-
#visit_all(type) ⇒ Object
: (Type::All type) -> void.
-
#visit_any(type) ⇒ Object
: (Type::Any type) -> void.
-
#visit_anything(type) ⇒ Object
: (Type::Anything type) -> void.
-
#visit_attached_class(type) ⇒ Object
: (Type::AttachedClass type) -> void.
-
#visit_boolean(type) ⇒ Object
: (Type::Boolean type) -> void.
-
#visit_class(type) ⇒ Object
: (Type::Class type) -> void.
-
#visit_class_of(type) ⇒ Object
: (Type::ClassOf type) -> void.
-
#visit_generic(type) ⇒ Object
: (Type::Generic type) -> void.
-
#visit_nilable(type) ⇒ Object
: (Type::Nilable type) -> void.
-
#visit_no_return(type) ⇒ Object
: (Type::NoReturn type) -> void.
-
#visit_proc(type) ⇒ Object
: (Type::Proc type) -> void.
-
#visit_self_type(type) ⇒ Object
: (Type::SelfType type) -> void.
-
#visit_shape(type) ⇒ Object
: (Type::Shape type) -> void.
-
#visit_simple(type) ⇒ Object
: (Type::Simple type) -> void.
-
#visit_tuple(type) ⇒ Object
: (Type::Tuple type) -> void.
-
#visit_type_parameter(type) ⇒ Object
: (Type::TypeParameter type) -> void.
-
#visit_untyped(type) ⇒ Object
: (Type::Untyped type) -> void.
-
#visit_void(type) ⇒ Object
: (Type::Void type) -> void.
Constructor Details
#initialize ⇒ TypePrinter
: -> void
884 885 886 |
# File 'lib/rbi/rbs_printer.rb', line 884 def initialize @string = T.let(String.new, String) end |
Instance Attribute Details
#string ⇒ Object (readonly)
: String
881 882 883 |
# File 'lib/rbi/rbs_printer.rb', line 881 def string @string end |
Instance Method Details
#visit(node) ⇒ Object
: (Type node) -> void
889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 |
# File 'lib/rbi/rbs_printer.rb', line 889 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
: (Type::All type) -> void
1004 1005 1006 1007 1008 1009 1010 1011 |
# File 'lib/rbi/rbs_printer.rb', line 1004 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
: (Type::Any type) -> void
1014 1015 1016 1017 1018 1019 1020 1021 |
# File 'lib/rbi/rbs_printer.rb', line 1014 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
: (Type::Anything type) -> void
954 955 956 |
# File 'lib/rbi/rbs_printer.rb', line 954 def visit_anything(type) @string << "top" end |
#visit_attached_class(type) ⇒ Object
: (Type::AttachedClass type) -> void
979 980 981 |
# File 'lib/rbi/rbs_printer.rb', line 979 def visit_attached_class(type) @string << "instance" end |
#visit_boolean(type) ⇒ Object
: (Type::Boolean type) -> void
938 939 940 |
# File 'lib/rbi/rbs_printer.rb', line 938 def visit_boolean(type) @string << "bool" end |
#visit_class(type) ⇒ Object
: (Type::Class type) -> void
1081 1082 1083 1084 1085 |
# File 'lib/rbi/rbs_printer.rb', line 1081 def visit_class(type) @string << "Class[" visit(type.type) @string << "]" end |
#visit_class_of(type) ⇒ Object
: (Type::ClassOf type) -> void
997 998 999 1000 1001 |
# File 'lib/rbi/rbs_printer.rb', line 997 def visit_class_of(type) @string << "singleton(" visit(type.type) @string << ")" end |
#visit_generic(type) ⇒ Object
: (Type::Generic type) -> void
943 944 945 946 947 948 949 950 951 |
# File 'lib/rbi/rbs_printer.rb', line 943 def visit_generic(type) @string << translate_t_type(type.name.gsub(/\s/, "")) @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
: (Type::Nilable type) -> void
984 985 986 987 988 989 990 991 992 993 994 |
# File 'lib/rbi/rbs_printer.rb', line 984 def visit_nilable(type) inner = type.type if inner.is_a?(Type::Proc) @string << "(" end visit(inner) if inner.is_a?(Type::Proc) @string << ")" end @string << "?" end |
#visit_no_return(type) ⇒ Object
: (Type::NoReturn type) -> void
964 965 966 |
# File 'lib/rbi/rbs_printer.rb', line 964 def visit_no_return(type) @string << "bot" end |
#visit_proc(type) ⇒ Object
: (Type::Proc type) -> void
1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 |
# File 'lib/rbi/rbs_printer.rb', line 1054 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 proc_bind = type.proc_bind if proc_bind @string << "[self: " visit(proc_bind) @string << "] " end @string << "-> " visit(type.proc_returns) end |
#visit_self_type(type) ⇒ Object
: (Type::SelfType type) -> void
974 975 976 |
# File 'lib/rbi/rbs_printer.rb', line 974 def visit_self_type(type) @string << "self" end |
#visit_shape(type) ⇒ Object
: (Type::Shape type) -> void
1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 |
# File 'lib/rbi/rbs_printer.rb', line 1034 def visit_shape(type) @string << "{" type.types.each_with_index do |(key, value), index| @string << case key when String "\"#{key}\" => " when Symbol if key.match?(/\A[a-zA-Z_]+[a-zA-Z0-9_]*\z/) "#{key}: " else "\"#{key}\": " end end visit(value) @string << ", " if index < type.types.size - 1 end @string << "}" end |
#visit_simple(type) ⇒ Object
: (Type::Simple type) -> void
933 934 935 |
# File 'lib/rbi/rbs_printer.rb', line 933 def visit_simple(type) @string << translate_t_type(type.name.gsub(/\s/, "")) end |
#visit_tuple(type) ⇒ Object
: (Type::Tuple type) -> void
1024 1025 1026 1027 1028 1029 1030 1031 |
# File 'lib/rbi/rbs_printer.rb', line 1024 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
: (Type::TypeParameter type) -> void
1076 1077 1078 |
# File 'lib/rbi/rbs_printer.rb', line 1076 def visit_type_parameter(type) @string << type.name.to_s end |
#visit_untyped(type) ⇒ Object
: (Type::Untyped type) -> void
969 970 971 |
# File 'lib/rbi/rbs_printer.rb', line 969 def visit_untyped(type) @string << "untyped" end |
#visit_void(type) ⇒ Object
: (Type::Void type) -> void
959 960 961 |
# File 'lib/rbi/rbs_printer.rb', line 959 def visit_void(type) @string << "void" end |