Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
}
42 changes: 30 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,31 @@ 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":
event.push({
name,
type: child.properties.type as string,
});
break;
case "property":
property[name] = {
name,
exposed: child.properties?.exposed as string,
};
Copy link
Contributor

Choose a reason for hiding this comment

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

I think separate functions for each is better, as I'm sure this will become longer, even for property alone.

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 created smaller functions @saschanaz, would you prefer smaller files?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think this wouldn't become super huge to justify extra modules, for now let's keep it this way and see how it goes.

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.

}

/**
Expand Down