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 , Omit < Property , "type" > > ;
5
6
6
7
/**
7
8
* Converts patch files in KDL to match the [types](types.d.ts).
@@ -54,10 +55,46 @@ 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
+ * Extracts all event child nodes from a mixin node and returns them as an array of Event objects.
59
+ * Each event object contains the event's name and type.
60
+ * @param node The mixin node to extract events from.
61
+ * @returns An array of Event objects.
62
+ */
63
+ function extractMixinEvents ( node : Node ) : Event [ ] {
64
+ const rawEvents = node . children . filter (
65
+ ( child : any ) => child . name === "event" ,
66
+ ) ;
67
+ return rawEvents . map ( ( child : any ) => ( {
68
+ name : child . values [ 0 ] ,
69
+ type : child . properties . type ,
70
+ } ) ) ;
71
+ }
72
+
73
+ /**
74
+ * Extracts all property child nodes from a mixin node and returns them as a Properties object.
75
+ * Each property is keyed by its name and contains its name and exposed value.
76
+ * @param node The mixin node to extract properties from.
77
+ * @returns A Properties object mapping property names to property details.
78
+ */
79
+ function extractMixinProperties ( node : Node ) : Properties {
80
+ const rawProperties = node . children . filter (
81
+ ( child : any ) => child . name === "property" ,
82
+ ) ;
83
+ return rawProperties . reduce ( ( acc : Properties , child : any ) => {
84
+ const name = child . values [ 0 ] ;
85
+ acc [ name ] = {
86
+ name,
87
+ exposed : child . properties ?. exposed ,
88
+ } ;
89
+ return acc ;
90
+ } , { } ) ;
91
+ }
92
+
93
+ /**
94
+ * Handles a mixin node by extracting its name and associated events and properties.
58
95
* 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.
96
+ * Uses helper functions to collect events and properties.
97
+ * Adds them to the mixins record under the mixin's name.
61
98
* @param node The mixin node to handle.
62
99
* @param mixins The record of mixins to update.
63
100
*/
@@ -66,14 +103,11 @@ function handleMixin(node: Node, mixins: Record<string, any>) {
66
103
if ( typeof name !== "string" ) {
67
104
throw new Error ( "Missing mixin name" ) ;
68
105
}
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 } } ;
106
+
107
+ const event : Event [ ] = extractMixinEvents ( node ) ;
108
+ const property : Properties = extractMixinProperties ( node ) ;
109
+
110
+ mixins [ name ] = { name, events : { event } , properties : { property } } ;
77
111
}
78
112
79
113
/**
0 commit comments