Skip to content

Commit

Permalink
Track typedefs and generics when resolving promises (#1102)
Browse files Browse the repository at this point in the history
* Track typedefs and generics when resolving promises

* dedupe typedefs map
  • Loading branch information
saschanaz authored Aug 14, 2021
1 parent 6d459ad commit 302ec59
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
2 changes: 1 addition & 1 deletion baselines/dom.generated.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3456,7 +3456,7 @@ interface ClipboardItem {

declare var ClipboardItem: {
prototype: ClipboardItem;
new(items: Record<string, ClipboardItemData>, options?: ClipboardItemOptions): ClipboardItem;
new(items: Record<string, ClipboardItemDataType | PromiseLike<ClipboardItemDataType>>, options?: ClipboardItemOptions): ClipboardItem;
};

/** A CloseEvent is sent to clients using WebSockets when the connection is closed. This is delivered to the listener indicated by the WebSocket object's onclose attribute. */
Expand Down
21 changes: 16 additions & 5 deletions src/build/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export function emitWebIdl(
const allEnumsMap = webidl.enums ? webidl.enums.enum : {};
const allCallbackFunctionsMap =
webidl.callbackFunctions?.callbackFunction ?? {};
const allTypeDefsMap = new Set(webidl.typedefs?.typedef.map((td) => td.name));
const allTypedefsMap = toNameMap(webidl.typedefs?.typedef ?? []);

/// Tag name to element name map
const tagNameToEleName = getTagNameToElementNameMap();
Expand Down Expand Up @@ -415,7 +415,7 @@ export function emitWebIdl(
)
return objDomType;
// Name of a type alias. Just return itself
if (allTypeDefsMap.has(objDomType)) return objDomType;
if (allTypedefsMap[objDomType]) return objDomType;

throw new Error("Unknown DOM type: " + objDomType);
}
Expand Down Expand Up @@ -648,11 +648,22 @@ export function emitWebIdl(
}

function resolvePromise<T extends Browser.Typed>(t: T): T {
if (t.type !== "Promise") {
const typedef =
typeof t.type === "string" ? allTypedefsMap[t.type] : undefined;
const typeOwner = typedef ?? t;
if (typeOwner.type !== "Promise") {
if (t.subtype) {
return {
...t,
subtype: Array.isArray(t.subtype)
? t.subtype.map(resolvePromise)
: resolvePromise(t.subtype),
};
}
return t;
}
const type = [t.subtype!].flat();
type.push({ ...t, type: "PromiseLike" });
const type = [typeOwner.subtype!].flat();
type.push({ ...typeOwner, type: "PromiseLike" });
return { ...t, subtype: undefined, type };
}

Expand Down

0 comments on commit 302ec59

Please sign in to comment.