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" ;
3
3
import { readdir , readFile } from "fs/promises" ;
4
4
import { merge } from "./helpers.js" ;
5
5
@@ -15,14 +15,22 @@ function parseKDL(kdlText: string) {
15
15
16
16
const nodes = output ! ;
17
17
const enums : Record < string , Enum > = { } ;
18
+ const mixins : Record < string , any > = { } ;
18
19
19
20
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 } ` ) ;
22
30
}
23
31
}
24
32
25
- return { enums : { enum : enums } } ;
33
+ return { enums : { enum : enums } , mixins : { mixin : mixins } } ;
26
34
}
27
35
28
36
/**
@@ -31,20 +39,43 @@ function parseKDL(kdlText: string) {
31
39
* @param node The enum node to handle.
32
40
* @param enums The record of enums to update.
33
41
*/
34
- function handleEnum ( node : any , enums : Record < string , Enum > ) {
42
+ function handleEnum ( node : Node , enums : Record < string , Enum > ) {
35
43
const name = node . values [ 0 ] ;
36
44
if ( typeof name !== "string" ) {
37
45
throw new Error ( "Missing enum name" ) ;
38
46
}
39
47
const values : string [ ] = [ ] ;
40
48
41
- for ( const child of node . children ?? [ ] ) {
49
+ for ( const child of node . children ) {
42
50
values . push ( child . name ) ;
43
51
}
44
52
45
53
enums [ name ] = { name, value : values } ;
46
54
}
47
55
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
+
48
79
/**
49
80
* Collect all file URLs in a directory.
50
81
*/
0 commit comments