Class: RBI::TypePrinter
- Inherits:
-
Object
- Object
- RBI::TypePrinter
- Defined in:
- lib/rbi/rbs_printer.rb
Instance Attribute Summary collapse
- #string ⇒ Object readonly
Instance Method Summary collapse
-
#initialize(max_line_length: nil) ⇒ TypePrinter
constructor
A new instance of TypePrinter.
- #visit(node) ⇒ Object
- #visit_all(type) ⇒ Object
- #visit_any(type) ⇒ Object
- #visit_anything(type) ⇒ Object
- #visit_attached_class(type) ⇒ Object
- #visit_boolean(type) ⇒ Object
- #visit_class(type) ⇒ Object
- #visit_class_of(type) ⇒ Object
- #visit_generic(type) ⇒ Object
- #visit_module(type) ⇒ Object
- #visit_nilable(type) ⇒ Object
- #visit_no_return(type) ⇒ Object
- #visit_proc(type) ⇒ Object
- #visit_self_type(type) ⇒ Object
- #visit_shape(type) ⇒ Object
- #visit_simple(type) ⇒ Object
- #visit_tuple(type) ⇒ Object
- #visit_type_parameter(type) ⇒ Object
- #visit_untyped(type) ⇒ Object
- #visit_void(type) ⇒ Object
Constructor Details
#initialize(max_line_length: nil) ⇒ TypePrinter
Returns a new instance of TypePrinter.
999 1000 1001 1002 |
# File 'lib/rbi/rbs_printer.rb', line 999 def initialize(max_line_length: nil) @string = String.new #: String @max_line_length = max_line_length end |
Instance Attribute Details
#string ⇒ Object (readonly)
996 997 998 |
# File 'lib/rbi/rbs_printer.rb', line 996 def string @string end |
Instance Method Details
#visit(node) ⇒ Object
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 |
# File 'lib/rbi/rbs_printer.rb', line 1005 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) when Type::Module visit_module(node) else raise Error, "Unhandled node: #{node.class}" end end |
#visit_all(type) ⇒ Object
1127 1128 1129 1130 1131 1132 1133 1134 |
# File 'lib/rbi/rbs_printer.rb', line 1127 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
1137 1138 1139 1140 1141 1142 1143 1144 |
# File 'lib/rbi/rbs_printer.rb', line 1137 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
1072 1073 1074 |
# File 'lib/rbi/rbs_printer.rb', line 1072 def visit_anything(type) @string << "top" end |
#visit_attached_class(type) ⇒ Object
1097 1098 1099 |
# File 'lib/rbi/rbs_printer.rb', line 1097 def visit_attached_class(type) @string << "instance" end |
#visit_boolean(type) ⇒ Object
1056 1057 1058 |
# File 'lib/rbi/rbs_printer.rb', line 1056 def visit_boolean(type) @string << "bool" end |
#visit_class(type) ⇒ Object
1204 1205 1206 1207 1208 |
# File 'lib/rbi/rbs_printer.rb', line 1204 def visit_class(type) @string << "Class[" visit(type.type) @string << "]" end |
#visit_class_of(type) ⇒ Object
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 |
# File 'lib/rbi/rbs_printer.rb', line 1115 def visit_class_of(type) @string << "singleton(" visit(type.type) @string << ")" if (type_parameter = type.type_parameter) @string << "[" visit(type_parameter) @string << "]" end end |
#visit_generic(type) ⇒ Object
1061 1062 1063 1064 1065 1066 1067 1068 1069 |
# File 'lib/rbi/rbs_printer.rb', line 1061 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_module(type) ⇒ Object
1211 1212 1213 1214 1215 |
# File 'lib/rbi/rbs_printer.rb', line 1211 def visit_module(type) @string << "Module[" visit(type.type) @string << "]" end |
#visit_nilable(type) ⇒ Object
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 |
# File 'lib/rbi/rbs_printer.rb', line 1102 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
1082 1083 1084 |
# File 'lib/rbi/rbs_printer.rb', line 1082 def visit_no_return(type) @string << "bot" end |
#visit_proc(type) ⇒ Object
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 |
# File 'lib/rbi/rbs_printer.rb', line 1177 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
1092 1093 1094 |
# File 'lib/rbi/rbs_printer.rb', line 1092 def visit_self_type(type) @string << "self" end |
#visit_shape(type) ⇒ Object
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 |
# File 'lib/rbi/rbs_printer.rb', line 1157 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
1051 1052 1053 |
# File 'lib/rbi/rbs_printer.rb', line 1051 def visit_simple(type) @string << translate_t_type(type.name.gsub(/\s/, "")) end |
#visit_tuple(type) ⇒ Object
1147 1148 1149 1150 1151 1152 1153 1154 |
# File 'lib/rbi/rbs_printer.rb', line 1147 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
1199 1200 1201 |
# File 'lib/rbi/rbs_printer.rb', line 1199 def visit_type_parameter(type) @string << type.name.to_s end |
#visit_untyped(type) ⇒ Object
1087 1088 1089 |
# File 'lib/rbi/rbs_printer.rb', line 1087 def visit_untyped(type) @string << "untyped" end |
#visit_void(type) ⇒ Object
1077 1078 1079 |
# File 'lib/rbi/rbs_printer.rb', line 1077 def visit_void(type) @string << "void" end |