Skip to content

Commit 11652aa

Browse files
authored
Support [PutForwards] for TS>5.5 (microsoft#1838)
Co-authored-by: saschanaz <saschanaz@users.noreply.github.com>
1 parent 575c5d7 commit 11652aa

File tree

4 files changed

+79
-42
lines changed

4 files changed

+79
-42
lines changed

baselines/dom.generated.d.ts

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3634,7 +3634,8 @@ declare var CSSCounterStyleRule: {
36343634
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/CSSFontFaceRule) */
36353635
interface CSSFontFaceRule extends CSSRule {
36363636
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/CSSFontFaceRule/style) */
3637-
readonly style: CSSStyleDeclaration;
3637+
get style(): CSSStyleDeclaration;
3638+
set style(cssText: string);
36383639
}
36393640

36403641
declare var CSSFontFaceRule: {
@@ -3705,7 +3706,8 @@ interface CSSImportRule extends CSSRule {
37053706
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/CSSImportRule/layerName) */
37063707
readonly layerName: string | null;
37073708
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/CSSImportRule/media) */
3708-
readonly media: MediaList;
3709+
get media(): MediaList;
3710+
set media(mediaText: string);
37093711
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/CSSImportRule/styleSheet) */
37103712
readonly styleSheet: CSSStyleSheet | null;
37113713
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/CSSImportRule/supportsText) */
@@ -3726,7 +3728,8 @@ interface CSSKeyframeRule extends CSSRule {
37263728
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/CSSKeyframeRule/keyText) */
37273729
keyText: string;
37283730
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/CSSKeyframeRule/style) */
3729-
readonly style: CSSStyleDeclaration;
3731+
get style(): CSSStyleDeclaration;
3732+
set style(cssText: string);
37303733
}
37313734

37323735
declare var CSSKeyframeRule: {
@@ -3899,7 +3902,8 @@ declare var CSSMatrixComponent: {
38993902
*/
39003903
interface CSSMediaRule extends CSSConditionRule {
39013904
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/CSSMediaRule/media) */
3902-
readonly media: MediaList;
3905+
get media(): MediaList;
3906+
set media(mediaText: string);
39033907
}
39043908

39053909
declare var CSSMediaRule: {
@@ -3925,7 +3929,8 @@ declare var CSSNamespaceRule: {
39253929
};
39263930

39273931
interface CSSNestedDeclarations extends CSSRule {
3928-
readonly style: CSSStyleDeclaration;
3932+
get style(): CSSStyleDeclaration;
3933+
set style(cssText: string);
39293934
}
39303935

39313936
declare var CSSNestedDeclarations: {
@@ -3986,7 +3991,8 @@ interface CSSPageRule extends CSSGroupingRule {
39863991
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/CSSPageRule/selectorText) */
39873992
selectorText: string;
39883993
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/CSSPageRule/style) */
3989-
readonly style: CSSStyleDeclaration;
3994+
get style(): CSSStyleDeclaration;
3995+
set style(cssText: string);
39903996
}
39913997

39923998
declare var CSSPageRule: {
@@ -5480,7 +5486,8 @@ interface CSSStyleRule extends CSSGroupingRule {
54805486
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/CSSStyleRule/selectorText) */
54815487
selectorText: string;
54825488
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/CSSStyleRule/style) */
5483-
readonly style: CSSStyleDeclaration;
5489+
get style(): CSSStyleDeclaration;
5490+
set style(cssText: string);
54845491
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/CSSStyleRule/styleMap) */
54855492
readonly styleMap: StylePropertyMap;
54865493
}
@@ -7438,7 +7445,7 @@ interface Document extends Node, DocumentOrShadowRoot, FontFaceSource, GlobalEve
74387445
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Document/location)
74397446
*/
74407447
get location(): Location;
7441-
set location(href: string | Location);
7448+
set location(href: string);
74427449
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Document/fullscreenchange_event) */
74437450
onfullscreenchange: ((this: Document, ev: Event) => any) | null;
74447451
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Document/fullscreenerror_event) */
@@ -8090,7 +8097,8 @@ interface Element extends Node, ARIAMixin, Animatable, ChildNode, NonDocumentTyp
80908097
*
80918098
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Element/classList)
80928099
*/
8093-
readonly classList: DOMTokenList;
8100+
get classList(): DOMTokenList;
8101+
set classList(value: string);
80948102
/**
80958103
* Returns the value of element's class content attribute. Can be set to change it.
80968104
*
@@ -8135,7 +8143,8 @@ interface Element extends Node, ARIAMixin, Animatable, ChildNode, NonDocumentTyp
81358143
outerHTML: string;
81368144
readonly ownerDocument: Document;
81378145
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Element/part) */
8138-
readonly part: DOMTokenList;
8146+
get part(): DOMTokenList;
8147+
set part(value: string);
81398148
/**
81408149
* Returns the namespace prefix.
81418150
*
@@ -8351,7 +8360,8 @@ interface ElementCSSInlineStyle {
83518360
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/HTMLElement/attributeStyleMap) */
83528361
readonly attributeStyleMap: StylePropertyMap;
83538362
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/HTMLElement/style) */
8354-
readonly style: CSSStyleDeclaration;
8363+
get style(): CSSStyleDeclaration;
8364+
set style(cssText: string);
83558365
}
83568366

83578367
interface ElementContentEditable {
@@ -9975,7 +9985,8 @@ interface HTMLAnchorElement extends HTMLElement, HTMLHyperlinkElementUtils {
99759985
*/
99769986
rel: string;
99779987
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/HTMLAnchorElement/relList) */
9978-
readonly relList: DOMTokenList;
9988+
get relList(): DOMTokenList;
9989+
set relList(value: string);
99799990
/**
99809991
* Sets or retrieves the relationship between the object and the destination of the link.
99819992
* @deprecated
@@ -10034,7 +10045,8 @@ interface HTMLAreaElement extends HTMLElement, HTMLHyperlinkElementUtils {
1003410045
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/HTMLAreaElement/rel) */
1003510046
rel: string;
1003610047
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/HTMLAreaElement/relList) */
10037-
readonly relList: DOMTokenList;
10048+
get relList(): DOMTokenList;
10049+
set relList(value: string);
1003810050
/** Sets or retrieves the shape of the object. */
1003910051
shape: string;
1004010052
/**
@@ -10830,7 +10842,8 @@ interface HTMLFormElement extends HTMLElement {
1083010842
*/
1083110843
noValidate: boolean;
1083210844
rel: string;
10833-
readonly relList: DOMTokenList;
10845+
get relList(): DOMTokenList;
10846+
set relList(value: string);
1083410847
/**
1083510848
* Sets or retrieves the window or frame at which to target content.
1083610849
*
@@ -11221,7 +11234,8 @@ interface HTMLIFrameElement extends HTMLElement {
1122111234
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/HTMLIFrameElement/referrerPolicy) */
1122211235
referrerPolicy: ReferrerPolicy;
1122311236
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/HTMLIFrameElement/sandbox) */
11224-
readonly sandbox: DOMTokenList;
11237+
get sandbox(): DOMTokenList;
11238+
set sandbox(value: string);
1122511239
/**
1122611240
* Sets or retrieves whether the frame can be scrolled.
1122711241
* @deprecated
@@ -11840,13 +11854,15 @@ interface HTMLLinkElement extends HTMLElement, LinkStyle {
1184011854
*/
1184111855
rel: string;
1184211856
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/HTMLLinkElement/relList) */
11843-
readonly relList: DOMTokenList;
11857+
get relList(): DOMTokenList;
11858+
set relList(value: string);
1184411859
/**
1184511860
* Sets or retrieves the relationship between the object and the destination of the link.
1184611861
* @deprecated
1184711862
*/
1184811863
rev: string;
11849-
readonly sizes: DOMTokenList;
11864+
get sizes(): DOMTokenList;
11865+
set sizes(value: string);
1185011866
/**
1185111867
* Sets or retrieves the window or frame at which to target content.
1185211868
* @deprecated
@@ -12604,7 +12620,8 @@ interface HTMLOutputElement extends HTMLElement {
1260412620
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/HTMLOutputElement/form) */
1260512621
readonly form: HTMLFormElement | null;
1260612622
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/HTMLOutputElement/htmlFor) */
12607-
readonly htmlFor: DOMTokenList;
12623+
get htmlFor(): DOMTokenList;
12624+
set htmlFor(value: string);
1260812625
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/HTMLOutputElement/labels) */
1260912626
readonly labels: NodeListOf<HTMLLabelElement>;
1261012627
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/HTMLOutputElement/name) */
@@ -19589,7 +19606,8 @@ declare var Response: {
1958919606
*/
1959019607
interface SVGAElement extends SVGGraphicsElement, SVGURIReference {
1959119608
rel: string;
19592-
readonly relList: DOMTokenList;
19609+
get relList(): DOMTokenList;
19610+
set relList(value: string);
1959319611
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/SVGAElement/target) */
1959419612
readonly target: SVGAnimatedString;
1959519613
addEventListener<K extends keyof SVGElementEventMap>(type: K, listener: (this: SVGAElement, ev: SVGElementEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void;
@@ -22553,7 +22571,8 @@ interface StyleSheet {
2255322571
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/StyleSheet/href) */
2255422572
readonly href: string | null;
2255522573
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/StyleSheet/media) */
22556-
readonly media: MediaList;
22574+
get media(): MediaList;
22575+
set media(mediaText: string);
2255722576
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/StyleSheet/ownerNode) */
2255822577
readonly ownerNode: Element | ProcessingInstruction | null;
2255922578
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/StyleSheet/parentStyleSheet) */
@@ -26474,7 +26493,7 @@ interface Window extends EventTarget, AnimationFrameProvider, GlobalEventHandler
2647426493
readonly length: number;
2647526494
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Window/location) */
2647626495
get location(): Location;
26477-
set location(href: string | Location);
26496+
set location(href: string);
2647826497
/**
2647926498
* Returns true if the location bar is visible; otherwise, returns false.
2648026499
*

inputfiles/overridingTypes.jsonc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,7 @@
599599
"deprecated": true
600600
},
601601
"location": {
602+
// Pre-TS-5.1 hack to make window.location assignable
602603
"readonly": false
603604
},
604605
"onorientationchange": {
@@ -810,7 +811,10 @@
810811
"overrideType": "HTMLCollectionOf<HTMLScriptElement>"
811812
},
812813
"location": {
814+
// Pre-TS-5.1 hack to make document.location assignable
813815
"readonly": false,
816+
// Technically this can be null for a detached iframe.
817+
// But that's an edge case and flipping this also breaks compatibility.
814818
"nullable": false
815819
},
816820
"body": {

src/build.ts

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as Browser from "./build/types.js";
22
import { promises as fs } from "fs";
33
import { merge, resolveExposure, arrayToMap } from "./build/helpers.js";
4-
import { emitWebIdl } from "./build/emitter.js";
4+
import { type CompilerBehavior, emitWebIdl } from "./build/emitter.js";
55
import { convert } from "./build/widlprocess.js";
66
import { getExposedTypes } from "./build/expose.js";
77
import {
@@ -37,7 +37,7 @@ interface EmitOptions {
3737
global: string[];
3838
name: string;
3939
outputFolder: URL;
40-
useIteratorObject: boolean;
40+
compilerBehavior: CompilerBehavior;
4141
}
4242

4343
async function emitFlavor(
@@ -53,7 +53,7 @@ async function emitFlavor(
5353
exposed,
5454
options.global[0],
5555
"",
56-
options.useIteratorObject,
56+
options.compilerBehavior,
5757
);
5858
await fs.writeFile(
5959
new URL(`${options.name}.generated.d.ts`, options.outputFolder),
@@ -64,7 +64,7 @@ async function emitFlavor(
6464
exposed,
6565
options.global[0],
6666
"sync",
67-
options.useIteratorObject,
67+
options.compilerBehavior,
6868
);
6969
await fs.writeFile(
7070
new URL(`${options.name}.iterable.generated.d.ts`, options.outputFolder),
@@ -75,7 +75,7 @@ async function emitFlavor(
7575
exposed,
7676
options.global[0],
7777
"async",
78-
options.useIteratorObject,
78+
options.compilerBehavior,
7979
);
8080
await fs.writeFile(
8181
new URL(
@@ -294,12 +294,18 @@ async function emitDom() {
294294
const emitVariations = [
295295
{
296296
outputFolder: new URL("./ts5.5/", outputFolder),
297-
useIteratorObject: false,
297+
compilerBehavior: {},
298+
},
299+
{
300+
outputFolder,
301+
compilerBehavior: {
302+
useIteratorObject: true,
303+
allowUnrelatedSetterType: true,
304+
} as CompilerBehavior,
298305
},
299-
{ outputFolder, useIteratorObject: true },
300306
];
301307

302-
for (const { outputFolder, useIteratorObject } of emitVariations) {
308+
for (const { outputFolder, compilerBehavior } of emitVariations) {
303309
// Create output folder
304310
await fs.mkdir(outputFolder, {
305311
// Doesn't need to be recursive, but this helpfully ignores EEXIST
@@ -310,31 +316,31 @@ async function emitDom() {
310316
name: "dom",
311317
global: ["Window"],
312318
outputFolder,
313-
useIteratorObject,
319+
compilerBehavior,
314320
});
315321
emitFlavor(webidl, new Set(knownTypes.Worker), {
316322
name: "webworker",
317323
global: ["Worker", "DedicatedWorker", "SharedWorker", "ServiceWorker"],
318324
outputFolder,
319-
useIteratorObject,
325+
compilerBehavior,
320326
});
321327
emitFlavor(webidl, new Set(knownTypes.Worker), {
322328
name: "sharedworker",
323329
global: ["SharedWorker", "Worker"],
324330
outputFolder,
325-
useIteratorObject,
331+
compilerBehavior,
326332
});
327333
emitFlavor(webidl, new Set(knownTypes.Worker), {
328334
name: "serviceworker",
329335
global: ["ServiceWorker", "Worker"],
330336
outputFolder,
331-
useIteratorObject,
337+
compilerBehavior,
332338
});
333339
emitFlavor(webidl, new Set(knownTypes.Worklet), {
334340
name: "audioworklet",
335341
global: ["AudioWorklet", "Worklet"],
336342
outputFolder,
337-
useIteratorObject,
343+
compilerBehavior,
338344
});
339345
}
340346

src/build/emitter.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,16 @@ function isEventHandler(p: Browser.Property) {
132132
return typeof p.eventHandler === "string";
133133
}
134134

135+
export interface CompilerBehavior {
136+
useIteratorObject?: boolean;
137+
allowUnrelatedSetterType?: boolean;
138+
}
139+
135140
export function emitWebIdl(
136141
webidl: Browser.WebIdl,
137142
global: string,
138143
iterator: "" | "sync" | "async",
139-
useIteratorObject: boolean,
144+
compilerBehavior: CompilerBehavior,
140145
): string {
141146
// Global print target
142147
const printer = createTextWriter("\n");
@@ -821,17 +826,20 @@ export function emitWebIdl(
821826
pType += " | undefined";
822827
}
823828
const optionalModifier = !p.optional || prefix ? "" : "?";
824-
if (!prefix && !p.readonly && p.putForwards) {
829+
const canPutForward =
830+
compilerBehavior.allowUnrelatedSetterType || !p.readonly;
831+
if (!prefix && canPutForward && p.putForwards) {
825832
printer.printLine(`get ${p.name}${optionalModifier}(): ${pType};`);
826833

827834
const forwardingProperty =
828835
allInterfacesMap[pType].properties?.property[p.putForwards];
829836
if (!forwardingProperty) {
830837
throw new Error("Couldn't find [PutForwards]");
831838
}
832-
const setterType = `${convertDomTypeToTsType(
833-
forwardingProperty,
834-
)} | ${pType}`;
839+
let setterType = `${convertDomTypeToTsType(forwardingProperty)}`;
840+
if (!compilerBehavior.allowUnrelatedSetterType) {
841+
setterType += ` | ${pType}`;
842+
}
835843
printer.printLine(
836844
`set ${p.name}${optionalModifier}(${p.putForwards}: ${setterType});`,
837845
);
@@ -1541,7 +1549,7 @@ export function emitWebIdl(
15411549
}
15421550

15431551
function emitSelfIterator(i: Browser.Interface) {
1544-
if (!useIteratorObject) return;
1552+
if (!compilerBehavior.useIteratorObject) return;
15451553
const async = i.iterator?.async;
15461554
const name = getName(i);
15471555
const iteratorBaseType = `${async ? "Async" : ""}IteratorObject`;
@@ -1569,10 +1577,10 @@ export function emitWebIdl(
15691577
}
15701578
const async = i.iterator?.async;
15711579
const iteratorType = async
1572-
? !useIteratorObject
1580+
? !compilerBehavior.useIteratorObject
15731581
? "AsyncIterableIterator"
15741582
: `${name}AsyncIterator`
1575-
: !useIteratorObject
1583+
: !compilerBehavior.useIteratorObject
15761584
? "IterableIterator"
15771585
: subtypes.length !== 1
15781586
? `${name}Iterator`

0 commit comments

Comments
 (0)