forked from microsoft/TypeScript-DOM-lib-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
139 lines (121 loc) · 5.72 KB
/
Copy pathindex.ts
File metadata and controls
139 lines (121 loc) · 5.72 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import * as Browser from "./types";
import * as fs from "fs";
import * as path from "path";
import { merge, resolveExposure, markAsDeprecated, mapToArray } from "./helpers";
import { Flavor, emitWebIDl } from "./emitter";
import { convert } from "./widlprocess";
import { getExposedTypes } from "./expose";
function emitDomWorker(webidl: Browser.WebIdl, tsWorkerOutput: string, forceKnownWorkerTypes: Set<string>) {
const worker = getExposedTypes(webidl, "Worker", forceKnownWorkerTypes);
const result = emitWebIDl(worker, Flavor.Worker);
fs.writeFileSync(tsWorkerOutput, result);
return;
}
function emitDomWeb(webidl: Browser.WebIdl, tsWebOutput: string, forceKnownWindowTypes: Set<string>) {
const browser = getExposedTypes(webidl, "Window", forceKnownWindowTypes);
const result = emitWebIDl(browser, Flavor.Web);
fs.writeFileSync(tsWebOutput, result);
return;
}
function emitES6DomIterators(webidl: Browser.WebIdl, tsWebIteratorsOutput: string) {
fs.writeFileSync(tsWebIteratorsOutput, emitWebIDl(webidl, Flavor.ES6Iterators));
}
function emitDom() {
const __SOURCE_DIRECTORY__ = __dirname;
const inputFolder = path.join(__SOURCE_DIRECTORY__, "../", "inputfiles");
const outputFolder = path.join(__SOURCE_DIRECTORY__, "../", "generated");
// Create output folder
if (!fs.existsSync(outputFolder)) {
fs.mkdirSync(outputFolder);
}
const tsWebOutput = path.join(outputFolder, "dom.generated.d.ts");
const tsWebIteratorsOutput = path.join(outputFolder, "dom.iterable.generated.d.ts");
const tsWorkerOutput = path.join(outputFolder, "webworker.generated.d.ts");
const overriddenItems = require(path.join(inputFolder, "overridingTypes.json"));
const addedItems = require(path.join(inputFolder, "addedTypes.json"));
const comments = require(path.join(inputFolder, "comments.json"));
const removedItems = require(path.join(inputFolder, "removedTypes.json"));
const idlSources = require(path.join(inputFolder, "idlSources.json"));
const widlStandardTypes = idlSources.map(convertWidl);
function convertWidl({ title, deprecated }: { title: string; deprecated?: boolean }) {
const filename = title + ".widl";
const idl: string = fs.readFileSync(path.join(inputFolder, "idl", filename), { encoding: "utf-8" });
const commentsMapFilePath = path.join(inputFolder, "idl", title + ".commentmap.json");
const commentsMap: Record<string, string> = fs.existsSync(commentsMapFilePath) ? require(commentsMapFilePath) : {};
const result = convert(idl, commentsMap);
if (deprecated) {
mapToArray(result.browser.interfaces!.interface).forEach(markAsDeprecated);
result.partialInterfaces.forEach(markAsDeprecated);
}
return result;
}
/// Load the input file
let webidl: Browser.WebIdl = require(path.join(inputFolder, "browser.webidl.preprocessed.json"));
const knownTypes = require(path.join(inputFolder, "knownTypes.json"));
for (const w of widlStandardTypes) {
webidl = merge(webidl, w.browser, true);
}
for (const w of widlStandardTypes) {
for (const partial of w.partialInterfaces) {
// Fallback to mixins before every spec migrates to `partial interface mixin`.
const base = webidl.interfaces!.interface[partial.name] || webidl.mixins!.mixin[partial.name];
if (base) {
if (base.exposed) resolveExposure(partial, base.exposed);
merge(base.constants, partial.constants, true);
merge(base.methods, partial.methods, true);
merge(base.properties, partial.properties, true);
}
}
for (const partial of w.partialDictionaries) {
const base = webidl.dictionaries!.dictionary[partial.name];
if (base) {
merge(base.members, partial.members, true);
}
}
for (const include of w.includes) {
const target = webidl.interfaces!.interface[include.target];
if (target) {
if (target.implements) {
target.implements.push(include.includes);
}
else {
target.implements = [include.includes];
}
}
}
}
webidl = prune(webidl, removedItems);
webidl = merge(webidl, addedItems);
webidl = merge(webidl, overriddenItems);
webidl = merge(webidl, comments);
for (const name in webidl.interfaces!.interface) {
const i = webidl.interfaces!.interface[name];
if (i["override-exposed"]) {
resolveExposure(i, i["override-exposed"]!, true);
}
}
emitDomWeb(webidl, tsWebOutput, new Set(knownTypes.Window));
emitDomWorker(webidl, tsWorkerOutput, new Set(knownTypes.Worker));
emitES6DomIterators(webidl, tsWebIteratorsOutput);
function prune(obj: Browser.WebIdl, template: Partial<Browser.WebIdl>): Browser.WebIdl {
const result = filterByNull(obj, template);
if (obj.typedefs) result.typedefs!.typedef = obj.typedefs.typedef.filter(t => !(template.typedefs && template.typedefs.typedef.find(o => o["new-type"] === t["new-type"])));
return result;
function filterByNull(obj: any, template: any) {
if (!template) return obj;
const filtered: any = {};
for (const k in obj) {
if (template.hasOwnProperty(k) && !Array.isArray(template[k])) {
if (template[k] !== null) {
filtered[k] = filterByNull(obj[k], template[k]);
}
}
else {
filtered[k] = obj[k];
}
}
return filtered;
}
}
}
emitDom();