1- import { parse } from "kdljs" ;
2- import type { Enum } from "./types" ;
1+ import { parse , type Node } from "kdljs" ;
2+ import type { Enum , Event } from "./types" ;
33import { readdir , readFile } from "fs/promises" ;
44import { merge } from "./helpers.js" ;
55
@@ -15,14 +15,22 @@ function parseKDL(kdlText: string) {
1515
1616 const nodes = output ! ;
1717 const enums : Record < string , Enum > = { } ;
18+ const mixins : Record < string , any > = { } ;
1819
1920 for ( const node of nodes ) {
20- if ( node . name === "enum" ) {
21- handleEnum ( node , enums ) ;
21+ switch ( node . name ) {
22+ case "enum" :
23+ handleEnum ( node , enums ) ;
24+ break ;
25+ case "interface-mixin" :
26+ handleMixin ( node , mixins ) ;
27+ break ;
28+ default :
29+ throw new Error ( `Unknown node name: ${ node . name } ` ) ;
2230 }
2331 }
2432
25- return { enums : { enum : enums } } ;
33+ return { enums : { enum : enums } , mixins : { mixin : mixins } } ;
2634}
2735
2836/**
@@ -31,20 +39,43 @@ function parseKDL(kdlText: string) {
3139 * @param node The enum node to handle.
3240 * @param enums The record of enums to update.
3341 */
34- function handleEnum ( node : any , enums : Record < string , Enum > ) {
42+ function handleEnum ( node : Node , enums : Record < string , Enum > ) {
3543 const name = node . values [ 0 ] ;
3644 if ( typeof name !== "string" ) {
3745 throw new Error ( "Missing enum name" ) ;
3846 }
3947 const values : string [ ] = [ ] ;
4048
41- for ( const child of node . children ?? [ ] ) {
49+ for ( const child of node . children ) {
4250 values . push ( child . name ) ;
4351 }
4452
4553 enums [ name ] = { name, value : values } ;
4654}
4755
56+ /**
57+ * Handles a mixin node by extracting its name and associated events.
58+ * 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.
61+ * @param node The mixin node to handle.
62+ * @param mixins The record of mixins to update.
63+ */
64+ function handleMixin ( node : Node , mixins : Record < string , any > ) {
65+ const name = node . values [ 0 ] ;
66+ if ( typeof name !== "string" ) {
67+ throw new Error ( "Missing mixin name" ) ;
68+ }
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 } } ;
77+ }
78+
4879/**
4980 * Collect all file URLs in a directory.
5081 */
0 commit comments