forked from microsoft/TypeScript-DOM-lib-generator
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhelpers.ts
338 lines (314 loc) · 7.91 KB
/
helpers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import * as Browser from "./types";
// Extended types used but not defined in the spec
export const bufferSourceTypes = new Set([
"ArrayBuffer",
"ArrayBufferView",
"DataView",
"Int8Array",
"Uint8Array",
"Int16Array",
"Uint16Array",
"Uint8ClampedArray",
"Int32Array",
"Uint32Array",
"Float32Array",
"Float64Array",
]);
export const integerTypes = new Set([
"byte",
"octet",
"short",
"unsigned short",
"long",
"unsigned long",
"long long",
"unsigned long long",
]);
export const stringTypes = new Set([
"ByteString",
"DOMString",
"USVString",
"CSSOMString",
]);
const floatTypes = new Set([
"float",
"unrestricted float",
"double",
"unrestricted double",
]);
const sameTypes = new Set([
"any",
"boolean",
"Date",
"Function",
"Promise",
"undefined",
"void",
]);
export const baseTypeConversionMap = new Map<string, string>([
...[...bufferSourceTypes].map((type) => [type, type] as [string, string]),
...[...integerTypes].map((type) => [type, "number"] as [string, string]),
...[...floatTypes].map((type) => [type, "number"] as [string, string]),
...[...stringTypes].map((type) => [type, "string"] as [string, string]),
...[...sameTypes].map((type) => [type, type] as [string, string]),
["object", "any"],
["sequence", "Array"],
["record", "Record"],
["FrozenArray", "ReadonlyArray"],
["EventHandler", "EventHandler"],
]);
export function filter<T>(
obj: T,
fn: (o: any, n: string | undefined) => boolean
): T {
if (typeof obj === "object") {
if (Array.isArray(obj)) {
return mapDefined(obj, (e) =>
fn(e, undefined) ? filter(e, fn) : undefined
) as any as T;
} else {
const result: any = {};
for (const e in obj) {
if (fn(obj[e], e)) {
result[e] = filter(obj[e], fn);
}
}
return result;
}
}
return obj;
}
export function filterProperties<T, U extends T>(
obj: Record<string, U>,
fn: (o: T) => boolean
): Record<string, U> {
const result: Record<string, U> = {};
for (const e in obj) {
if (fn(obj[e])) {
result[e] = obj[e];
}
}
return result;
}
export function exposesTo(o: { exposed?: string }, target: string): boolean {
if (!o || typeof o.exposed !== "string") {
return true;
}
return o.exposed.includes(target);
}
export function merge<T>(target: T, src: T, shallow?: boolean): T {
if (typeof target !== "object" || typeof src !== "object") {
return src;
}
for (const k in src) {
if (Object.getOwnPropertyDescriptor(src, k)) {
if (Object.getOwnPropertyDescriptor(target, k)) {
const targetProp = target[k];
const srcProp = src[k];
if (Array.isArray(targetProp) && Array.isArray(srcProp)) {
mergeNamedArrays(targetProp, srcProp);
} else {
if (Array.isArray(targetProp) !== Array.isArray(srcProp)) {
throw new Error(
"Mismatch on property: " + k + JSON.stringify(srcProp)
);
}
if (
shallow &&
typeof (targetProp as any).name === "string" &&
typeof (srcProp as any).name === "string"
) {
target[k] = srcProp;
} else {
target[k] = merge(targetProp, srcProp, shallow);
}
}
} else {
target[k] = src[k];
}
}
}
return target;
}
function mergeNamedArrays<T extends { name: string; "new-type": string }>(
srcProp: T[],
targetProp: T[]
) {
const map: any = {};
for (const e1 of srcProp) {
const name = e1.name || e1["new-type"];
if (name) {
map[name] = e1;
}
}
for (const e2 of targetProp) {
const name = e2.name || e2["new-type"];
if (name && map[name]) {
merge(map[name], e2);
} else {
srcProp.push(e2);
}
}
}
export function distinct<T>(a: T[]): T[] {
return Array.from(new Set(a).values());
}
export function mapToArray<T>(m: Record<string, T>): T[] {
return Object.keys(m || {}).map((k) => m[k]);
}
export function arrayToMap<T, U>(
array: ReadonlyArray<T>,
makeKey: (value: T) => string,
makeValue: (value: T) => U
): Record<string, U> {
const result: Record<string, U> = {};
for (const value of array) {
result[makeKey(value)] = makeValue(value);
}
return result;
}
export function map<T, U>(
obj: Record<string, T> | undefined,
fn: (o: T) => U
): U[] {
return Object.keys(obj || {}).map((k) => fn(obj![k]));
}
export function mapDefined<T, U>(
array: ReadonlyArray<T> | undefined,
mapFn: (x: T, i: number) => U | undefined
): U[] {
const result: U[] = [];
if (array) {
for (let i = 0; i < array.length; i++) {
const mapped = mapFn(array[i], i);
if (mapped !== undefined) {
result.push(mapped);
}
}
}
return result;
}
export function toNameMap<T extends { name: string }>(
array: T[]
): Record<string, T> {
const result: Record<string, T> = {};
for (const value of array) {
result[value.name] = value;
}
return result;
}
export function concat<T>(a: T[] | undefined, b: T[] | undefined): T[] {
return !a ? b || [] : a.concat(b || []);
}
export function getEmptyWebIDL(): Browser.WebIdl {
return {
"callback-functions": {
"callback-function": {},
},
"callback-interfaces": {
interface: {},
},
dictionaries: {
dictionary: {},
},
enums: {
enum: {},
},
interfaces: {
interface: {},
},
mixins: {
mixin: {},
},
typedefs: {
typedef: [],
},
namespaces: [],
};
}
export function resolveExposure(
obj: Record<string, any>,
exposure: string,
override?: boolean
): void {
if (!exposure) {
throw new Error("No exposure set");
}
if ("exposed" in obj && (override || obj.exposed === undefined)) {
obj.exposed = exposure;
}
for (const key in obj) {
if (typeof obj[key] === "object" && obj[key]) {
resolveExposure(obj[key], exposure, override);
}
}
}
function collectTypeReferences(obj: any): string[] {
const collection: string[] = [];
if (typeof obj !== "object") {
return collection;
}
if (Array.isArray(obj)) {
return collection.concat(...obj.map(collectTypeReferences));
}
if (typeof obj.type === "string") {
collection.push(obj.type);
}
if (Array.isArray(obj.implements)) {
collection.push(...obj.implements);
}
if (typeof obj.extends === "string") {
collection.push(obj.extends);
}
for (const e in obj) {
collection.push(...collectTypeReferences(obj[e]));
}
return collection;
}
function getNonValueTypeMap(webidl: Browser.WebIdl) {
const namedTypes: { name: string }[] = [
...mapToArray(webidl["callback-functions"]!["callback-function"]),
...mapToArray(webidl["callback-interfaces"]!.interface),
...mapToArray(webidl.dictionaries!.dictionary),
...mapToArray(webidl.enums!.enum),
...mapToArray(webidl.mixins!.mixin),
];
const map = new Map(namedTypes.map((t) => [t.name, t] as [string, any]));
webidl.typedefs!.typedef.map((typedef) =>
map.set(typedef["new-type"], typedef)
);
return map;
}
export function followTypeReferences(
webidl: Browser.WebIdl,
filteredInterfaces: Record<string, Browser.Interface>
): Set<string> {
const set = new Set<string>();
const map = getNonValueTypeMap(webidl);
new Set(collectTypeReferences(filteredInterfaces)).forEach(follow);
return set;
function follow(reference: string) {
if (
baseTypeConversionMap.has(reference) ||
reference in filteredInterfaces
) {
return;
}
const type = map.get(reference);
if (!type) {
return;
}
if (!set.has(type.name || type["new-type"])) {
set.add(type.name || type["new-type"]);
collectTypeReferences(type).forEach(follow);
}
}
}
export function markAsDeprecated(i: Browser.Interface): void {
for (const method of mapToArray(i.methods.method)) {
method.deprecated = 1;
}
for (const property of mapToArray(i.properties!.property)) {
property.deprecated = 1;
}
}