Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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/abstractworker.kdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
interface-mixin AbstractWorker {
Copy link
Contributor

Choose a reason for hiding this comment

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

In this specific case, I'd prefer to put all the event things to a single file as events.kdl, so that I can see which events are not covered by automation and can ultimately remove this file.

In this PR just the filename can change, no addition is needed.

(Because all event data technically exists, we don't have a way to track that to assign automatically right now)

event error type=ErrorEvent
}
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 have a final newline - if you use VSCode then "Files: Insert Final Newline" should do it for you.

39 changes: 35 additions & 4 deletions src/build/patches.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { parse } from "kdljs";
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
import { parse } from "kdljs";
import { parse, type Node } from "kdljs";

import type { Enum } from "./types";
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 @@ -45,6 +53,29 @@ function handleEnum(node: any, enums: Record<string, Enum>) {
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.
*/
export function handleMixin(node: any, mixins: Record<string, any>) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
export function handleMixin(node: any, mixins: Record<string, any>) {
function handleMixin(node: Node, mixins: Record<string, any>) {

(Something I missed last time)

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