@@ -18,27 +18,34 @@ export function parseKDL(kdlText: string) {
18
18
19
19
for ( const node of nodes ) {
20
20
if ( node . name === "enum" ) {
21
- // Handle enum
22
- const name = node . values [ 0 ] ;
23
- if ( typeof name !== "string" ) {
24
- throw new Error ( "Missing enum name" ) ;
25
- }
26
- const values : string [ ] = [ ] ;
21
+ handleEnum ( node , enums ) ;
22
+ }
23
+ }
27
24
28
- for ( const child of node . children ?? [ ] ) {
29
- if ( child . name !== "value" || typeof child . values [ 0 ] !== "string" ) {
30
- throw new Error (
31
- "enum values should be in the form of `value {name}`" ,
32
- ) ;
33
- }
34
- values . push ( child . values [ 0 ] ) ;
35
- }
25
+ return { enums : { enum : enums } } ;
26
+ }
36
27
37
- enums [ name ] = { name, value : values } ;
28
+ /**
29
+ * Handles an enum node by extracting its name and values.
30
+ * Throws an error if the enum name is missing or if the values are not in the correct format.
31
+ * @param node The enum node to handle.
32
+ * @param enums The record of enums to update.
33
+ */
34
+ function handleEnum ( node : any , enums : Record < string , Enum > ) {
35
+ const name = node . values [ 0 ] ;
36
+ if ( typeof name !== "string" ) {
37
+ throw new Error ( "Missing enum name" ) ;
38
+ }
39
+ const values : string [ ] = [ ] ;
40
+
41
+ for ( const child of node . children ?? [ ] ) {
42
+ if ( child . name !== "value" || typeof child . values [ 0 ] !== "string" ) {
43
+ throw new Error ( "enum values should be in the form of `value {name}`" ) ;
38
44
}
45
+ values . push ( child . values [ 0 ] ) ;
39
46
}
40
47
41
- return { enums : { enum : enums } } ;
48
+ enums [ name ] = { name , value : values } ;
42
49
}
43
50
44
51
/**
0 commit comments