Skip to content

Commit de9e7bd

Browse files
committed
Refactor: Update method signatures in KDL to include signatureIndex for better handling of overloaded methods
1 parent dd5502e commit de9e7bd

File tree

2 files changed

+35
-60
lines changed

2 files changed

+35
-60
lines changed

inputfiles/patches/scroll.kdl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
interface Element {
2-
method scroll overrideType=void
3-
method scroll overrideType=void
4-
method scrollBy overrideType=void
5-
method scrollBy overrideType=void
6-
method scrollTo overrideType=void
7-
method scrollTo overrideType=void
8-
method scrollIntoView overrideType=void
2+
method scroll overrideType=void signatureIndex=0
3+
method scroll overrideType=void signatureIndex=1
4+
method scrollBy overrideType=void signatureIndex=0
5+
method scrollBy overrideType=void signatureIndex=1
6+
method scrollTo overrideType=void signatureIndex=0
7+
method scrollTo overrideType=void signatureIndex=1
8+
method scrollIntoView overrideType=void signatureIndex=0
99
}

src/build/patches.ts

Lines changed: 28 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type {
55
Property,
66
Interface,
77
WebIdl,
8-
Method,
8+
Method as originalMethod,
99
Typed,
1010
Dictionary,
1111
Member,
@@ -18,6 +18,10 @@ type DeepPartial<T> = T extends object
1818
? { [K in keyof T]?: DeepPartial<T[K]> }
1919
: T;
2020

21+
interface Method extends Omit<originalMethod, "signature"> {
22+
signature: Signature[] | Record<string, Signature>
23+
}
24+
2125
function optionalMember<const T>(prop: string, type: T, value?: Value) {
2226
if (value === undefined) {
2327
return {};
@@ -155,7 +159,7 @@ function handleMixinandInterfaces(
155159

156160
const event: Event[] = [];
157161
const property: Record<string, Partial<Property>> = {};
158-
const method: Record<string, Partial<Method>> = {};
162+
let method: Record<string, DeepPartial<Method>> = {};
159163

160164
for (const child of node.children) {
161165
switch (child.name) {
@@ -170,44 +174,16 @@ function handleMixinandInterfaces(
170174
case "method": {
171175
const methodName = string(child.values[0]);
172176
const m = handleMethod(child);
173-
if (method[methodName]) {
174-
// ts: The goal here is to merge multiple method signatures together for methods with the same name.
175-
const existingSig = method[methodName].signature;
176-
const newSigEntry = Array.isArray(m.signature)
177-
? m.signature[0]
178-
: m.signature && (m.signature as any)[0];
179-
if (Array.isArray(existingSig)) {
180-
// Both are arrays, push new entry (if newSigEntry is available)
181-
if (newSigEntry !== undefined) {
182-
existingSig.push(newSigEntry);
183-
}
184-
} else if (
185-
existingSig &&
186-
typeof existingSig === "object" &&
187-
!Array.isArray(existingSig)
188-
) {
189-
// Existing is an object, add next numeric key
190-
let nextKey = 0;
191-
// Only own, string keys that are numbers
192-
while (
193-
Object.prototype.hasOwnProperty.call(existingSig, String(nextKey))
194-
) {
195-
nextKey++;
196-
}
197-
if (newSigEntry !== undefined) {
198-
(existingSig as Record<string, any>)[String(nextKey)] =
199-
newSigEntry;
200-
}
201-
}
202-
break;
203-
}
204-
method[methodName] = m;
177+
method = merge(method, {
178+
[methodName]: m
179+
});
205180
break;
206181
}
207182
default:
208183
throw new Error(`Unknown node name: ${child.name}`);
209184
}
210185
}
186+
console.log(method)
211187

212188
const interfaceObject = type === "interface" && {
213189
...optionalMember("exposed", "string", node.properties?.exposed),
@@ -271,7 +247,7 @@ function handleProperty(child: Node): Partial<Property> {
271247
* Handles a child node of type "method" and adds it to the method object.
272248
* @param child The child node to handle.
273249
*/
274-
function handleMethod(child: Node): Partial<Method> {
250+
function handleMethod(child: Node): DeepPartial<Method> {
275251
const name = string(child.values[0]);
276252

277253
let typeNode: Node | undefined;
@@ -298,24 +274,23 @@ function handleMethod(child: Node): Partial<Method> {
298274
}
299275
}
300276

301-
const signature:
302-
| DeepPartial<Signature>[]
303-
| Record<string, DeepPartial<Signature>> = child.properties?.overrideType
304-
? {
305-
"0": {
306-
overrideType: string(child.properties?.overrideType),
307-
param: params,
308-
},
309-
}
310-
: [
311-
{
312-
param: params,
313-
...(typeNode
314-
? handleTyped(typeNode)
315-
: { type: string(child.properties?.returns) }),
316-
},
317-
];
318-
return { name, signature } as Partial<Method>;
277+
// Determine the actual signature object
278+
const signatureObj: DeepPartial<Signature> = {
279+
param: params,
280+
...(typeNode
281+
? handleTyped(typeNode)
282+
: { ...optionalMember("type", "string", child.properties?.returns), ...optionalMember("overrideType", "string", child.properties?.overrideType) }),
283+
};
284+
285+
let signature: Record<string, DeepPartial<Signature>> | DeepPartial<Signature>[];
286+
const signatureIndex = child.properties?.signatureIndex;
287+
if (typeof signatureIndex == "number") {
288+
signature = { [signatureIndex]: signatureObj };
289+
} else {
290+
signature = [signatureObj];
291+
}
292+
return { name, signature };
293+
319294
}
320295

321296
/**

0 commit comments

Comments
 (0)