Skip to content

Commit 501d38e

Browse files
committed
-
1 parent 36e43e3 commit 501d38e

File tree

1 file changed

+7
-24
lines changed

1 file changed

+7
-24
lines changed

src/build/patches.ts

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { parse, type Value, type Node } from "kdljs";
1+
import { parse, 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,24 +7,6 @@ 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-
2810
/**
2911
* Converts patch files in KDL to match the [types](types.d.ts).
3012
*/
@@ -110,12 +92,15 @@ function handleMixin(node: Node): DeepPartial<Interface> {
11092
}
11193
}
11294

113-
return {
95+
const result = {
11496
name,
11597
events: { event },
11698
properties: { property },
117-
...optionalMember("extends", "string", node.properties?.extends),
11899
} as DeepPartial<Interface>;
100+
if (node.properties.extends) {
101+
result.extends = node.properties.extends as string;
102+
}
103+
return result;
119104
}
120105

121106
/**
@@ -136,9 +121,7 @@ function handleEvent(child: Node): Event {
136121
function handleProperty(child: Node): Partial<Property> {
137122
return {
138123
name: child.values[0] as string,
139-
...optionalMember("exposed", "string", child.properties?.exposed),
140-
...optionalMember("optional", "boolean", child.properties?.optional),
141-
...optionalMember("overrideType", "string", child.properties?.overrideType),
124+
exposed: child.properties?.exposed as string,
142125
};
143126
}
144127

0 commit comments

Comments
 (0)