11import { parse , type Node } from "kdljs" ;
2- import type { Enum , Event } from "./types" ;
2+ import type { Enum , Event , Property } from "./types" ;
33import { readdir , readFile } from "fs/promises" ;
44import { merge } from "./helpers.js" ;
5+ type Properties = Record < string , Partial < Property > > ;
56
67/**
78 * Converts patch files in KDL to match the [types](types.d.ts).
@@ -54,10 +55,9 @@ function handleEnum(node: Node, enums: Record<string, Enum>) {
5455}
5556
5657/**
57- * Handles a mixin node by extracting its name and associated events .
58+ * Handles a mixin node by extracting its name and associated members .
5859 * Throws an error if the mixin name is missing.
59- * If the mixin node specifies "event" as its second value, it collects all child nodes as events,
60- * each with a name and type, and adds them to the mixins record under the mixin's name.
60+ * Adds them to the mixins record under the mixin's name.
6161 * @param node The mixin node to handle.
6262 * @param mixins The record of mixins to update.
6363 */
@@ -66,14 +66,48 @@ function handleMixin(node: Node, mixins: Record<string, any>) {
6666 if ( typeof name !== "string" ) {
6767 throw new Error ( "Missing mixin name" ) ;
6868 }
69- const rawEvents = node . children . filter (
70- ( child : any ) => child . name === "event" ,
71- ) ;
72- const event : Event [ ] = rawEvents . map ( ( child : any ) => ( {
73- name : child . values [ 0 ] ,
74- type : child . properties . type ,
75- } ) ) ;
76- mixins [ name ] = { name, events : { event } } ;
69+
70+ const event : Event [ ] = [ ] ;
71+ const property : Properties = { } ;
72+
73+ for ( const child of node . children ) {
74+ switch ( child . name ) {
75+ case "event" :
76+ event . push ( handleEvent ( child ) ) ;
77+ break ;
78+ case "property" : {
79+ const propName = child . values [ 0 ] as string ;
80+ property [ propName ] = handleProperty ( child ) ;
81+ break ;
82+ }
83+ default :
84+ throw new Error ( `Unknown node name: ${ child . name } ` ) ;
85+ }
86+ }
87+
88+ mixins [ name ] = { name, events : { event } , properties : { property } } ;
89+ }
90+
91+ /**
92+ * Handles a child node of type "event" and adds it to the event array.
93+ * @param child The child node to handle.
94+ */
95+ function handleEvent ( child : Node ) {
96+ return {
97+ name : child . values [ 0 ] as string ,
98+ type : child . properties . type as string ,
99+ } ;
100+ }
101+
102+ /**
103+ * Handles a child node of type "property" and adds it to the property object.
104+ * @param child The child node to handle.
105+ */
106+ function handleProperty ( child : Node ) {
107+ return {
108+ name : child . values [ 0 ] as string ,
109+ exposed : child . properties ?. exposed as string ,
110+ } ;
77111}
78112
79113/**
0 commit comments