Skip to content

Commit 53c90c7

Browse files
Bashamegasaschanaz
andauthored
Add support for parsing dictionary nodes in KDL (microsoft#2228)
Co-authored-by: saschanaz <saschanaz@users.noreply.github.com>
1 parent 0a38f9e commit 53c90c7

File tree

4 files changed

+55
-22
lines changed

4 files changed

+55
-22
lines changed

inputfiles/overridingTypes.jsonc

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3573,28 +3573,6 @@
35733573
},
35743574
"dictionaries": {
35753575
"dictionary": {
3576-
"FontFaceDescriptors": {
3577-
"members": {
3578-
"member": {
3579-
"display":{
3580-
"type": "FontDisplay"
3581-
}
3582-
}
3583-
}
3584-
},
3585-
// https://github.com/microsoft/TypeScript/issues/46036
3586-
"CryptoKeyPair": {
3587-
"members": {
3588-
"member": {
3589-
"privateKey": {
3590-
"required": true
3591-
},
3592-
"publicKey": {
3593-
"required": true
3594-
}
3595-
}
3596-
}
3597-
},
35983576
"StructuredSerializeOptions": {
35993577
"members": {
36003578
"member": {

inputfiles/patches/crypto.kdl

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

inputfiles/patches/css-font.kdl

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

src/build/patches.ts

Lines changed: 46 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,45 @@ 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+
return {
311+
name,
312+
...optionalMember("type", "string", c.properties?.type),
313+
...optionalMember("required", "boolean", c.properties?.required),
314+
};
315+
}
316+
271317
/**
272318
* Collect all file URLs in a directory.
273319
*/

0 commit comments

Comments
 (0)