diff --git a/src/emitter.ts b/src/emitter.ts index df6514fbb..66a97ba2d 100644 --- a/src/emitter.ts +++ b/src/emitter.ts @@ -289,6 +289,17 @@ export function emitWebIdl(webidl: Browser.WebIdl, flavor: Flavor, iterator: boo return type.nullable ? makeNullable(type.name) : type.name; } + function convertDomTypeToTsReturnType(obj: Browser.Typed): string { + const type = convertDomTypeToTsType(obj); + if (type === "undefined") { + return "void"; + } + if (type === "Promise") { + return "Promise"; + } + return type; + } + function convertDomTypeToTsTypeWorker(obj: Browser.Typed): { name: string; nullable: boolean } { let type; if (typeof obj.type === "string") { @@ -296,7 +307,7 @@ export function emitWebIdl(webidl: Browser.WebIdl, flavor: Flavor, iterator: boo } else { const types = obj.type.map(convertDomTypeToTsTypeWorker); - const isAny = types.find(t => t.name === "any"); + const isAny = types.some(t => t.name === "any"); if (isAny) { type = { name: "any", @@ -306,7 +317,7 @@ export function emitWebIdl(webidl: Browser.WebIdl, flavor: Flavor, iterator: boo else { type = { name: types.map(t => t.name).join(" | "), - nullable: !!types.find(t => t.nullable) || !!obj.nullable + nullable: types.some(t => t.nullable) || !!obj.nullable }; } } @@ -542,7 +553,7 @@ export function emitWebIdl(webidl: Browser.WebIdl, flavor: Flavor, iterator: boo const m = methods[0]; const overload = m.signature[0]; const paramsString = overload.param ? paramsToString(overload.param) : ""; - const returnType = overload.type ? convertDomTypeToTsType(overload) : "void"; + const returnType = overload.type ? convertDomTypeToTsReturnType(overload) : "void"; printer.printLine(`type ${i.name} = ((${paramsString}) => ${returnType}) | { ${m.name}(${paramsString}): ${returnType}; };`); } printer.printLine(""); @@ -590,9 +601,9 @@ export function emitWebIdl(webidl: Browser.WebIdl, flavor: Flavor, iterator: boo function isCovariantEventHandler(i: Browser.Interface, p: Browser.Property) { return isEventHandler(p) && iNameToEhParents[i.name] && iNameToEhParents[i.name].length > 0 && - !!iNameToEhParents[i.name].find( + iNameToEhParents[i.name].some( i => iNameToEhList[i.name] && iNameToEhList[i.name].length > 0 && - !!iNameToEhList[i.name].find(e => e.name === p.name)); + iNameToEhList[i.name].some(e => e.name === p.name)); } 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 function emitSignature(s: Browser.Signature, prefix: string | undefined, name: string | undefined, printLine: (s: string) => void) { const paramsString = s.param ? paramsToString(s.param) : ""; - let returnType = convertDomTypeToTsType(s); + let returnType = convertDomTypeToTsReturnType(s); returnType = s.nullable ? makeNullable(returnType) : returnType; emitComments(s, printLine); printLine(`${prefix || ""}${name || ""}(${paramsString}): ${returnType};`); @@ -990,8 +1001,8 @@ export function emitWebIdl(webidl: Browser.WebIdl, flavor: Flavor, iterator: boo // Some types are static types with non-static members. For example, // NodeFilter is a static method itself, however it has an "acceptNode" method // that expects the user to implement. - const hasNonStaticMethod = i.methods && !!mapToArray(i.methods.method).find(m => !m.static); - const hasProperty = i.properties && mapToArray(i.properties.property).find(p => !p.static); + const hasNonStaticMethod = i.methods && mapToArray(i.methods.method).some(m => !m.static); + const hasProperty = i.properties && mapToArray(i.properties.property).some(p => !p.static); const hasNonStaticMember = hasNonStaticMethod || hasProperty; // For static types with non-static members, we put the non-static members into an diff --git a/src/helpers.ts b/src/helpers.ts index 568ffbf4a..e12f7a300 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -5,7 +5,7 @@ export const bufferSourceTypes = new Set(["ArrayBuffer", "ArrayBufferView", "Dat export const integerTypes = new Set(["byte", "octet", "short", "unsigned short", "long", "unsigned long", "long long", "unsigned long long"]); export const stringTypes = new Set(["ByteString", "DOMString", "USVString", "CSSOMString"]); const floatTypes = new Set(["float", "unrestricted float", "double", "unrestricted double"]); -const sameTypes = new Set(["any", "boolean", "Date", "Function", "Promise", "void"]); +const sameTypes = new Set(["any", "boolean", "Date", "Function", "Promise", "undefined", "void"]); export const baseTypeConversionMap = new Map([ ...[...bufferSourceTypes].map(type => [type, type] as [string, string]), ...[...integerTypes].map(type => [type, "number"] as [string, string]), diff --git a/src/idlfetcher.ts b/src/idlfetcher.ts index d956b125d..05ea14340 100644 --- a/src/idlfetcher.ts +++ b/src/idlfetcher.ts @@ -71,6 +71,9 @@ function extractIDL(dom: DocumentFragment) { } return !previous.classList.contains("atrisk") && !previous.textContent!.includes("IDL Index"); }); + elements.forEach(el => { + el.querySelector("span.idlHeader")?.remove(); + }); return elements.map(element => trimCommonIndentation(element.textContent!).trim()).join('\n\n'); } diff --git a/src/index.ts b/src/index.ts index 6af231e5c..e43960268 100644 --- a/src/index.ts +++ b/src/index.ts @@ -98,7 +98,14 @@ function emitDom() { for (const [key, value] of Object.entries(descriptions)) { const target = idl.interfaces!.interface[key] || namespaces[key]; if (target) { - target.comment = transformVerbosity(key, value); + if (value.startsWith("REDIRECT")) { + // When an MDN article for an interface redirects to a different one, + // it implies the interface was renamed in the specification and + // its old name should be deprecated. + markAsDeprecated(target); + } else { + target.comment = transformVerbosity(key, value); + } } } return idl;