Skip to content

Commit 40a54bf

Browse files
Bashamegasaschanaz
andauthored
Handle Properties in kdl (#2088)
Co-authored-by: saschanaz <[email protected]>
1 parent 2bc0ce7 commit 40a54bf

File tree

3 files changed

+49
-21
lines changed

3 files changed

+49
-21
lines changed

inputfiles/addedTypes.jsonc

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,6 @@
2020
}
2121
}
2222
}
23-
},
24-
"WebGLRenderingContextBase": {
25-
"properties": {
26-
"property": {
27-
"canvas": {
28-
"exposed": "Window"
29-
}
30-
}
31-
}
3223
}
3324
}
3425
},

inputfiles/patches/webgl.kdl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
interface-mixin WebGLRenderingContextBase {
2+
property canvas exposed=Window
3+
}

src/build/patches.ts

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { parse, type Node } from "kdljs";
2-
import type { Enum, Event } from "./types";
2+
import type { Enum, Event, Property } from "./types";
33
import { readdir, readFile } from "fs/promises";
44
import { merge } from "./helpers.js";
5+
type Properties = Record<string, Partial<Property>>;
56

67
/**
78
* Converts patch files in KDL to match the [types](types.d.ts).
@@ -54,10 +55,9 @@ function handleEnum(node: Node, enums: Record<string, Enum>) {
5455
}
5556

5657
/**
57-
* Handles a mixin node by extracting its name and associated events.
58+
* Handles a mixin node by extracting its name and associated members.
5859
* Throws an error if the mixin name is missing.
59-
* If the mixin node specifies "event" as its second value, it collects all child nodes as events,
60-
* each with a name and type, and adds them to the mixins record under the mixin's name.
60+
* Adds them to the mixins record under the mixin's name.
6161
* @param node The mixin node to handle.
6262
* @param mixins The record of mixins to update.
6363
*/
@@ -66,14 +66,48 @@ function handleMixin(node: Node, mixins: Record<string, any>) {
6666
if (typeof name !== "string") {
6767
throw new Error("Missing mixin name");
6868
}
69-
const rawEvents = node.children.filter(
70-
(child: any) => child.name === "event",
71-
);
72-
const event: Event[] = rawEvents.map((child: any) => ({
73-
name: child.values[0],
74-
type: child.properties.type,
75-
}));
76-
mixins[name] = { name, events: { event } };
69+
70+
const event: Event[] = [];
71+
const property: Properties = {};
72+
73+
for (const child of node.children) {
74+
switch (child.name) {
75+
case "event":
76+
event.push(handleEvent(child));
77+
break;
78+
case "property": {
79+
const propName = child.values[0] as string;
80+
property[propName] = handleProperty(child);
81+
break;
82+
}
83+
default:
84+
throw new Error(`Unknown node name: ${child.name}`);
85+
}
86+
}
87+
88+
mixins[name] = { name, events: { event }, properties: { property } };
89+
}
90+
91+
/**
92+
* Handles a child node of type "event" and adds it to the event array.
93+
* @param child The child node to handle.
94+
*/
95+
function handleEvent(child: Node) {
96+
return {
97+
name: child.values[0] as string,
98+
type: child.properties.type as string,
99+
};
100+
}
101+
102+
/**
103+
* Handles a child node of type "property" and adds it to the property object.
104+
* @param child The child node to handle.
105+
*/
106+
function handleProperty(child: Node) {
107+
return {
108+
name: child.values[0] as string,
109+
exposed: child.properties?.exposed as string,
110+
};
77111
}
78112

79113
/**

0 commit comments

Comments
 (0)