8
8
import src .utils as ut
9
9
from src .ir .decorators import two_way_subtyping
10
10
11
+
11
12
class TypeScriptBuiltinFactory (bt .BuiltinFactory ):
13
+ def __init__ (self , max_union_types = 10 , max_types_in_union = 4 ,
14
+ max_string_literal_types = 10 , max_num_literal_types = 10 ):
15
+ self ._literal_type_factory = LiteralTypeFactory (
16
+ max_string_literal_types , max_num_literal_types )
17
+ self ._union_type_factory = UnionTypeFactory (max_union_types ,
18
+ max_types_in_union )
19
+
12
20
def get_language (self ):
13
21
return "typescript"
14
22
@@ -80,16 +88,17 @@ def get_big_decimal_type(self):
80
88
def get_null_type (self ):
81
89
return NullType (primitive = False )
82
90
83
- def get_non_nothing_types (self ): # Overwriting Parent method to add TS-specific types
91
+ def get_non_nothing_types (self ):
92
+ # Overwriting Parent method to add TS-specific types
84
93
types = super ().get_non_nothing_types ()
85
94
types .extend ([
86
95
self .get_null_type (),
87
96
UndefinedType (primitive = False ),
88
- ] + literal_types .get_literal_types ())
97
+ ] + self . _literal_type_factory .get_literal_types ())
89
98
return types
90
99
91
100
def get_decl_candidates (self ):
92
- return [gen_type_alias_decl ,]
101
+ return [gen_type_alias_decl , ]
93
102
94
103
def update_add_node_to_parent (self ):
95
104
return {
@@ -98,7 +107,7 @@ def update_add_node_to_parent(self):
98
107
99
108
def get_compound_types (self , gen_object ):
100
109
return [
101
- union_types .get_union_type (gen_object ),
110
+ self . _union_type_factory .get_union_type (gen_object ),
102
111
]
103
112
104
113
def get_constant_candidates (self , constants ):
@@ -121,9 +130,12 @@ def get_constant_candidates(self, constants):
121
130
122
131
"""
123
132
return {
124
- "NumberLiteralType" : lambda etype : ast .IntegerConstant (etype .literal , NumberLiteralType ),
125
- "StringLiteralType" : lambda etype : ast .StringConstant (etype .literal ),
126
- "UnionType" : lambda etype : union_types .get_union_constant (etype , constants ),
133
+ "NumberLiteralType" : lambda etype : ast .IntegerConstant (
134
+ etype .literal , NumberLiteralType ),
135
+ "StringLiteralType" : lambda etype : ast .StringConstant (
136
+ etype .literal ),
137
+ "UnionType" : lambda etype : self ._union_type_factory .get_union_constant (
138
+ etype , constants ),
127
139
}
128
140
129
141
@@ -755,12 +767,19 @@ def get_union_type(self, gen_object):
755
767
the already generated types or create a new one.
756
768
757
769
"""
758
- return self .gen_union_type (gen_object )
759
770
generated = len (self .unions )
760
771
if generated == 0 :
761
772
return self .gen_union_type (gen_object )
762
773
if generated >= self .max_ut or ut .random .bool ():
763
- return ut .random .choice (self .unions )
774
+ union_t = ut .random .choice (self .unions )
775
+ if union_t .has_type_variables ():
776
+ # We might have selected a union type that holds a type
777
+ # variable. However, we must be careful because it might be
778
+ # no possible to use the selected union type since it uses
779
+ # a type variable that is out of context.
780
+ return self .gen_union_type (gen_object )
781
+ else :
782
+ return union_t
764
783
return self .gen_union_type (gen_object )
765
784
766
785
def get_union_constant (self , utype , constants ):
@@ -830,8 +849,7 @@ def gen_type_alias_decl(gen,
830
849
831
850
"""
832
851
alias_type = (etype if etype else
833
- gen .select_type ()
834
- )
852
+ gen .select_type ())
835
853
initial_depth = gen .depth
836
854
gen .depth += 1
837
855
gen .depth = initial_depth
@@ -846,18 +864,3 @@ def gen_type_alias_decl(gen,
846
864
def add_type_alias (gen , namespace , type_name , ta_decl ):
847
865
gen .context ._add_entity (namespace , 'types' , type_name , ta_decl .get_type ())
848
866
gen .context ._add_entity (namespace , 'decls' , type_name , ta_decl )
849
-
850
-
851
- # Literal Types
852
-
853
- # TODO make these limits user-configurable
854
- MAX_STRING_LITERAL_TYPES = 10
855
- MAX_NUM_LITERAL_TYPES = 10
856
- literal_types = LiteralTypeFactory (MAX_STRING_LITERAL_TYPES , MAX_NUM_LITERAL_TYPES )
857
-
858
- # Union Types
859
-
860
- # TODO make these limits user-configurable
861
- MAX_UNION_TYPES = 10
862
- MAX_TYPES_IN_UNION = 4
863
- union_types = UnionTypeFactory (MAX_UNION_TYPES , MAX_TYPES_IN_UNION )
0 commit comments