Class: RBI::TypePrinter
- Inherits:
-
Object
- Object
- RBI::TypePrinter
- Extended by:
- T::Sig
- Defined in:
- lib/rbi/rbs_printer.rb
Instance Attribute Summary collapse
-
#string ⇒ Object
readonly
Returns the value of attribute string.
Instance Method Summary collapse
-
#initialize ⇒ 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_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 ⇒ TypePrinter
Returns a new instance of TypePrinter.
816 817 818 |
# File 'lib/rbi/rbs_printer.rb', line 816 def initialize @string = T.let(String.new, String) end |
Instance Attribute Details
#string ⇒ Object (readonly)
Returns the value of attribute string.
813 814 815 |
# File 'lib/rbi/rbs_printer.rb', line 813 def string @string end |
Instance Method Details
#visit(node) ⇒ Object
821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 |
# File 'lib/rbi/rbs_printer.rb', line 821 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
936 937 938 939 940 941 942 943 |
# File 'lib/rbi/rbs_printer.rb', line 936 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
946 947 948 949 950 951 952 953 |
# File 'lib/rbi/rbs_printer.rb', line 946 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
886 887 888 |
# File 'lib/rbi/rbs_printer.rb', line 886 def visit_anything(type) @string << "top" end |
#visit_attached_class(type) ⇒ Object
911 912 913 |
# File 'lib/rbi/rbs_printer.rb', line 911 def visit_attached_class(type) @string << "instance" end |
#visit_boolean(type) ⇒ Object
870 871 872 |
# File 'lib/rbi/rbs_printer.rb', line 870 def visit_boolean(type) @string << "bool" end |
#visit_class(type) ⇒ Object
1013 1014 1015 1016 1017 |
# File 'lib/rbi/rbs_printer.rb', line 1013 def visit_class(type) @string << "Class[" visit(type.type) @string << "]" end |
#visit_class_of(type) ⇒ Object
929 930 931 932 933 |
# File 'lib/rbi/rbs_printer.rb', line 929 def visit_class_of(type) @string << "singleton(" visit(type.type) @string << ")" end |
#visit_generic(type) ⇒ Object
875 876 877 878 879 880 881 882 883 |
# File 'lib/rbi/rbs_printer.rb', line 875 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
916 917 918 919 920 921 922 923 924 925 926 |
# File 'lib/rbi/rbs_printer.rb', line 916 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
896 897 898 |
# File 'lib/rbi/rbs_printer.rb', line 896 def visit_no_return(type) @string << "bot" end |
#visit_proc(type) ⇒ Object
986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 |
# File 'lib/rbi/rbs_printer.rb', line 986 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
906 907 908 |
# File 'lib/rbi/rbs_printer.rb', line 906 def visit_self_type(type) @string << "self" end |
#visit_shape(type) ⇒ Object
966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 |
# File 'lib/rbi/rbs_printer.rb', line 966 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
865 866 867 |
# File 'lib/rbi/rbs_printer.rb', line 865 def visit_simple(type) @string << translate_t_type(type.name.gsub(/\s/, "")) end |
#visit_tuple(type) ⇒ Object
956 957 958 959 960 961 962 963 |
# File 'lib/rbi/rbs_printer.rb', line 956 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
1008 1009 1010 |
# File 'lib/rbi/rbs_printer.rb', line 1008 def visit_type_parameter(type) @string << type.name.to_s end |
#visit_untyped(type) ⇒ Object
901 902 903 |
# File 'lib/rbi/rbs_printer.rb', line 901 def visit_untyped(type) @string << "untyped" end |
#visit_void(type) ⇒ Object
891 892 893 |
# File 'lib/rbi/rbs_printer.rb', line 891 def visit_void(type) @string << "void" end |