1
1
import { parse , type Node } from "kdljs" ;
2
- import type { Enum , Event } from "./types" ;
2
+ import type { Enum , Event , Property } from "./types" ;
3
3
import { readdir , readFile } from "fs/promises" ;
4
4
import { merge } from "./helpers.js" ;
5
+ type Properties = Record < string , Partial < Property > > ;
5
6
6
7
/**
7
8
* Converts patch files in KDL to match the [types](types.d.ts).
@@ -54,10 +55,9 @@ function handleEnum(node: Node, enums: Record<string, Enum>) {
54
55
}
55
56
56
57
/**
57
- * Handles a mixin node by extracting its name and associated events .
58
+ * Handles a mixin node by extracting its name and associated members .
58
59
* 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.
61
61
* @param node The mixin node to handle.
62
62
* @param mixins The record of mixins to update.
63
63
*/
@@ -66,14 +66,48 @@ function handleMixin(node: Node, mixins: Record<string, any>) {
66
66
if ( typeof name !== "string" ) {
67
67
throw new Error ( "Missing mixin name" ) ;
68
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 } } ;
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
+ } ;
77
111
}
78
112
79
113
/**
0 commit comments