Skip to content

Commit 8f6b062

Browse files
committed
Add support for parsing dictionary nodes in KDL
1 parent 7179603 commit 8f6b062

File tree

4 files changed

+59
-22
lines changed

4 files changed

+59
-22
lines changed

inputfiles/overridingTypes.jsonc

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3597,28 +3597,6 @@
35973597
},
35983598
"dictionaries": {
35993599
"dictionary": {
3600-
"FontFaceDescriptors": {
3601-
"members": {
3602-
"member": {
3603-
"display":{
3604-
"type": "FontDisplay"
3605-
}
3606-
}
3607-
}
3608-
},
3609-
// https://github.com/microsoft/TypeScript/issues/46036
3610-
"CryptoKeyPair": {
3611-
"members": {
3612-
"member": {
3613-
"privateKey": {
3614-
"required": true
3615-
},
3616-
"publicKey": {
3617-
"required": true
3618-
}
3619-
}
3620-
}
3621-
},
36223600
"StructuredSerializeOptions": {
36233601
"members": {
36243602
"member": {

inputfiles/patches/crypto.kdl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//https://github.com/microsoft/TypeScript/issues/46036
2+
3+
dictionary CryptoKeyPair {
4+
member privateKey required=#true
5+
member publicKey required=#true
6+
}

inputfiles/patches/css-font.kdl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,9 @@ enum FontDisplay {
66
fallback
77
optional
88
}
9+
10+
dictionary FontFaceDescriptors {
11+
member display {
12+
type FontDisplay
13+
}
14+
}

src/build/patches.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import type {
77
WebIdl,
88
Method,
99
Typed,
10+
Dictionary,
11+
Member,
1012
} from "./types.js";
1113
import { readdir, readFile } from "fs/promises";
1214
import { merge } from "./helpers.js";
@@ -82,6 +84,7 @@ function parseKDL(kdlText: string): DeepPartial<WebIdl> {
8284
const enums: Record<string, Enum> = {};
8385
const mixin: Record<string, DeepPartial<Interface>> = {};
8486
const interfaces: Record<string, DeepPartial<Interface>> = {};
87+
const dictionary: Record<string, DeepPartial<Dictionary>> = {};
8588

8689
for (const node of nodes) {
8790
const name = string(node.values[0]);
@@ -95,6 +98,9 @@ function parseKDL(kdlText: string): DeepPartial<WebIdl> {
9598
case "interface":
9699
interfaces[name] = handleMixinandInterfaces(node, "interface");
97100
break;
101+
case "dictionary":
102+
dictionary[name] = handleDictionary(node);
103+
break;
98104
default:
99105
throw new Error(`Unknown node name: ${node.name}`);
100106
}
@@ -104,6 +110,7 @@ function parseKDL(kdlText: string): DeepPartial<WebIdl> {
104110
enums: { enum: enums },
105111
mixins: { mixin },
106112
interfaces: { interface: interfaces },
113+
dictionaries: { dictionary },
107114
};
108115
}
109116

@@ -268,6 +275,46 @@ function handleMethod(child: Node): Partial<Method> {
268275
return { name, signature };
269276
}
270277

278+
/**
279+
* Handles dictionary nodes
280+
* @param child The dictionary node to handle.
281+
*/
282+
function handleDictionary(child: Node): DeepPartial<Dictionary> {
283+
const name = string(child.values[0]);
284+
const member: Record<string, Partial<Member>> = {};
285+
286+
for (const c of child.children) {
287+
switch (c.name) {
288+
case "member": {
289+
const memberName = string(c.values[0]);
290+
member[memberName] = handleMember(c);
291+
break;
292+
}
293+
default:
294+
throw new Error(`Unknown node name: ${c.name}`);
295+
}
296+
}
297+
298+
return {
299+
name,
300+
members: { member },
301+
};
302+
}
303+
304+
/**
305+
* Handles dictionary member nodes
306+
* @param c The member node to handle.
307+
*/
308+
function handleMember(c: Node): Partial<Member> {
309+
const name = string(c.values[0]);
310+
const type: Node | undefined = c.children.find((n) => n.name === "type");
311+
return {
312+
name,
313+
...(type ? handleTyped(type) : {}),
314+
...optionalMember("required", "boolean", c.properties?.required),
315+
};
316+
}
317+
271318
/**
272319
* Collect all file URLs in a directory.
273320
*/

0 commit comments

Comments
 (0)