8000 Update tooling for new IDL and MDN by MattiasBuelens · Pull Request #920 · microsoft/TypeScript-DOM-lib-generator · GitHub
[go: up one dir, main page]

Skip to content

Update tooling for new IDL and MDN #920

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions src/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,14 +289,25 @@ 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<undefined>") {
return "Promise<void>";
}
return type;
}

function convertDomTypeToTsTypeWorker(obj: Browser.Typed): { name: string; nullable: boolean } {
let type;
if (typeof obj.type === "string") {
type = { name: convertDomTypeToTsTypeSimple(obj.type), nullable: !!obj.nullable };
}
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",
Expand All @@ -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
};
}
}
Expand Down Expand Up @@ -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("");
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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};`);
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>([
...[...bufferSourceTypes].map(type => [type, type] as [string, string]),
...[...integerTypes].map(type => [type, "number"] as [string, string]),
Expand Down
3 changes: 3 additions & 0 deletions src/idlfetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand Down
9 changes: 8 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
0