Skip to content

Commit

Permalink
Support [PutForwards] for TS>5.5 (#1838)
Browse files Browse the repository at this point in the history
Co-authored-by: saschanaz <saschanaz@users.noreply.github.com>
  • Loading branch information
saschanaz and saschanaz authored Nov 2, 2024
1 parent 575c5d7 commit 11652aa
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 42 deletions.
61 changes: 40 additions & 21 deletions baselines/dom.generated.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3634,7 +3634,8 @@ declare var CSSCounterStyleRule: {
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/CSSFontFaceRule) */
interface CSSFontFaceRule extends CSSRule {
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/CSSFontFaceRule/style) */
readonly style: CSSStyleDeclaration;
get style(): CSSStyleDeclaration;
set style(cssText: string);
}

declare var CSSFontFaceRule: {
Expand Down Expand Up @@ -3705,7 +3706,8 @@ interface CSSImportRule extends CSSRule {
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/CSSImportRule/layerName) */
readonly layerName: string | null;
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/CSSImportRule/media) */
readonly media: MediaList;
get media(): MediaList;
set media(mediaText: string);
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/CSSImportRule/styleSheet) */
readonly styleSheet: CSSStyleSheet | null;
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/CSSImportRule/supportsText) */
Expand All @@ -3726,7 +3728,8 @@ interface CSSKeyframeRule extends CSSRule {
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/CSSKeyframeRule/keyText) */
keyText: string;
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/CSSKeyframeRule/style) */
readonly style: CSSStyleDeclaration;
get style(): CSSStyleDeclaration;
set style(cssText: string);
}

declare var CSSKeyframeRule: {
Expand Down Expand Up @@ -3899,7 +3902,8 @@ declare var CSSMatrixComponent: {
*/
interface CSSMediaRule extends CSSConditionRule {
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/CSSMediaRule/media) */
readonly media: MediaList;
get media(): MediaList;
set media(mediaText: string);
}

declare var CSSMediaRule: {
Expand All @@ -3925,7 +3929,8 @@ declare var CSSNamespaceRule: {
};

interface CSSNestedDeclarations extends CSSRule {
readonly style: CSSStyleDeclaration;
get style(): CSSStyleDeclaration;
set style(cssText: string);
}

declare var CSSNestedDeclarations: {
Expand Down Expand Up @@ -3986,7 +3991,8 @@ interface CSSPageRule extends CSSGroupingRule {
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/CSSPageRule/selectorText) */
selectorText: string;
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/CSSPageRule/style) */
readonly style: CSSStyleDeclaration;
get style(): CSSStyleDeclaration;
set style(cssText: string);
}

declare var CSSPageRule: {
Expand Down Expand Up @@ -5480,7 +5486,8 @@ interface CSSStyleRule extends CSSGroupingRule {
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/CSSStyleRule/selectorText) */
selectorText: string;
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/CSSStyleRule/style) */
readonly style: CSSStyleDeclaration;
get style(): CSSStyleDeclaration;
set style(cssText: string);
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/CSSStyleRule/styleMap) */
readonly styleMap: StylePropertyMap;
}
Expand Down Expand Up @@ -7438,7 +7445,7 @@ interface Document extends Node, DocumentOrShadowRoot, FontFaceSource, GlobalEve
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Document/location)
*/
get location(): Location;
set location(href: string | Location);
set location(href: string);
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Document/fullscreenchange_event) */
onfullscreenchange: ((this: Document, ev: Event) => any) | null;
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Document/fullscreenerror_event) */
Expand Down Expand Up @@ -8090,7 +8097,8 @@ interface Element extends Node, ARIAMixin, Animatable, ChildNode, NonDocumentTyp
*
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Element/classList)
*/
readonly classList: DOMTokenList;
get classList(): DOMTokenList;
set classList(value: string);
/**
* Returns the value of element's class content attribute. Can be set to change it.
*
Expand Down Expand Up @@ -8135,7 +8143,8 @@ interface Element extends Node, ARIAMixin, Animatable, ChildNode, NonDocumentTyp
outerHTML: string;
readonly ownerDocument: Document;
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Element/part) */
readonly part: DOMTokenList;
get part(): DOMTokenList;
set part(value: string);
/**
* Returns the namespace prefix.
*
Expand Down Expand Up @@ -8351,7 +8360,8 @@ interface ElementCSSInlineStyle {
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/HTMLElement/attributeStyleMap) */
readonly attributeStyleMap: StylePropertyMap;
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/HTMLElement/style) */
readonly style: CSSStyleDeclaration;
get style(): CSSStyleDeclaration;
set style(cssText: string);
}

interface ElementContentEditable {
Expand Down Expand Up @@ -9975,7 +9985,8 @@ interface HTMLAnchorElement extends HTMLElement, HTMLHyperlinkElementUtils {
*/
rel: string;
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/HTMLAnchorElement/relList) */
readonly relList: DOMTokenList;
get relList(): DOMTokenList;
set relList(value: string);
/**
* Sets or retrieves the relationship between the object and the destination of the link.
* @deprecated
Expand Down Expand Up @@ -10034,7 +10045,8 @@ interface HTMLAreaElement extends HTMLElement, HTMLHyperlinkElementUtils {
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/HTMLAreaElement/rel) */
rel: string;
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/HTMLAreaElement/relList) */
readonly relList: DOMTokenList;
get relList(): DOMTokenList;
set relList(value: string);
/** Sets or retrieves the shape of the object. */
shape: string;
/**
Expand Down Expand Up @@ -10830,7 +10842,8 @@ interface HTMLFormElement extends HTMLElement {
*/
noValidate: boolean;
rel: string;
readonly relList: DOMTokenList;
get relList(): DOMTokenList;
set relList(value: string);
/**
* Sets or retrieves the window or frame at which to target content.
*
Expand Down Expand Up @@ -11221,7 +11234,8 @@ interface HTMLIFrameElement extends HTMLElement {
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/HTMLIFrameElement/referrerPolicy) */
referrerPolicy: ReferrerPolicy;
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/HTMLIFrameElement/sandbox) */
readonly sandbox: DOMTokenList;
get sandbox(): DOMTokenList;
set sandbox(value: string);
/**
* Sets or retrieves whether the frame can be scrolled.
* @deprecated
Expand Down Expand Up @@ -11840,13 +11854,15 @@ interface HTMLLinkElement extends HTMLElement, LinkStyle {
*/
rel: string;
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/HTMLLinkElement/relList) */
readonly relList: DOMTokenList;
get relList(): DOMTokenList;
set relList(value: string);
/**
* Sets or retrieves the relationship between the object and the destination of the link.
* @deprecated
*/
rev: string;
readonly sizes: DOMTokenList;
get sizes(): DOMTokenList;
set sizes(value: string);
/**
* Sets or retrieves the window or frame at which to target content.
* @deprecated
Expand Down Expand Up @@ -12604,7 +12620,8 @@ interface HTMLOutputElement extends HTMLElement {
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/HTMLOutputElement/form) */
readonly form: HTMLFormElement | null;
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/HTMLOutputElement/htmlFor) */
readonly htmlFor: DOMTokenList;
get htmlFor(): DOMTokenList;
set htmlFor(value: string);
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/HTMLOutputElement/labels) */
readonly labels: NodeListOf<HTMLLabelElement>;
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/HTMLOutputElement/name) */
Expand Down Expand Up @@ -19589,7 +19606,8 @@ declare var Response: {
*/
interface SVGAElement extends SVGGraphicsElement, SVGURIReference {
rel: string;
readonly relList: DOMTokenList;
get relList(): DOMTokenList;
set relList(value: string);
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/SVGAElement/target) */
readonly target: SVGAnimatedString;
addEventListener<K extends keyof SVGElementEventMap>(type: K, listener: (this: SVGAElement, ev: SVGElementEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void;
Expand Down Expand Up @@ -22553,7 +22571,8 @@ interface StyleSheet {
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/StyleSheet/href) */
readonly href: string | null;
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/StyleSheet/media) */
readonly media: MediaList;
get media(): MediaList;
set media(mediaText: string);
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/StyleSheet/ownerNode) */
readonly ownerNode: Element | ProcessingInstruction | null;
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/StyleSheet/parentStyleSheet) */
Expand Down Expand Up @@ -26474,7 +26493,7 @@ interface Window extends EventTarget, AnimationFrameProvider, GlobalEventHandler
readonly length: number;
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Window/location) */
get location(): Location;
set location(href: string | Location);
set location(href: string);
/**
* Returns true if the location bar is visible; otherwise, returns false.
*
Expand Down
4 changes: 4 additions & 0 deletions inputfiles/overridingTypes.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,7 @@
"deprecated": true
},
"location": {
// Pre-TS-5.1 hack to make window.location assignable
"readonly": false
},
"onorientationchange": {
Expand Down Expand Up @@ -810,7 +811,10 @@
"overrideType": "HTMLCollectionOf<HTMLScriptElement>"
},
"location": {
// Pre-TS-5.1 hack to make document.location assignable
"readonly": false,
// Technically this can be null for a detached iframe.
// But that's an edge case and flipping this also breaks compatibility.
"nullable": false
},
"body": {
Expand Down
32 changes: 19 additions & 13 deletions src/build.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as Browser from "./build/types.js";
import { promises as fs } from "fs";
import { merge, resolveExposure, arrayToMap } from "./build/helpers.js";
import { emitWebIdl } from "./build/emitter.js";
import { type CompilerBehavior, emitWebIdl } from "./build/emitter.js";
import { convert } from "./build/widlprocess.js";
import { getExposedTypes } from "./build/expose.js";
import {
Expand Down Expand Up @@ -37,7 +37,7 @@ interface EmitOptions {
global: string[];
name: string;
outputFolder: URL;
useIteratorObject: boolean;
compilerBehavior: CompilerBehavior;
}

async function emitFlavor(
Expand All @@ -53,7 +53,7 @@ async function emitFlavor(
exposed,
options.global[0],
"",
options.useIteratorObject,
options.compilerBehavior,
);
await fs.writeFile(
new URL(`${options.name}.generated.d.ts`, options.outputFolder),
Expand All @@ -64,7 +64,7 @@ async function emitFlavor(
exposed,
options.global[0],
"sync",
options.useIteratorObject,
options.compilerBehavior,
);
await fs.writeFile(
new URL(`${options.name}.iterable.generated.d.ts`, options.outputFolder),
Expand All @@ -75,7 +75,7 @@ async function emitFlavor(
exposed,
options.global[0],
"async",
options.useIteratorObject,
options.compilerBehavior,
);
await fs.writeFile(
new URL(
Expand Down Expand Up @@ -294,12 +294,18 @@ async function emitDom() {
const emitVariations = [
{
outputFolder: new URL("./ts5.5/", outputFolder),
useIteratorObject: false,
compilerBehavior: {},
},
{
outputFolder,
compilerBehavior: {
useIteratorObject: true,
allowUnrelatedSetterType: true,
} as CompilerBehavior,
},
{ outputFolder, useIteratorObject: true },
];

for (const { outputFolder, useIteratorObject } of emitVariations) {
for (const { outputFolder, compilerBehavior } of emitVariations) {
// Create output folder
await fs.mkdir(outputFolder, {
// Doesn't need to be recursive, but this helpfully ignores EEXIST
Expand All @@ -310,31 +316,31 @@ async function emitDom() {
name: "dom",
global: ["Window"],
outputFolder,
useIteratorObject,
compilerBehavior,
});
emitFlavor(webidl, new Set(knownTypes.Worker), {
name: "webworker",
global: ["Worker", "DedicatedWorker", "SharedWorker", "ServiceWorker"],
outputFolder,
useIteratorObject,
compilerBehavior,
});
emitFlavor(webidl, new Set(knownTypes.Worker), {
name: "sharedworker",
global: ["SharedWorker", "Worker"],
outputFolder,
useIteratorObject,
compilerBehavior,
});
emitFlavor(webidl, new Set(knownTypes.Worker), {
name: "serviceworker",
global: ["ServiceWorker", "Worker"],
outputFolder,
useIteratorObject,
compilerBehavior,
});
emitFlavor(webidl, new Set(knownTypes.Worklet), {
name: "audioworklet",
global: ["AudioWorklet", "Worklet"],
outputFolder,
useIteratorObject,
compilerBehavior,
});
}

Expand Down
24 changes: 16 additions & 8 deletions src/build/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,16 @@ function isEventHandler(p: Browser.Property) {
return typeof p.eventHandler === "string";
}

export interface CompilerBehavior {
useIteratorObject?: boolean;
allowUnrelatedSetterType?: boolean;
}

export function emitWebIdl(
webidl: Browser.WebIdl,
global: string,
iterator: "" | "sync" | "async",
useIteratorObject: boolean,
compilerBehavior: CompilerBehavior,
): string {
// Global print target
const printer = createTextWriter("\n");
Expand Down Expand Up @@ -821,17 +826,20 @@ export function emitWebIdl(
pType += " | undefined";
}
const optionalModifier = !p.optional || prefix ? "" : "?";
if (!prefix && !p.readonly && p.putForwards) {
const canPutForward =
compilerBehavior.allowUnrelatedSetterType || !p.readonly;
if (!prefix && canPutForward && p.putForwards) {
printer.printLine(`get ${p.name}${optionalModifier}(): ${pType};`);

const forwardingProperty =
allInterfacesMap[pType].properties?.property[p.putForwards];
if (!forwardingProperty) {
throw new Error("Couldn't find [PutForwards]");
}
const setterType = `${convertDomTypeToTsType(
forwardingProperty,
)} | ${pType}`;
let setterType = `${convertDomTypeToTsType(forwardingProperty)}`;
if (!compilerBehavior.allowUnrelatedSetterType) {
setterType += ` | ${pType}`;
}
printer.printLine(
`set ${p.name}${optionalModifier}(${p.putForwards}: ${setterType});`,
);
Expand Down Expand Up @@ -1541,7 +1549,7 @@ export function emitWebIdl(
}

function emitSelfIterator(i: Browser.Interface) {
if (!useIteratorObject) return;
if (!compilerBehavior.useIteratorObject) return;
const async = i.iterator?.async;
const name = getName(i);
const iteratorBaseType = `${async ? "Async" : ""}IteratorObject`;
Expand Down Expand Up @@ -1569,10 +1577,10 @@ export function emitWebIdl(
}
const async = i.iterator?.async;
const iteratorType = async
? !useIteratorObject
? !compilerBehavior.useIteratorObject
? "AsyncIterableIterator"
: `${name}AsyncIterator`
: !useIteratorObject
: !compilerBehavior.useIteratorObject
? "IterableIterator"
: subtypes.length !== 1
? `${name}Iterator`
Expand Down

0 comments on commit 11652aa

Please sign in to comment.