Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
}
60 changes: 48 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, Omit<Property, "type">>;

/**
* Converts patch files in KDL to match the [types](types.d.ts).
Expand Down Expand Up @@ -54,10 +55,10 @@ 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 events and properties.
* 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.
* Uses helper functions to collect events and properties.
* 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 +67,49 @@ 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) {
const name = child.values[0] as string;
switch (child.name) {
case "event":
handleEvent(child, event, name);
break;
case "property":
handleProperty(child, property, name);
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.
* @param event The event array to update.
*/
function handleEvent(child: Node, event: Event[], name: string) {
event.push({
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we return Event here and do event.push in the caller? Like event.push(handleEvent(child)).

Also probably better to get the name in each function, as I think not all members will have a name, e.g. constructor.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have updated it for events and properties

name,
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.
* @param property The property object to update.
*/
function handleProperty(child: Node, property: Properties, name: string) {
property[name] = {
name,
exposed: child.properties?.exposed as string,
};
}

/**
Expand Down