Skip to content

Commit 58e6d23

Browse files
committed
Refactor type handling in patches.ts to enforce string validation
1 parent c5dcd9e commit 58e6d23

File tree

1 file changed

+21
-23
lines changed

1 file changed

+21
-23
lines changed

src/build/patches.ts

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,19 @@ function optionalMember<const T>(prop: string, type: T, value?: Value) {
3232
};
3333
}
3434

35-
function handleTyped(type: Node, returnType: string) {
35+
function string(arg: unknown): string {
36+
if (typeof arg !== "string") {
37+
throw new Error(`Expected a string but found ${typeof arg}`);
38+
}
39+
return arg;
40+
}
41+
42+
function handleTyped(type: Node, returnType?: Value) {
3643
const isTyped = type.name == "type";
37-
const name = isTyped ? (type.values[0] as string) : returnType;
44+
const name = string(isTyped ? type.values[0] : returnType);
3845
const subType =
3946
type.children.length > 0
40-
? { type: type.children[0].values[0] as string }
47+
? { type: string(type.children[0].values[0]) }
4148
: undefined;
4249
return {
4350
type: name,
@@ -61,10 +68,7 @@ function parseKDL(kdlText: string): DeepPartial<WebIdl> {
6168
const mixin: Record<string, DeepPartial<Interface>> = {};
6269

6370
for (const node of nodes) {
64-
const name = node.values[0];
65-
if (typeof name !== "string") {
66-
throw new Error(`Missing name for ${node.name}`);
67-
}
71+
const name = string(node.values[0]);
6872
switch (node.name) {
6973
case "enum":
7074
enums[name] = handleEnum(node);
@@ -87,10 +91,7 @@ function parseKDL(kdlText: string): DeepPartial<WebIdl> {
8791
* @param enums The record of enums to update.
8892
*/
8993
function handleEnum(node: Node): Enum {
90-
const name = node.properties?.name || node.values[0];
91-
if (typeof name !== "string") {
92-
throw new Error("Missing enum name");
93-
}
94+
const name = string(node.properties?.name || node.values[0]);
9495
const values: string[] = [];
9596

9697
for (const child of node.children) {
@@ -109,9 +110,6 @@ function handleEnum(node: Node): Enum {
109110
*/
110111
function handleMixin(node: Node): DeepPartial<Interface> {
111112
const name = node.values[0];
112-
if (typeof name !== "string") {
113-
throw new Error("Missing mixin name");
114-
}
115113

116114
const event: Event[] = [];
117115
const property: Record<string, Partial<Property>> = {};
@@ -123,12 +121,12 @@ function handleMixin(node: Node): DeepPartial<Interface> {
123121
event.push(handleEvent(child));
124122
break;
125123
case "property": {
126-
const propName = child.values[0] as string;
124+
const propName = string(child.values[0]);
127125
property[propName] = handleProperty(child);
128126
break;
129127
}
130128
case "method": {
131-
const methodName = child.values[0] as string;
129+
const methodName = string(child.values[0]);
132130
method[methodName] = handleMethod(child);
133131
break;
134132
}
@@ -152,8 +150,8 @@ function handleMixin(node: Node): DeepPartial<Interface> {
152150
*/
153151
function handleEvent(child: Node): Event {
154152
return {
155-
name: child.values[0] as string,
156-
type: child.properties.type as string,
153+
name: string(child.values[0]),
154+
type: string(child.properties.type),
157155
};
158156
}
159157

@@ -163,7 +161,7 @@ function handleEvent(child: Node): Event {
163161
*/
164162
function handleProperty(child: Node): Partial<Property> {
165163
return {
166-
name: child.values[0] as string,
164+
name: string(child.values[0]),
167165
...optionalMember("exposed", "string", child.properties?.exposed),
168166
...optionalMember("optional", "boolean", child.properties?.optional),
169167
...optionalMember("overrideType", "string", child.properties?.overrideType),
@@ -175,15 +173,15 @@ function handleProperty(child: Node): Partial<Property> {
175173
* @param child The child node to handle.
176174
*/
177175
function handleMethod(child: Node): Partial<Method> {
178-
const name = child.values[0] as string;
176+
const name = string(child.values[0]);
179177
const type = child.children[0];
180-
const returnType = child.properties.returns as string;
178+
const returnType = child.properties.returns;
181179

182180
const params = child.children
183181
.filter((c) => c.properties.type)
184182
.map((c) => ({
185-
name: c.values[0] as string,
186-
type: c.properties.type as string,
183+
name: string(c.values[0]),
184+
type: string(c.properties.type),
187185
}));
188186

189187
const signature: Method["signature"] = [

0 commit comments

Comments
 (0)