Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions inputfiles/addedTypes.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,6 @@
}
}
}
},
"WebGLRenderingContextBase": {
"properties": {
"property": {
"canvas": {
"exposed": "Window"
}
}
}
}
}
},
Expand Down
3 changes: 3 additions & 0 deletions inputfiles/patches/webgl.kdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
interface-mixin WebGLRenderingContextBase {
property canvas exposed=Window
}
58 changes: 46 additions & 12 deletions src/build/patches.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { parse, type Node } from "kdljs";
import type { Enum, Event } from "./types";
import type { Enum, Event, Property } from "./types";
import { readdir, readFile } from "fs/promises";
import { merge } from "./helpers.js";
type Properties = Record<string, Partial<Property>>;

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

/**
* Handles a mixin node by extracting its name and associated events.
* Handles a mixin node by extracting its name and associated members.
* Throws an error if the mixin name is missing.
* If the mixin node specifies "event" as its second value, it collects all child nodes as events,
* each with a name and type, and adds them to the mixins record under the mixin's name.
* 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.
*/
Expand All @@ -66,14 +66,48 @@ function handleMixin(node: Node, mixins: Record<string, any>) {
if (typeof name !== "string") {
throw new Error("Missing mixin name");
}
const rawEvents = node.children.filter(
(child: any) => child.name === "event",
);
const event: Event[] = rawEvents.map((child: any) => ({
name: child.values[0],
type: child.properties.type,
}));
mixins[name] = { name, events: { event } };

const event: Event[] = [];
const property: Properties = {};

for (const child of node.children) {
switch (child.name) {
case "event":
event.push(handleEvent(child));
break;
case "property": {
const propName = child.values[0] as string;
property[propName] = handleProperty(child);
break;
}
default:
throw new Error(`Unknown node name: ${child.name}`);
}
}

mixins[name] = { name, events: { event }, properties: { property } };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to return a value as handleProperty now does. But as handleEnum already does this, this can be done in a separate PR.

}

/**
* 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) {
return {
name: child.values[0] as string,
type: child.properties.type as string,
};
}

/**
* 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) {
return {
name: child.values[0] as string,
exposed: child.properties?.exposed as string,
};
}

/**
Expand Down
Loading