Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 7a3b7a0

Browse files
committedMar 27, 2024·
Add [Symbol.toStringTag] property to all interfaces
Fixes #1641
1 parent 6621974 commit 7a3b7a0

8 files changed

+5071
-7
lines changed
 
+219
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
/////////////////////////////
2+
/// AudioWorklet [Symbol.toStringTag] APIs
3+
/////////////////////////////
4+
5+
interface AbortController {
6+
/**
7+
* The well-known symbol @@toStringTag.
8+
*
9+
* [MDN Reference](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toStringTag)
10+
*/
11+
readonly [Symbol.toStringTag]: string;
12+
}
13+
14+
interface AudioWorkletProcessor {
15+
/**
16+
* The well-known symbol @@toStringTag.
17+
*
18+
* [MDN Reference](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toStringTag)
19+
*/
20+
readonly [Symbol.toStringTag]: string;
21+
}
22+
23+
interface CompressionStream {
24+
/**
25+
* The well-known symbol @@toStringTag.
26+
*
27+
* [MDN Reference](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toStringTag)
28+
*/
29+
readonly [Symbol.toStringTag]: string;
30+
}
31+
32+
interface DecompressionStream {
33+
/**
34+
* The well-known symbol @@toStringTag.
35+
*
36+
* [MDN Reference](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toStringTag)
37+
*/
38+
readonly [Symbol.toStringTag]: string;
39+
}
40+
41+
interface Event {
42+
/**
43+
* The well-known symbol @@toStringTag.
44+
*
45+
* [MDN Reference](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toStringTag)
46+
*/
47+
readonly [Symbol.toStringTag]: string;
48+
}
49+
50+
interface EventTarget {
51+
/**
52+
* The well-known symbol @@toStringTag.
53+
*
54+
* [MDN Reference](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toStringTag)
55+
*/
56+
readonly [Symbol.toStringTag]: string;
57+
}
58+
59+
interface ReadableByteStreamController {
60+
/**
61+
* The well-known symbol @@toStringTag.
62+
*
63+
* [MDN Reference](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toStringTag)
64+
*/
65+
readonly [Symbol.toStringTag]: string;
66+
}
67+
68+
interface ReadableStream {
69+
/**
70+
* The well-known symbol @@toStringTag.
71+
*
72+
* [MDN Reference](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toStringTag)
73+
*/
74+
readonly [Symbol.toStringTag]: string;
75+
}
76+
77+
interface ReadableStreamBYOBReader {
78+
/**
79+
* The well-known symbol @@toStringTag.
80+
*
81+
* [MDN Reference](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toStringTag)
82+
*/
83+
readonly [Symbol.toStringTag]: string;
84+
}
85+
86+
interface ReadableStreamBYOBRequest {
87+
/**
88+
* The well-known symbol @@toStringTag.
89+
*
90+
* [MDN Reference](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toStringTag)
91+
*/
92+
readonly [Symbol.toStringTag]: string;
93+
}
94+
95+
interface ReadableStreamDefaultController {
96+
/**
97+
* The well-known symbol @@toStringTag.
98+
*
99+
* [MDN Reference](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toStringTag)
100+
*/
101+
readonly [Symbol.toStringTag]: string;
102+
}
103+
104+
interface ReadableStreamDefaultReader {
105+
/**
106+
* The well-known symbol @@toStringTag.
107+
*
108+
* [MDN Reference](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toStringTag)
109+
*/
110+
readonly [Symbol.toStringTag]: string;
111+
}
112+
113+
interface TextDecoder {
114+
/**
115+
* The well-known symbol @@toStringTag.
116+
*
117+
* [MDN Reference](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toStringTag)
118+
*/
119+
readonly [Symbol.toStringTag]: string;
120+
}
121+
122+
interface TextDecoderStream {
123+
/**
124+
* The well-known symbol @@toStringTag.
125+
*
126+
* [MDN Reference](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toStringTag)
127+
*/
128+
readonly [Symbol.toStringTag]: string;
129+
}
130+
131+
interface TextEncoder {
132+
/**
133+
* The well-known symbol @@toStringTag.
134+
*
135+
* [MDN Reference](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toStringTag)
136+
*/
137+
readonly [Symbol.toStringTag]: string;
138+
}
139+
140+
interface TextEncoderStream {
141+
/**
142+
* The well-known symbol @@toStringTag.
143+
*
144+
* [MDN Reference](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toStringTag)
145+
*/
146+
readonly [Symbol.toStringTag]: string;
147+
}
148+
149+
interface TransformStream {
150+
/**
151+
* The well-known symbol @@toStringTag.
152+
*
153+
* [MDN Reference](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toStringTag)
154+
*/
155+
readonly [Symbol.toStringTag]: string;
156+
}
157+
158+
interface TransformStreamDefaultController {
159+
/**
160+
* The well-known symbol @@toStringTag.
161+
*
162+
* [MDN Reference](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toStringTag)
163+
*/
164+
readonly [Symbol.toStringTag]: string;
165+
}
166+
167+
interface URL {
168+
/**
169+
* The well-known symbol @@toStringTag.
170+
*
171+
* [MDN Reference](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toStringTag)
172+
*/
173+
readonly [Symbol.toStringTag]: string;
174+
}
175+
176+
interface URLSearchParams {
177+
/**
178+
* The well-known symbol @@toStringTag.
179+
*
180+
* [MDN Reference](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toStringTag)
181+
*/
182+
readonly [Symbol.toStringTag]: string;
183+
}
184+
185+
interface WorkletGlobalScope {
186+
/**
187+
* The well-known symbol @@toStringTag.
188+
*
189+
* [MDN Reference](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toStringTag)
190+
*/
191+
readonly [Symbol.toStringTag]: string;
192+
}
193+
194+
interface WritableStream {
195+
/**
196+
* The well-known symbol @@toStringTag.
197+
*
198+
* [MDN Reference](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toStringTag)
199+
*/
200+
readonly [Symbol.toStringTag]: string;
201+
}
202+
203+
interface WritableStreamDefaultController {
204+
/**
205+
* The well-known symbol @@toStringTag.
206+
*
207+
* [MDN Reference](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toStringTag)
208+
*/
209+
readonly [Symbol.toStringTag]: string;
210+
}
211+
212+
interface WritableStreamDefaultWriter {
213+
/**
214+
* The well-known symbol @@toStringTag.
215+
*
216+
* [MDN Reference](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toStringTag)
217+
*/
218+
readonly [Symbol.toStringTag]: string;
219+
}

‎baselines/dom.tostringtag.d.ts

+2,064
Large diffs are not rendered by default.

‎baselines/serviceworker.tostringtag.d.ts

+894
Large diffs are not rendered by default.

‎baselines/sharedworker.tostringtag.d.ts

+876
Large diffs are not rendered by default.

‎baselines/webworker.tostringtag.d.ts

+957
Large diffs are not rendered by default.

‎src/build.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,23 @@ async function emitFlavor(
5252
result,
5353
);
5454

55-
const iterators = emitWebIdl(exposed, options.global[0], "sync");
55+
const toStringTag = emitWebIdl(exposed, options.global[0], "toStringTag");
56+
await fs.writeFile(
57+
new URL(`${options.name}.tostringtag.d.ts`, options.outputFolder),
58+
toStringTag,
59+
);
60+
61+
const iterators = emitWebIdl(exposed, options.global[0], "iterator");
5662
await fs.writeFile(
5763
new URL(`${options.name}.iterable.generated.d.ts`, options.outputFolder),
5864
iterators,
5965
);
6066

61-
const asyncIterators = emitWebIdl(exposed, options.global[0], "async");
67+
const asyncIterators = emitWebIdl(
68+
exposed,
69+
options.global[0],
70+
"asyncIterator",
71+
);
6272
await fs.writeFile(
6373
new URL(
6474
`${options.name}.asynciterable.generated.d.ts`,

‎src/build/emitter.ts

+39-5
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ function isEventHandler(p: Browser.Property) {
135135
export function emitWebIdl(
136136
webidl: Browser.WebIdl,
137137
global: string,
138-
iterator: "" | "sync" | "async",
138+
flavor: "" | "iterator" | "asyncIterator" | "toStringTag",
139139
): string {
140140
// Global print target
141141
const printer = createTextWriter("\n");
@@ -230,10 +230,12 @@ export function emitWebIdl(
230230
getParentsWithConstant,
231231
);
232232

233-
switch (iterator) {
234-
case "sync":
233+
switch (flavor) {
234+
case "toStringTag":
235+
return emitES6ToStringTag();
236+
case "iterator":
235237
return emitES6DomIterators();
236-
case "async":
238+
case "asyncIterator":
237239
return emitES2018DomAsyncIterators();
238240
default:
239241
return emit();
@@ -417,7 +419,7 @@ export function emitWebIdl(
417419
}
418420

419421
function convertDomTypeToTsTypeSimple(objDomType: string): string {
420-
if (objDomType === "sequence" && iterator !== "") {
422+
if (objDomType === "sequence" && flavor !== "") {
421423
return "Iterable";
422424
}
423425
if (baseTypeConversionMap.has(objDomType)) {
@@ -1786,6 +1788,38 @@ export function emitWebIdl(
17861788
printer.printLine("}");
17871789
}
17881790

1791+
function emitES6ToStringTag() {
1792+
printer.reset();
1793+
printer.printLine("/////////////////////////////");
1794+
printer.printLine(`/// ${global} [Symbol.toStringTag] APIs`);
1795+
printer.printLine("/////////////////////////////");
1796+
1797+
const toStringTagProperty = {
1798+
type: "USVString",
1799+
name: "[Symbol.toStringTag]",
1800+
readonly: true,
1801+
comment:
1802+
"The well-known symbol @@toStringTag.\n\n" +
1803+
"[MDN Reference](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toStringTag)",
1804+
};
1805+
1806+
const interfaces = getElements(webidl.interfaces, "interface");
1807+
interfaces.sort(compareName).forEach((i) => {
1808+
if (!i.noInterfaceObject && !i.extends && !i.legacyNamespace) {
1809+
printer.printLine("");
1810+
printer.printLine(`interface ${i.name} {`);
1811+
printer.increaseIndent();
1812+
1813+
emitProperty("", i, EmitScope.InstanceOnly, toStringTagProperty);
1814+
1815+
printer.decreaseIndent();
1816+
printer.printLine("}");
1817+
}
1818+
});
1819+
1820+
return printer.getResult();
1821+
}
1822+
17891823
function emitES6DomIterators() {
17901824
printer.reset();
17911825
printer.printLine("/////////////////////////////");

‎src/test.ts

+10
Original file line numberDiff line numberDiff line change
@@ -60,55 +60,65 @@ function test() {
6060
compileGeneratedFiles(
6161
"es6",
6262
"dom.generated.d.ts",
63+
"dom.tostringtag.d.ts",
6364
"dom.iterable.generated.d.ts",
6465
) &&
6566
compileGeneratedFiles(
6667
"es2018",
6768
"dom.generated.d.ts",
69+
"dom.tostringtag.d.ts",
6870
"dom.asynciterable.generated.d.ts",
6971
) &&
7072
compileGeneratedFiles("es5", "webworker.generated.d.ts") &&
7173
compileGeneratedFiles(
7274
"es6",
7375
"webworker.generated.d.ts",
76+
"webworker.tostringtag.d.ts",
7477
"webworker.iterable.generated.d.ts",
7578
) &&
7679
compileGeneratedFiles(
7780
"es2018",
7881
"webworker.generated.d.ts",
82+
"webworker.tostringtag.d.ts",
7983
"webworker.asynciterable.generated.d.ts",
8084
) &&
8185
compileGeneratedFiles("es5", "sharedworker.generated.d.ts") &&
8286
compileGeneratedFiles(
8387
"es6",
8488
"sharedworker.generated.d.ts",
89+
"sharedworker.tostringtag.d.ts",
8590
"sharedworker.iterable.generated.d.ts",
8691
) &&
8792
compileGeneratedFiles(
8893
"es2018",
8994
"sharedworker.generated.d.ts",
95+
"sharedworker.tostringtag.d.ts",
9096
"sharedworker.asynciterable.generated.d.ts",
9197
) &&
9298
compileGeneratedFiles("es5", "serviceworker.generated.d.ts") &&
9399
compileGeneratedFiles(
94100
"es6",
95101
"serviceworker.generated.d.ts",
102+
"serviceworker.tostringtag.d.ts",
96103
"serviceworker.iterable.generated.d.ts",
97104
) &&
98105
compileGeneratedFiles(
99106
"es2018",
100107
"serviceworker.generated.d.ts",
108+
"serviceworker.tostringtag.d.ts",
101109
"serviceworker.asynciterable.generated.d.ts",
102110
) &&
103111
compileGeneratedFiles("es5", "audioworklet.generated.d.ts") &&
104112
compileGeneratedFiles(
105113
"es6",
106114
"audioworklet.generated.d.ts",
115+
"audioworklet.tostringtag.d.ts",
107116
"audioworklet.iterable.generated.d.ts",
108117
) &&
109118
compileGeneratedFiles(
110119
"es2018",
111120
"audioworklet.generated.d.ts",
121+
"audioworklet.tostringtag.d.ts",
112122
"audioworklet.asynciterable.generated.d.ts",
113123
)
114124
) {

0 commit comments

Comments
 (0)
Please sign in to comment.