Skip to content

Commit cf24210

Browse files
author
Orta Therox
authored
Merge pull request #920 from MattiasBuelens/update-tooling
Update tooling for new IDL and MDN
2 parents 76e9c67 + 255b759 commit cf24210

File tree

4 files changed

+31
-10
lines changed

4 files changed

+31
-10
lines changed

src/emitter.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -289,14 +289,25 @@ export function emitWebIdl(webidl: Browser.WebIdl, flavor: Flavor, iterator: boo
289289
return type.nullable ? makeNullable(type.name) : type.name;
290290
}
291291

292+
function convertDomTypeToTsReturnType(obj: Browser.Typed): string {
293+
const type = convertDomTypeToTsType(obj);
294+
if (type === "undefined") {
295+
return "void";
296+
}
297+
if (type === "Promise<undefined>") {
298+
return "Promise<void>";
299+
}
300+
return type;
301+
}
302+
292303
function convertDomTypeToTsTypeWorker(obj: Browser.Typed): { name: string; nullable: boolean } {
293304
let type;
294305
if (typeof obj.type === "string") {
295306
type = { name: convertDomTypeToTsTypeSimple(obj.type), nullable: !!obj.nullable };
296307
}
297308
else {
298309
const types = obj.type.map(convertDomTypeToTsTypeWorker);
299-
const isAny = types.find(t => t.name === "any");
310+
const isAny = types.some(t => t.name === "any");
300311
if (isAny) {
301312
type = {
302313
name: "any",
@@ -306,7 +317,7 @@ export function emitWebIdl(webidl: Browser.WebIdl, flavor: Flavor, iterator: boo
306317
else {
307318
type = {
308319
name: types.map(t => t.name).join(" | "),
309-
nullable: !!types.find(t => t.nullable) || !!obj.nullable
320+
nullable: types.some(t => t.nullable) || !!obj.nullable
310321
};
311322
}
312323
}
@@ -542,7 +553,7 @@ export function emitWebIdl(webidl: Browser.WebIdl, flavor: Flavor, iterator: boo
542553
const m = methods[0];
543554
const overload = m.signature[0];
544555
const paramsString = overload.param ? paramsToString(overload.param) : "";
545-
const returnType = overload.type ? convertDomTypeToTsType(overload) : "void";
556+
const returnType = overload.type ? convertDomTypeToTsReturnType(overload) : "void";
546557
printer.printLine(`type ${i.name} = ((${paramsString}) => ${returnType}) | { ${m.name}(${paramsString}): ${returnType}; };`);
547558
}
548559
printer.printLine("");
@@ -590,9 +601,9 @@ export function emitWebIdl(webidl: Browser.WebIdl, flavor: Flavor, iterator: boo
590601
function isCovariantEventHandler(i: Browser.Interface, p: Browser.Property) {
591602
return isEventHandler(p) &&
592603
iNameToEhParents[i.name] && iNameToEhParents[i.name].length > 0 &&
593-
!!iNameToEhParents[i.name].find(
604+
iNameToEhParents[i.name].some(
594605
i => iNameToEhList[i.name] && iNameToEhList[i.name].length > 0 &&
595-
!!iNameToEhList[i.name].find(e => e.name === p.name));
606+
iNameToEhList[i.name].some(e => e.name === p.name));
596607
}
597608

598609
function emitProperty(prefix: string, i: Browser.Interface, emitScope: EmitScope, p: Browser.Property) {
@@ -690,7 +701,7 @@ export function emitWebIdl(webidl: Browser.WebIdl, flavor: Flavor, iterator: boo
690701

691702
function emitSignature(s: Browser.Signature, prefix: string | undefined, name: string | undefined, printLine: (s: string) => void) {
692703
const paramsString = s.param ? paramsToString(s.param) : "";
693-
let returnType = convertDomTypeToTsType(s);
704+
let returnType = convertDomTypeToTsReturnType(s);
694705
returnType = s.nullable ? makeNullable(returnType) : returnType;
695706
emitComments(s, printLine);
696707
printLine(`${prefix || ""}${name || ""}(${paramsString}): ${returnType};`);
@@ -990,8 +1001,8 @@ export function emitWebIdl(webidl: Browser.WebIdl, flavor: Flavor, iterator: boo
9901001
// Some types are static types with non-static members. For example,
9911002
// NodeFilter is a static method itself, however it has an "acceptNode" method
9921003
// that expects the user to implement.
993-
const hasNonStaticMethod = i.methods && !!mapToArray(i.methods.method).find(m => !m.static);
994-
const hasProperty = i.properties && mapToArray(i.properties.property).find(p => !p.static);
1004+
const hasNonStaticMethod = i.methods && mapToArray(i.methods.method).some(m => !m.static);
1005+
const hasProperty = i.properties && mapToArray(i.properties.property).some(p => !p.static);
9951006
const hasNonStaticMember = hasNonStaticMethod || hasProperty;
9961007

9971008
// For static types with non-static members, we put the non-static members into an

src/helpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export const bufferSourceTypes = new Set(["ArrayBuffer", "ArrayBufferView", "Dat
55
export const integerTypes = new Set(["byte", "octet", "short", "unsigned short", "long", "unsigned long", "long long", "unsigned long long"]);
66
export const stringTypes = new Set(["ByteString", "DOMString", "USVString", "CSSOMString"]);
77
const floatTypes = new Set(["float", "unrestricted float", "double", "unrestricted double"]);
8-
const sameTypes = new Set(["any", "boolean", "Date", "Function", "Promise", "void"]);
8+
const sameTypes = new Set(["any", "boolean", "Date", "Function", "Promise", "undefined", "void"]);
99
export const baseTypeConversionMap = new Map<string, string>([
1010
...[...bufferSourceTypes].map(type => [type, type] as [string, string]),
1111
...[...integerTypes].map(type => [type, "number"] as [string, string]),

src/idlfetcher.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ function extractIDL(dom: DocumentFragment) {
7171
}
7272
return !previous.classList.contains("atrisk") && !previous.textContent!.includes("IDL Index");
7373
});
74+
elements.forEach(el => {
75+
el.querySelector("span.idlHeader")?.remove();
76+
});
7477
return elements.map(element => trimCommonIndentation(element.textContent!).trim()).join('\n\n');
7578
}
7679

src/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,14 @@ function emitDom() {
9898
for (const [key, value] of Object.entries(descriptions)) {
9999
const target = idl.interfaces!.interface[key] || namespaces[key];
100100
if (target) {
101-
target.comment = transformVerbosity(key, value);
101+
if (value.startsWith("REDIRECT")) {
102+
// When an MDN article for an interface redirects to a different one,
103+
// it implies the interface was renamed in the specification and
104+
// its old name should be deprecated.
105+
markAsDeprecated(target);
106+
} else {
107+
target.comment = transformVerbosity(key, value);
108+
}
102109
}
103110
}
104111
return idl;

0 commit comments

Comments
 (0)