Skip to content

Commit b056cf1

Browse files
committed
split into smaller functions
1 parent a5176a7 commit b056cf1

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

src/build/patches.ts

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,34 @@ export function parseKDL(kdlText: string) {
1818

1919
for (const node of nodes) {
2020
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+
}
2724

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+
}
3627

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}`");
3844
}
45+
values.push(child.values[0]);
3946
}
4047

41-
return { enums: { enum: enums } };
48+
enums[name] = { name, value: values };
4249
}
4350

4451
/**

0 commit comments

Comments
 (0)