forked from microsoft/TypeScript-DOM-lib-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlegacy-namespace.ts
More file actions
56 lines (51 loc) · 1.55 KB
/
Copy pathlegacy-namespace.ts
File metadata and controls
56 lines (51 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import * as Browser from "./types";
import { mapToArray, arrayToMap } from "./helpers";
export function collectLegacyNamespaceTypes(webidl: Browser.WebIdl): Browser.Interface[] {
if (!webidl.namespaces) {
return [];
}
const namespaceMap: Record<string, Browser.Interface> = arrayToMap(webidl.namespaces, i => i.name, i => i);
for (const i of mapToArray(webidl.interfaces!.interface)) {
if (i["legacy-namespace"]) {
getNamespace(i["legacy-namespace"]).nested!.interfaces.push(i);
}
}
for (const i of mapToArray(webidl.dictionaries!.dictionary)) {
if (i["legacy-namespace"]) {
getNamespace(i["legacy-namespace"]).nested!.dictionaries.push(i);
}
}
for (const i of mapToArray(webidl.enums!.enum)) {
if (i["legacy-namespace"]) {
getNamespace(i["legacy-namespace"]).nested!.enums.push(i);
}
}
for (const i of webidl.typedefs!.typedef) {
if (i["legacy-namespace"]) {
getNamespace(i["legacy-namespace"]).nested!.typedefs.push(i);
}
}
return mapToArray(namespaceMap);
function getNamespace(name: string) {
if (name in namespaceMap) {
const nestedAdded = addEmptyNested(namespaceMap[name]);
namespaceMap[name] = nestedAdded;
return nestedAdded;
}
throw new Error(`Couldn't find a namespace named ${name}.`);
}
}
function addEmptyNested(namespace: Browser.Interface): Browser.Interface {
if (namespace.nested) {
return namespace;
};
return {
...namespace,
nested: {
interfaces: [],
enums: [],
dictionaries: [],
typedefs: [],
}
};
}