Skip to content

Commit f7c15b7

Browse files
committed
Enhance mixin handling in KDL parser by adding functions to extract events and properties from mixin nodes. Update handleMixin to utilize these new functions, improving code organization and clarity.
1 parent 5b2dec6 commit f7c15b7

File tree

1 file changed

+46
-12
lines changed

1 file changed

+46
-12
lines changed

src/build/patches.ts

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { parse, type Node } from "kdljs";
2-
import type { Enum, Event } from "./types";
2+
import type { Enum, Event, Property } from "./types";
33
import { readdir, readFile } from "fs/promises";
44
import { merge } from "./helpers.js";
5+
type Properties = Record<string, Omit<Property, "type">>;
56

67
/**
78
* Converts patch files in KDL to match the [types](types.d.ts).
@@ -54,10 +55,46 @@ function handleEnum(node: Node, enums: Record<string, Enum>) {
5455
}
5556

5657
/**
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.
5895
* 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.
6198
* @param node The mixin node to handle.
6299
* @param mixins The record of mixins to update.
63100
*/
@@ -66,14 +103,11 @@ function handleMixin(node: Node, mixins: Record<string, any>) {
66103
if (typeof name !== "string") {
67104
throw new Error("Missing mixin name");
68105
}
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 } };
77111
}
78112

79113
/**

0 commit comments

Comments
 (0)