Skip to content

Commit 405e381

Browse files
committed
Revert "-"
This reverts commit 501d38e.
1 parent d504495 commit 405e381

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

src/build/patches.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { parse, type Node } from "kdljs";
1+
import { parse, type Value, type Node } from "kdljs";
22
import type { Enum, Event, Property, Interface, WebIdl } from "./types";
33
import { readdir, readFile } from "fs/promises";
44
import { merge } from "./helpers.js";
@@ -7,6 +7,24 @@ type DeepPartial<T> = T extends object
77
? { [K in keyof T]?: DeepPartial<T[K]> }
88
: T;
99

10+
function optionalMember<const T>(prop: string, type: T, value?: Value) {
11+
if (value === undefined) {
12+
return {};
13+
}
14+
if (typeof value !== type) {
15+
throw new Error(`Expected type ${value} for ${prop}`);
16+
}
17+
return {
18+
[prop]: value as T extends "string"
19+
? string
20+
: T extends "number"
21+
? number
22+
: T extends "boolean"
23+
? boolean
24+
: never,
25+
};
26+
}
27+
1028
/**
1129
* Converts patch files in KDL to match the [types](types.d.ts).
1230
*/
@@ -92,15 +110,12 @@ function handleMixin(node: Node): DeepPartial<Interface> {
92110
}
93111
}
94112

95-
const result = {
113+
return {
96114
name,
97115
events: { event },
98116
properties: { property },
117+
...optionalMember("extends", "string", node.properties?.extends),
99118
} as DeepPartial<Interface>;
100-
if (node.properties.extends) {
101-
result.extends = node.properties.extends as string;
102-
}
103-
return result;
104119
}
105120

106121
/**
@@ -121,7 +136,9 @@ function handleEvent(child: Node): Event {
121136
function handleProperty(child: Node): Partial<Property> {
122137
return {
123138
name: child.values[0] as string,
124-
exposed: child.properties?.exposed as string,
139+
...optionalMember("exposed", "string", child.properties?.exposed),
140+
...optionalMember("optional", "boolean", child.properties?.optional),
141+
...optionalMember("overrideType", "string", child.properties?.overrideType),
125142
};
126143
}
127144

0 commit comments

Comments
 (0)