7
7
import 'package:code_builder/code_builder.dart' ;
8
8
9
9
import '../interop_gen/namer.dart' ;
10
+ import '../web_rename_map.dart' ;
10
11
import 'base.dart' ;
11
12
12
13
/// A built in type supported by `dart:js_interop` or by this library
@@ -83,20 +84,90 @@ class BuiltinType extends Type {
83
84
PrimitiveType .any || PrimitiveType .unknown => anyType,
84
85
PrimitiveType .object => BuiltinType (
85
86
name: 'JSObject' , fromDartJSInterop: true , isNullable: isNullable),
87
+ PrimitiveType .symbol => BuiltinType (
88
+ name: 'JSSymbol' , fromDartJSInterop: true , isNullable: isNullable),
89
+ PrimitiveType .bigint => BuiltinType (
90
+ name: 'JSBigInt' , fromDartJSInterop: true , isNullable: isNullable),
86
91
PrimitiveType .array => BuiltinType (
87
92
name: 'JSArray' ,
88
93
typeParams: [typeParams.single],
89
94
fromDartJSInterop: true ,
90
95
isNullable: isNullable),
91
- PrimitiveType .promise => BuiltinType (
92
- name: 'JSPromise' ,
93
- typeParams: [typeParams.single],
94
- fromDartJSInterop: true ,
95
- isNullable: isNullable),
96
- PrimitiveType .function => BuiltinType (
97
- name: 'JSFunction' , fromDartJSInterop: true , isNullable: isNullable),
98
96
};
99
97
}
98
+
99
+ static BuiltinType ? referred (String name,
100
+ {bool ? isNullable, List <Type > typeParams = const []}) {
101
+ final jsName = switch (name) {
102
+ 'Array' => 'JSArray' ,
103
+ 'Promise' => 'JSPromise' ,
104
+ 'ArrayBuffer' => 'JSArrayBuffer' ,
105
+ 'Function' => 'JSFunction' ,
106
+ 'DataView' => 'JSDataView' ,
107
+ 'Float32Array' => 'JSFloat32Array' ,
108
+ 'Float64Array' => 'JSFloat64Array' ,
109
+ 'Int8Array' => 'JSInt8Array' ,
110
+ 'Int16Array' => 'JSInt16Array' ,
111
+ 'Int32Array' => 'JSInt32Array' ,
112
+ 'Int64Array' => 'JSInt64Array' ,
113
+ 'Uint8Array' => 'JSUint8Array' ,
114
+ 'Uint16Array' => 'JSUint16Array' ,
115
+ 'Uint32Array' => 'JSUint32Array' ,
116
+ 'Uint8ClampedArray' => 'JSUint8ClampedArray' ,
117
+ _ => null
118
+ };
119
+ final jsTypeArgs = switch (name) { 'Array' || 'Promise' => 1 , _ => 0 };
120
+ if (jsName case final typeName? ) {
121
+ return BuiltinType (
122
+ name: typeName,
123
+ fromDartJSInterop: true ,
124
+ typeParams: typeParams.take (jsTypeArgs).toList (),
125
+ isNullable: isNullable);
126
+ }
127
+ return null ;
128
+ }
129
+ }
130
+
131
+ class PackageWebType extends Type {
132
+ @override
133
+ final String name;
134
+
135
+ final List <Type > typeParams;
136
+
137
+ final bool ? isNullable;
138
+
139
+ @override
140
+ ID get id => ID (type: 'type' , name: name);
141
+
142
+ @override
143
+ String ? get dartName => null ;
144
+
145
+ PackageWebType ._(
146
+ {required this .name, this .typeParams = const [], this .isNullable});
147
+
148
+ @override
149
+ Reference emit ([TypeOptions ? options]) {
150
+ options ?? = TypeOptions ();
151
+
152
+ // TODO: We can make this a shared function as it is called a lot
153
+ // between types
154
+ return TypeReference ((t) => t
155
+ ..symbol = name
156
+ ..types.addAll (typeParams
157
+ // if there is only one type param, and it is void, ignore
158
+ .where ((p) => typeParams.length != 1 || p != BuiltinType .$voidType)
159
+ .map ((p) => p.emit (TypeOptions ())))
160
+ ..url = 'package:web/web.dart'
161
+ ..isNullable = isNullable ?? options! .nullable);
162
+ }
163
+
164
+ static PackageWebType parse (String name,
165
+ {bool ? isNullable, List <Type > typeParams = const []}) {
166
+ return PackageWebType ._(
167
+ name: renameMap.containsKey (name) ? renameMap[name]! : name,
168
+ isNullable: isNullable,
169
+ typeParams: typeParams);
170
+ }
100
171
}
101
172
102
173
enum PrimitiveType {
@@ -110,7 +181,7 @@ enum PrimitiveType {
110
181
object,
111
182
unknown,
112
183
undefined,
184
+ symbol,
113
185
array,
114
- promise,
115
- function
186
+ bigint
116
187
}
0 commit comments