Skip to content

Commit 7bf5b55

Browse files
Bashamegasaschanaz
andauthored
Add support for type parameters for interfaces and nullable property (#2288)
Co-authored-by: saschanaz <saschanaz@users.noreply.github.com>
1 parent df66d7f commit 7bf5b55

File tree

3 files changed

+17
-25
lines changed

3 files changed

+17
-25
lines changed

inputfiles/overridingTypes.jsonc

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1310,26 +1310,6 @@
13101310
}
13111311
}
13121312
},
1313-
"IDBRequest": {
1314-
"properties": {
1315-
"property": {
1316-
"source": {
1317-
"name": "source",
1318-
"nullable": false
1319-
},
1320-
"result": {
1321-
"name": "result",
1322-
"overrideType": "T"
1323-
}
1324-
}
1325-
},
1326-
"typeParameters": [
1327-
{
1328-
"name": "T",
1329-
"default": "any"
1330-
}
1331-
]
1332-
},
13331313
"ImageBitmapRenderingContext": {
13341314
"properties": {
13351315
"property": {

inputfiles/patches/indexeddb.kdl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
interface IDBRequest {
2+
typeParameters T default=any
3+
property source {
4+
type nullable=#false
5+
}
6+
property result overrideType=T
7+
}

src/build/patches.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,15 @@ function string(arg: unknown): string {
4848
return arg;
4949
}
5050

51-
function handleTyped(type: Node): Typed {
51+
function handleTyped(type: Node): DeepPartial<Typed> {
5252
const isTyped = type.name == "type";
5353
if (!isTyped) {
5454
throw new Error("Expected a type node");
5555
}
56-
const name = string(type.values[0]);
5756
const subType =
5857
type.children.length > 0 ? handleTyped(type.children[0]) : undefined;
5958
return {
60-
type: name,
59+
...optionalMember("type", "string", type.values[0]),
6160
subtype: subType,
6261
...optionalMember("nullable", "boolean", type.properties?.nullable),
6362
};
@@ -167,8 +166,9 @@ function handleMixinAndInterfaces(
167166
const name = string(node.properties?.name || node.values[0]);
168167

169168
const event: Event[] = [];
170-
const property: Record<string, Partial<Property>> = {};
169+
const property: Record<string, DeepPartial<Property>> = {};
171170
let method: Record<string, DeepPartial<OverridableMethod>> = {};
171+
let typeParameters = {};
172172

173173
for (const child of node.children) {
174174
switch (child.name) {
@@ -188,12 +188,17 @@ function handleMixinAndInterfaces(
188188
});
189189
break;
190190
}
191+
case "typeParameters": {
192+
typeParameters = handleTypeParameters(child);
193+
break;
194+
}
191195
default:
192196
throw new Error(`Unknown node name: ${child.name}`);
193197
}
194198
}
195199

196200
const interfaceObject = type === "interface" && {
201+
...typeParameters,
197202
...optionalMember("exposed", "string", node.properties?.exposed),
198203
...optionalMember("deprecated", "string", node.properties?.deprecated),
199204
...optionalMember(
@@ -240,7 +245,7 @@ function handleEvent(child: Node): Event {
240245
* Handles a child node of type "property" and adds it to the property object.
241246
* @param child The child node to handle.
242247
*/
243-
function handleProperty(child: Node): Partial<Property> {
248+
function handleProperty(child: Node): DeepPartial<Property> {
244249
let typeNode: Node | undefined;
245250
for (const c of child.children) {
246251
if (c.name === "type") {

0 commit comments

Comments
 (0)