-
Notifications
You must be signed in to change notification settings - Fork 469
Expand file tree
/
Copy pathpatches.ts
More file actions
565 lines (519 loc) · 16 KB
/
patches.ts
File metadata and controls
565 lines (519 loc) · 16 KB
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
//! This file:
//! - Locates all KDL files in the `patches/` directory.
//! - Parses and type-checks them.
//! - Merges their contents and applies structural transformations over the main IDL dataset.
import { parse, type Value, type Node, type Document } from "kdljs";
import type {
Enum,
Event,
Property,
Interface,
WebIdl,
Method,
Typed,
Param,
Dictionary,
Member,
Signature,
TypeDef,
} from "./types.ts";
import { readdir, readFile } from "fs/promises";
import { merge } from "./helpers.ts";
type DeepPartial<T> = T extends object
? { [K in keyof T]?: DeepPartial<T[K]> }
: T;
interface OverridableMethod extends Omit<Method, "signature"> {
signature: DeepPartial<Signature>[] | Record<number, DeepPartial<Signature>>;
}
function optionalMember<const T>(prop: string, type: T, value?: Value) {
if (value === undefined) {
return {};
}
if (typeof value !== type) {
throw new Error(`Expected type ${value} for ${prop}`);
}
return {
[prop]: value as T extends "string"
? string
: T extends "number"
? number
: T extends "boolean"
? boolean
: never,
};
}
function string(arg: unknown): string {
if (typeof arg !== "string") {
throw new Error(`Expected a string but found ${typeof arg}`);
}
return arg;
}
function handleSingleTypeNode(type: Node): DeepPartial<Typed> {
const isTyped = type.name == "type";
if (!isTyped) {
throw new Error("Expected a type node");
}
const subType =
type.children.length > 0 ? handleTyped(type.children) : undefined;
return {
...optionalMember("type", "string", type.values[0]),
subtype: subType,
...optionalMember("nullable", "boolean", type.properties?.nullable),
};
}
function handleTyped(
typeNodes: Node[],
property?: Value,
): DeepPartial<Typed> | undefined {
if (property) {
if (typeNodes.length) {
throw new Error("Type nodes can't coexist with type property");
}
return {
type: string(property),
subtype: undefined,
};
}
const types = typeNodes.map(handleSingleTypeNode);
if (typeNodes.length > 1) {
// union types
return { type: types };
}
// either a non-union type or no type
return types[0];
}
function handleSingleTypeParameterNode(node: Node) {
return {
name: string(node.values[0]),
...optionalMember("default", "string", node.properties?.default),
...optionalMember("extends", "string", node.properties?.extends),
};
}
function handleTypeParameter(node: Node[], property?: Value) {
let typeParameters: any;
if (typeof property === "string") {
typeParameters = [{ name: property }];
} else {
typeParameters = node.map(handleSingleTypeParameterNode);
}
if (!typeParameters || typeParameters.length === 0) {
return {};
}
return { typeParameters };
}
function optionalNestedMember<T>(prop: string, object: object, output: T) {
return Object.entries(object).length ? { [prop]: output } : {};
}
/**
* Converts parsed KDL Document nodes to match the [types](types.d.ts).
*/
function convertKDLNodes(nodes: Node[]): DeepPartial<WebIdl> {
const enums: Record<string, Partial<Enum>> = {};
const mixin: Record<string, DeepPartial<Interface>> = {};
const interfaces: Record<string, DeepPartial<Interface>> = {};
const dictionary: Record<string, DeepPartial<Dictionary>> = {};
const typedefs: DeepPartial<TypeDef>[] = [];
for (const node of nodes) {
// Note: no "removals" handling here; caller is responsible for splitting
const name = string(node.values[0]);
switch (node.name) {
case "enum":
enums[name] = handleEnum(node);
break;
case "interface-mixin":
mixin[name] = merge(
mixin[name],
handleMixinAndInterfaces(node, "mixin"),
);
break;
case "interface":
interfaces[name] = merge(
interfaces[name],
handleMixinAndInterfaces(node, "interface"),
);
break;
case "dictionary":
dictionary[name] = merge(dictionary[name], handleDictionary(node));
break;
case "typedef":
typedefs.push(handleTypedef(node));
break;
default:
throw new Error(`Unknown node name: ${node.name}`);
}
}
return {
...optionalNestedMember("enums", enums, { enum: enums }),
...optionalNestedMember("mixins", mixin, { mixin }),
...optionalNestedMember("interfaces", interfaces, {
interface: interfaces,
}),
...optionalNestedMember("dictionaries", dictionary, { dictionary }),
...optionalNestedMember("typedefs", typedefs, { typedef: typedefs }),
};
}
/**
* Handles an enum node by extracting its name and values.
* Throws an error if the enum name is missing or if the values are not in the correct format.
* @param node The enum node to handle.
* @param enums The record of enums to update.
*/
function handleEnum(node: Node): Partial<Enum> {
const name = string(node.properties?.name || node.values[0]);
const values: string[] = [];
for (const child of node.children) {
values.push(child.name);
}
return {
name,
...optionalNestedMember("value", values, values),
...optionalMember(
"legacyNamespace",
"string",
node.properties.legacyNamespace,
),
};
}
/**
* Handles a mixin node by extracting its name and associated members.
* Throws an error if the mixin name is missing.
* Adds them to the mixins record under the mixin's name.
* @param node The mixin node to handle.
* @param mixins The record of mixins to update.
*/
function handleMixinAndInterfaces(
node: Node,
type: "mixin" | "interface",
): DeepPartial<Interface> {
const name = string(node.properties?.name || node.values[0]);
const event: Event[] = [];
const property: Record<string, DeepPartial<Property>> = {};
let method: Record<string, DeepPartial<OverridableMethod>> = {};
let constructor: DeepPartial<OverridableMethod> | undefined;
const typeParameter = [];
for (const child of node.children) {
switch (child.name) {
case "event":
event.push(handleEvent(child));
break;
case "property": {
const propName = string(child.values[0]);
property[propName] = handleProperty(child);
break;
}
case "method": {
const methodName = string(child.values[0]);
const m = handleMethodAndConstructor(child);
method = merge(method, {
[methodName]: m,
});
break;
}
case "constructor": {
const c = handleMethodAndConstructor(child, true);
constructor = merge(constructor, c);
break;
}
case "typeParameter": {
typeParameter.push(child);
break;
}
default:
throw new Error(`Unknown node name: ${child.name}`);
}
}
const interfaceObject = type === "interface" && {
...(constructor ? { constructor } : {}),
...optionalMember("exposed", "string", node.properties?.exposed),
...optionalMember("deprecated", "string", node.properties?.deprecated),
...optionalMember(
"noInterfaceObject",
"boolean",
node.properties?.noInterfaceObject,
),
};
return {
name,
...optionalNestedMember("events", event, { event }),
properties: { property },
methods: { method },
...optionalMember("extends", "string", node.properties?.extends),
...optionalMember("overrideThis", "string", node.properties?.overrideThis),
...optionalMember("forward", "string", node.properties?.forward),
...optionalMember(
"forwardExtends",
"string",
node.properties?.forwardExtends,
),
...optionalMember(
"replaceReference",
"string",
node.properties?.replaceReference,
),
...handleTypeParameter(typeParameter, node.properties?.typeParameter),
...interfaceObject,
} as DeepPartial<Interface>;
}
/**
* Handles a child node of type "event" and adds it to the event array.
* @param child The child node to handle.
*/
function handleEvent(child: Node): Event {
return {
name: string(child.values[0]),
type: string(child.properties.type),
};
}
/**
* Handles a child node of type "property" and adds it to the property object.
* @param child The child node to handle.
*/
function handleProperty(child: Node): DeepPartial<Property> {
const typeNodes = child.children.filter((c) => c.name === "type");
return {
name: string(child.values[0]),
...optionalMember("exposed", "string", child.properties?.exposed),
...optionalMember("optional", "boolean", child.properties?.optional),
...optionalMember("overrideType", "string", child.properties?.overrideType),
...handleTyped(typeNodes, child.properties?.type),
...optionalMember("readonly", "boolean", child.properties?.readonly),
...optionalMember("deprecated", "string", child.properties?.deprecated),
...optionalMember("mdnUrl", "string", child.properties?.mdnUrl),
};
}
function handleParam(node: Node) {
const name = string(node.values[0]);
let additionalTypes: string[] | undefined;
const typeNodes: Node[] = [];
for (const child of node.children) {
switch (child.name) {
case "additionalTypes": {
if (additionalTypes) {
throw new Error("Unexpected multiple additionalTypes node");
}
additionalTypes = child.values.map(string);
break;
}
case "type": {
typeNodes.push(child);
break;
}
default:
throw new Error(`Unexpected child "${child.name}" in param "${name}"`);
}
}
return {
name,
...handleTyped(typeNodes, node.properties?.type),
...optionalMember("overrideType", "string", node.properties?.overrideType),
additionalTypes,
};
}
/**
* Handles a child node of type "method" or "constructor" and adds it to the method or constructor object.
* @param child The child node to handle.
* @param isConstructor Whether the child node is a constructor.
*/
function handleMethodAndConstructor(
child: Node,
isConstructor: boolean = false,
): DeepPartial<OverridableMethod> {
const name = isConstructor ? undefined : string(child.values[0]);
// Collect all type nodes into an array
const typeNodes: Node[] = [];
const params: DeepPartial<Param>[] = [];
for (const c of child.children) {
switch (c.name) {
case "type":
typeNodes.push(c);
break;
case "param":
params.push(handleParam(c));
break;
default:
throw new Error(`Unexpected child "${c.name}" in method "${name}"`);
}
}
const signatureIndex = child.properties?.signatureIndex;
const type = handleTyped(typeNodes, child.properties?.returns);
let signature: OverridableMethod["signature"] = [];
if (type || params.length > 0) {
// Determine the actual signature object
const signatureObj: DeepPartial<Signature> = {
param: params,
...type,
};
if (typeof signatureIndex == "number") {
signature = { [signatureIndex]: signatureObj };
} else {
signature = [signatureObj];
}
}
return {
name,
signature,
...optionalMember("exposed", "string", child.properties.exposed),
};
}
/**
* Handles dictionary nodes
* @param child The dictionary node to handle.
*/
function handleDictionary(child: Node): DeepPartial<Dictionary> {
const name = string(child.values[0]);
const member: Record<string, DeepPartial<Member>> = {};
const typeParameter = [];
for (const c of child.children) {
switch (c.name) {
case "member": {
const memberName = string(c.values[0]);
member[memberName] = handleMember(c);
break;
}
case "typeParameter": {
typeParameter.push(c);
break;
}
default:
throw new Error(`Unknown node name: ${c.name}`);
}
}
return {
name,
members: { member },
...handleTypeParameter(typeParameter, child.properties?.typeParameter),
...optionalMember(
"legacyNamespace",
"string",
child.properties?.legacyNamespace,
),
...optionalMember("overrideType", "string", child.properties?.overrideType),
};
}
/**
* Handles dictionary member nodes
* @param c The member node to handle.
*/
function handleMember(c: Node): DeepPartial<Member> {
const name = string(c.values[0]);
const typeNodes = c.children.filter((c) => c.name === "type");
return {
name,
...handleTyped(typeNodes, c.properties?.type),
...optionalMember("required", "boolean", c.properties?.required),
...optionalMember("deprecated", "string", c.properties?.deprecated),
...optionalMember("overrideType", "string", c.properties?.overrideType),
};
}
/**
* Handles typedef nodes
* @param node The typedef node to handle.
*/
function handleTypedef(node: Node): DeepPartial<TypeDef> {
const typeNodes: Node[] = [];
const typeParameter = [];
for (const child of node.children) {
switch (child.name) {
case "type":
typeNodes.push(child);
break;
case "typeParameter": {
typeParameter.push(child);
break;
}
default:
throw new Error(
`Unexpected child "${child.name}" in typedef "${node.values[0]}"`,
);
}
}
return {
name: string(node.values[0]),
...handleTyped(typeNodes),
...optionalMember(
"legacyNamespace",
"string",
node.properties?.legacyNamespace,
),
...optionalMember("overrideType", "string", node.properties?.overrideType),
...handleTypeParameter(typeParameter, node.properties?.typeParameter),
};
}
/**
* Collect all file URLs in a directory.
*/
async function getAllFileURLs(folder: URL): Promise<URL[]> {
const entries = await readdir(folder, { withFileTypes: true });
return entries.map((entry) => new URL(entry.name, folder));
}
/**
* Read and parse a single KDL file into its KDL Document structure.
*/
async function readPatchDocument(fileUrl: URL): Promise<Document> {
const text = await readFile(fileUrl, "utf8");
const { output, errors } = parse(text);
if (errors.length) {
throw new Error(`KDL parse errors in ${fileUrl.toString()}`, {
cause: errors,
});
}
return output!;
}
/**
* Recursively remove all 'name' fields from the object and its children, and
* replace any empty objects ({} or []) with null.
*/
function convertForRemovals(obj: unknown): unknown {
if (Array.isArray(obj)) {
return obj.map(convertForRemovals).filter((v) => v !== undefined);
}
if (obj && typeof obj === "object") {
const newObj: Record<string, unknown> = {};
for (const [key, value] of Object.entries(obj)) {
if (key !== "name") {
const cleaned = convertForRemovals(value);
// (intentionally covers null too)
if (typeof cleaned === "object") {
newObj[key] = cleaned;
} else if (cleaned !== undefined) {
newObj[key] = null;
}
}
}
// Replace empty objects with null
return Object.keys(newObj).length === 0 ? null : newObj;
}
return obj;
}
/**
* Read, parse, and merge all KDL files under the input folder.
* Splits the main patch content and the removals from each file for combined processing.
*
* Returns:
* {
* patches: merged patch contents (excluding removals),
* removalPatches: merged removals, with names stripped
* }
*/
export default async function readPatches(): Promise<{
patches: any;
removalPatches: any;
}> {
const patchDirectory = new URL("../../inputfiles/patches/", import.meta.url);
const fileUrls = await getAllFileURLs(patchDirectory);
// Stage 1: Parse all file KDLs into Documents
const documents = await Promise.all(fileUrls.map(readPatchDocument));
// Stage 2: Group by patches or removals
const merged = documents.flat();
const patchNodes = merged.filter((node) => node.name !== "removals");
const removalNodes = merged
.filter((node) => node.name === "removals")
.map((node) => node.children)
.flat();
// Stage 3: Convert the nodes for patches and removals respectively
const patches = convertKDLNodes(patchNodes);
const removalPatches = convertForRemovals(
convertKDLNodes(removalNodes),
) as DeepPartial<WebIdl>;
return { patches, removalPatches };
}