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
10 changes: 0 additions & 10 deletions inputfiles/addedTypes.jsonc
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
{
"mixins": {
"mixin": {
"AbstractWorker": {
"events": {
"event": [
{
"name": "error",
"type": "ErrorEvent"
}
]
}
},
"DocumentAndElementEventHandlers": {
"events": {
"event": [
Expand Down
3 changes: 3 additions & 0 deletions inputfiles/patches/events.kdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
interface-mixin AbstractWorker {
event error type=ErrorEvent
}
45 changes: 38 additions & 7 deletions src/build/patches.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { parse } from "kdljs";
import type { Enum } from "./types";
import { parse, type Node } from "kdljs";
import type { Enum, Event } from "./types";
import { readdir, readFile } from "fs/promises";
import { merge } from "./helpers.js";

Expand All @@ -15,14 +15,22 @@ function parseKDL(kdlText: string) {

const nodes = output!;
const enums: Record<string, Enum> = {};
const mixins: Record<string, any> = {};

for (const node of nodes) {
if (node.name === "enum") {
handleEnum(node, enums);
switch (node.name) {
case "enum":
handleEnum(node, enums);
break;
case "interface-mixin":
handleMixin(node, mixins);
break;
default:
throw new Error(`Unknown node name: ${node.name}`);
}
}

return { enums: { enum: enums } };
return { enums: { enum: enums }, mixins: { mixin: mixins } };
}

/**
Expand All @@ -31,20 +39,43 @@ function parseKDL(kdlText: string) {
* @param node The enum node to handle.
* @param enums The record of enums to update.
*/
function handleEnum(node: any, enums: Record<string, Enum>) {
function handleEnum(node: Node, enums: Record<string, Enum>) {
const name = node.values[0];
if (typeof name !== "string") {
throw new Error("Missing enum name");
}
const values: string[] = [];

for (const child of node.children ?? []) {
for (const child of node.children) {
values.push(child.name);
}

enums[name] = { name, value: values };
}

/**
* Handles a mixin node by extracting its name and associated events.
* 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.
* @param node The mixin node to handle.
* @param mixins The record of mixins to update.
*/
function handleMixin(node: Node, mixins: Record<string, any>) {
const name = node.values[0];
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 } };
}

/**
* Collect all file URLs in a directory.
*/
Expand Down