Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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, Omit<Property, "type">>;

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

/**
* Handles a mixin node by extracting its name and associated events.
* Extracts all event child nodes from a mixin node and returns them as an array of Event objects.
* Each event object contains the event's name and type.
* @param node The mixin node to extract events from.
* @returns An array of Event objects.
*/
function extractMixinEvents(node: Node): Event[] {
const rawEvents = node.children.filter(
(child: any) => child.name === "event",
);
return rawEvents.map((child: any) => ({
name: child.values[0],
type: child.properties.type,
}));
}

/**
* Extracts all property child nodes from a mixin node and returns them as a Properties object.
* Each property is keyed by its name and contains its name and exposed value.
* @param node The mixin node to extract properties from.
* @returns A Properties object mapping property names to property details.
*/
function extractMixinProperties(node: Node): Properties {
const rawProperties = node.children.filter(
(child: any) => child.name === "property",
);
return rawProperties.reduce((acc: Properties, child: any) => {
const name = child.values[0];
acc[name] = {
name,
exposed: child.properties?.exposed,
};
return acc;
}, {});
}

/**
* 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 +103,11 @@ 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[] = extractMixinEvents(node);
const property: Properties = extractMixinProperties(node);
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 we'd want to iterate on children rather than filtering, so that if there's an unknown child then it can throw. Just like how handleMixin is called from parseKDL.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is it now better @saschanaz, I have updated it :)


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