From 91a589220a928c359c0061affd772881601da33b Mon Sep 17 00:00:00 2001 From: Bashamega Date: Fri, 25 Jul 2025 15:37:54 +0300 Subject: [PATCH 1/6] Refactor mixin handling in KDL parser - Removed AbstractWorker event definitions from addedTypes.jsonc. - Introduced abstractworker.kdl to define AbstractWorker mixin with error event. - Updated patches.ts to handle mixin parsing and return mixins in the output. --- inputfiles/addedTypes.jsonc | 10 -------- inputfiles/patches/abstractworker.kdl | 3 +++ src/build/patches.ts | 33 +++++++++++++++++++++++---- 3 files changed, 32 insertions(+), 14 deletions(-) create mode 100644 inputfiles/patches/abstractworker.kdl diff --git a/inputfiles/addedTypes.jsonc b/inputfiles/addedTypes.jsonc index 62a55b192..df27d57c5 100644 --- a/inputfiles/addedTypes.jsonc +++ b/inputfiles/addedTypes.jsonc @@ -1,16 +1,6 @@ { "mixins": { "mixin": { - "AbstractWorker": { - "events": { - "event": [ - { - "name": "error", - "type": "ErrorEvent" - } - ] - } - }, "DocumentAndElementEventHandlers": { "events": { "event": [ diff --git a/inputfiles/patches/abstractworker.kdl b/inputfiles/patches/abstractworker.kdl new file mode 100644 index 000000000..777088a7b --- /dev/null +++ b/inputfiles/patches/abstractworker.kdl @@ -0,0 +1,3 @@ +mixin AbstractWorker event { + error ErrorEvent +} \ No newline at end of file diff --git a/src/build/patches.ts b/src/build/patches.ts index b2875f6b2..b57323c53 100644 --- a/src/build/patches.ts +++ b/src/build/patches.ts @@ -1,5 +1,5 @@ import { parse } from "kdljs"; -import type { Enum } from "./types"; +import type { Enum, Event } from "./types"; import { readdir, readFile } from "fs/promises"; import { merge } from "./helpers.js"; @@ -15,14 +15,22 @@ function parseKDL(kdlText: string) { const nodes = output!; const enums: Record = {}; + const mixins: Record = {}; for (const node of nodes) { - if (node.name === "enum") { - handleEnum(node, enums); + switch (node.name) { + case "enum": + handleEnum(node, enums); + break; + case "mixin": + handelMixin(node, mixins); + break; + default: + throw new Error(`Unknown node name: ${node.name}`); } } - return { enums: { enum: enums } }; + return { enums: { enum: enums }, mixins: { mixin: mixins } }; } /** @@ -45,6 +53,23 @@ function handleEnum(node: any, enums: Record) { enums[name] = { name, value: values }; } +export function handelMixin(node: any, mixins: Record) { + const name = node.values[0]; + if (typeof name !== "string") { + throw new Error("Missing mixin name"); + } + const event: Event[] = []; + if (node.values[1] == "event") { + node.children?.forEach((child: any) => { + event.push({ + name: child.name, + type: child.values[0], + }); + }); + } + mixins[name] = { name, events: { event } }; +} + /** * Collect all file URLs in a directory. */ From b69cdd76ec3c8833a633905bca36e2bb2ea94ff8 Mon Sep 17 00:00:00 2001 From: Bashamega Date: Fri, 25 Jul 2025 15:41:36 +0300 Subject: [PATCH 2/6] chore: add documentation for the function --- src/build/patches.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/build/patches.ts b/src/build/patches.ts index b57323c53..e8fbfbadb 100644 --- a/src/build/patches.ts +++ b/src/build/patches.ts @@ -53,6 +53,14 @@ function handleEnum(node: any, enums: Record) { 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 handelMixin(node: any, mixins: Record) { const name = node.values[0]; if (typeof name !== "string") { From 368a4dd81f35cb6ff198ccdb8640c90b36e470d5 Mon Sep 17 00:00:00 2001 From: Bashamega Date: Sat, 26 Jul 2025 09:17:42 +0300 Subject: [PATCH 3/6] Update --- inputfiles/patches/abstractworker.kdl | 4 ++-- src/build/patches.ts | 17 +++++++++-------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/inputfiles/patches/abstractworker.kdl b/inputfiles/patches/abstractworker.kdl index 777088a7b..6834e58dd 100644 --- a/inputfiles/patches/abstractworker.kdl +++ b/inputfiles/patches/abstractworker.kdl @@ -1,3 +1,3 @@ -mixin AbstractWorker event { - error ErrorEvent +interface-mixin AbstractWorker { + event error type=ErrorEvent } \ No newline at end of file diff --git a/src/build/patches.ts b/src/build/patches.ts index e8fbfbadb..71ccb0c2a 100644 --- a/src/build/patches.ts +++ b/src/build/patches.ts @@ -22,7 +22,7 @@ function parseKDL(kdlText: string) { case "enum": handleEnum(node, enums); break; - case "mixin": + case "interface-mixin": handelMixin(node, mixins); break; default: @@ -67,14 +67,15 @@ export function handelMixin(node: any, mixins: Record) { throw new Error("Missing mixin name"); } const event: Event[] = []; - if (node.values[1] == "event") { - node.children?.forEach((child: any) => { - event.push({ - name: child.name, - type: child.values[0], - }); + const rawEvents = node.children.filter( + (child: any) => child.name === "event", + ); + rawEvents.forEach((child: any) => { + event.push({ + name: child.values[0], + type: child.properties.type, }); - } + }); mixins[name] = { name, events: { event } }; } From 8520d048a6d85b7c1cffcac7aa078c5d46e74d4f Mon Sep 17 00:00:00 2001 From: Bashamega Date: Sat, 26 Jul 2025 09:19:33 +0300 Subject: [PATCH 4/6] chore: rename function --- src/build/patches.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/build/patches.ts b/src/build/patches.ts index 71ccb0c2a..adab7c27e 100644 --- a/src/build/patches.ts +++ b/src/build/patches.ts @@ -23,7 +23,7 @@ function parseKDL(kdlText: string) { handleEnum(node, enums); break; case "interface-mixin": - handelMixin(node, mixins); + handleMixin(node, mixins); break; default: throw new Error(`Unknown node name: ${node.name}`); @@ -61,7 +61,7 @@ function handleEnum(node: any, enums: Record) { * @param node The mixin node to handle. * @param mixins The record of mixins to update. */ -export function handelMixin(node: any, mixins: Record) { +export function handleMixin(node: any, mixins: Record) { const name = node.values[0]; if (typeof name !== "string") { throw new Error("Missing mixin name"); From 5b611b804c24908b83520167d207d3a553848d3e Mon Sep 17 00:00:00 2001 From: Bashamega Date: Sat, 26 Jul 2025 09:25:12 +0300 Subject: [PATCH 5/6] Refactor event extraction in handleMixin function - Simplified event array creation using map instead of forEach. - Improved readability and performance of the event handling logic. --- src/build/patches.ts | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/build/patches.ts b/src/build/patches.ts index adab7c27e..5ca0f182a 100644 --- a/src/build/patches.ts +++ b/src/build/patches.ts @@ -66,16 +66,13 @@ export function handleMixin(node: any, mixins: Record) { if (typeof name !== "string") { throw new Error("Missing mixin name"); } - const event: Event[] = []; const rawEvents = node.children.filter( (child: any) => child.name === "event", ); - rawEvents.forEach((child: any) => { - event.push({ - name: child.values[0], - type: child.properties.type, - }); - }); + const event: Event[] = rawEvents.map((child: any) => ({ + name: child.values[0], + type: child.properties.type, + })); mixins[name] = { name, events: { event } }; } From 39ac3c4d135597364fec13c350e454a3ddb46af9 Mon Sep 17 00:00:00 2001 From: Bashamega Date: Sun, 27 Jul 2025 08:00:09 +0300 Subject: [PATCH 6/6] - --- inputfiles/patches/{abstractworker.kdl => events.kdl} | 2 +- src/build/patches.ts | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) rename inputfiles/patches/{abstractworker.kdl => events.kdl} (97%) diff --git a/inputfiles/patches/abstractworker.kdl b/inputfiles/patches/events.kdl similarity index 97% rename from inputfiles/patches/abstractworker.kdl rename to inputfiles/patches/events.kdl index 6834e58dd..1d8c40f68 100644 --- a/inputfiles/patches/abstractworker.kdl +++ b/inputfiles/patches/events.kdl @@ -1,3 +1,3 @@ interface-mixin AbstractWorker { event error type=ErrorEvent -} \ No newline at end of file +} diff --git a/src/build/patches.ts b/src/build/patches.ts index 5ca0f182a..86ba83232 100644 --- a/src/build/patches.ts +++ b/src/build/patches.ts @@ -1,4 +1,4 @@ -import { parse } from "kdljs"; +import { parse, type Node } from "kdljs"; import type { Enum, Event } from "./types"; import { readdir, readFile } from "fs/promises"; import { merge } from "./helpers.js"; @@ -39,14 +39,14 @@ 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) { +function handleEnum(node: Node, enums: Record) { 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); } @@ -61,7 +61,7 @@ function handleEnum(node: any, enums: Record) { * @param node The mixin node to handle. * @param mixins The record of mixins to update. */ -export function handleMixin(node: any, mixins: Record) { +function handleMixin(node: Node, mixins: Record) { const name = node.values[0]; if (typeof name !== "string") { throw new Error("Missing mixin name");