Skip to content

Commit ddd8639

Browse files
authored
Add more types and return value from handleEnum/Mixin (microsoft#2094)
Co-authored-by: saschanaz <[email protected]>
1 parent 40a54bf commit ddd8639

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

src/build/patches.ts

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import { parse, type Node } from "kdljs";
2-
import type { Enum, Event, Property } from "./types";
2+
import type { Enum, Event, Property, Interface, WebIdl } from "./types";
33
import { readdir, readFile } from "fs/promises";
44
import { merge } from "./helpers.js";
5-
type Properties = Record<string, Partial<Property>>;
5+
6+
type DeepPartial<T> = T extends object
7+
? { [K in keyof T]?: DeepPartial<T[K]> }
8+
: T;
69

710
/**
811
* Converts patch files in KDL to match the [types](types.d.ts).
912
*/
10-
function parseKDL(kdlText: string) {
13+
function parseKDL(kdlText: string): DeepPartial<WebIdl> {
1114
const { output, errors } = parse(kdlText);
1215

1316
if (errors.length) {
@@ -16,22 +19,26 @@ function parseKDL(kdlText: string) {
1619

1720
const nodes = output!;
1821
const enums: Record<string, Enum> = {};
19-
const mixins: Record<string, any> = {};
22+
const mixin: Record<string, DeepPartial<Interface>> = {};
2023

2124
for (const node of nodes) {
25+
const name = node.values[0];
26+
if (typeof name !== "string") {
27+
throw new Error(`Missing name for ${node.name}`);
28+
}
2229
switch (node.name) {
2330
case "enum":
24-
handleEnum(node, enums);
31+
enums[name] = handleEnum(node);
2532
break;
2633
case "interface-mixin":
27-
handleMixin(node, mixins);
34+
mixin[name] = handleMixin(node);
2835
break;
2936
default:
3037
throw new Error(`Unknown node name: ${node.name}`);
3138
}
3239
}
3340

34-
return { enums: { enum: enums }, mixins: { mixin: mixins } };
41+
return { enums: { enum: enums }, mixins: { mixin } };
3542
}
3643

3744
/**
@@ -40,7 +47,7 @@ function parseKDL(kdlText: string) {
4047
* @param node The enum node to handle.
4148
* @param enums The record of enums to update.
4249
*/
43-
function handleEnum(node: Node, enums: Record<string, Enum>) {
50+
function handleEnum(node: Node): Enum {
4451
const name = node.values[0];
4552
if (typeof name !== "string") {
4653
throw new Error("Missing enum name");
@@ -51,7 +58,7 @@ function handleEnum(node: Node, enums: Record<string, Enum>) {
5158
values.push(child.name);
5259
}
5360

54-
enums[name] = { name, value: values };
61+
return { name, value: values };
5562
}
5663

5764
/**
@@ -61,14 +68,14 @@ function handleEnum(node: Node, enums: Record<string, Enum>) {
6168
* @param node The mixin node to handle.
6269
* @param mixins The record of mixins to update.
6370
*/
64-
function handleMixin(node: Node, mixins: Record<string, any>) {
71+
function handleMixin(node: Node): DeepPartial<Interface> {
6572
const name = node.values[0];
6673
if (typeof name !== "string") {
6774
throw new Error("Missing mixin name");
6875
}
6976

7077
const event: Event[] = [];
71-
const property: Properties = {};
78+
const property: Record<string, Partial<Property>> = {};
7279

7380
for (const child of node.children) {
7481
switch (child.name) {
@@ -85,14 +92,18 @@ function handleMixin(node: Node, mixins: Record<string, any>) {
8592
}
8693
}
8794

88-
mixins[name] = { name, events: { event }, properties: { property } };
95+
return {
96+
name,
97+
events: { event },
98+
properties: { property },
99+
} as DeepPartial<Interface>;
89100
}
90101

91102
/**
92103
* Handles a child node of type "event" and adds it to the event array.
93104
* @param child The child node to handle.
94105
*/
95-
function handleEvent(child: Node) {
106+
function handleEvent(child: Node): Event {
96107
return {
97108
name: child.values[0] as string,
98109
type: child.properties.type as string,
@@ -103,7 +114,7 @@ function handleEvent(child: Node) {
103114
* Handles a child node of type "property" and adds it to the property object.
104115
* @param child The child node to handle.
105116
*/
106-
function handleProperty(child: Node) {
117+
function handleProperty(child: Node): Partial<Property> {
107118
return {
108119
name: child.values[0] as string,
109120
exposed: child.properties?.exposed as string,

0 commit comments

Comments
 (0)