Skip to content

Commit 288facf

Browse files
saschanazsandersn
authored andcommitted
Add explicit toString() (#754)
* Add explicit toString() * support `stringifier;`
1 parent d182a31 commit 288facf

File tree

4 files changed

+47
-7
lines changed

4 files changed

+47
-7
lines changed

baselines/dom.generated.d.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4026,6 +4026,7 @@ interface DOMMatrixReadOnly {
40264026
toJSON(): any;
40274027
transformPoint(point?: DOMPointInit): DOMPoint;
40284028
translate(tx?: number, ty?: number, tz?: number): DOMMatrix;
4029+
toString(): string;
40294030
}
40304031

40314032
declare var DOMMatrixReadOnly: {
@@ -4034,6 +4035,7 @@ declare var DOMMatrixReadOnly: {
40344035
fromFloat32Array(array32: Float32Array): DOMMatrixReadOnly;
40354036
fromFloat64Array(array64: Float64Array): DOMMatrixReadOnly;
40364037
fromMatrix(other?: DOMMatrixInit): DOMMatrixReadOnly;
4038+
toString(): string;
40374039
};
40384040

40394041
/** Provides the ability to parse XML or HTML source code from a string into a DOM Document. */
@@ -4191,6 +4193,7 @@ interface DOMTokenList {
41914193
* Can be set, to change the associated attribute.
41924194
*/
41934195
value: string;
4196+
toString(): string;
41944197
/**
41954198
* Adds all arguments passed, except those already present.
41964199
*
@@ -7071,6 +7074,7 @@ interface HTMLHyperlinkElementUtils {
70717074
host: string;
70727075
hostname: string;
70737076
href: string;
7077+
toString(): string;
70747078
readonly origin: string;
70757079
password: string;
70767080
pathname: string;
@@ -9924,6 +9928,7 @@ interface Location {
99249928
* Can be set, to navigate to the given URL.
99259929
*/
99269930
href: string;
9931+
toString(): string;
99279932
/**
99289933
* Returns the Location object's URL's origin.
99299934
*/
@@ -10382,10 +10387,10 @@ declare var MediaKeys: {
1038210387
interface MediaList {
1038310388
readonly length: number;
1038410389
mediaText: string;
10390+
toString(): string;
1038510391
appendMedium(medium: string): void;
1038610392
deleteMedium(medium: string): void;
1038710393
item(index: number): string | null;
10388-
toString(): number;
1038910394
[index: number]: string;
1039010395
}
1039110396

@@ -12652,6 +12657,7 @@ interface Range extends AbstractRange {
1265212657
setStartAfter(node: Node): void;
1265312658
setStartBefore(node: Node): void;
1265412659
surroundContents(newParent: Node): void;
12660+
toString(): string;
1265512661
readonly END_TO_END: number;
1265612662
readonly END_TO_START: number;
1265712663
readonly START_TO_END: number;
@@ -12665,6 +12671,7 @@ declare var Range: {
1266512671
readonly END_TO_START: number;
1266612672
readonly START_TO_END: number;
1266712673
readonly START_TO_START: number;
12674+
toString(): string;
1266812675
};
1266912676

1267012677
interface ReadableByteStreamController {
@@ -15036,11 +15043,13 @@ interface Selection {
1503615043
selectAllChildren(node: Node): void;
1503715044
setBaseAndExtent(anchorNode: Node, anchorOffset: number, focusNode: Node, focusOffset: number): void;
1503815045
setPosition(node: Node | null, offset?: number): void;
15046+
toString(): string;
1503915047
}
1504015048

1504115049
declare var Selection: {
1504215050
prototype: Selection;
1504315051
new(): Selection;
15052+
toString(): string;
1504415053
};
1504515054

1504615055
interface ServiceUIFrameContext {
@@ -16045,6 +16054,7 @@ interface URL {
1604516054
host: string;
1604616055
hostname: string;
1604716056
href: string;
16057+
toString(): string;
1604816058
readonly origin: string;
1604916059
password: string;
1605016060
pathname: string;
@@ -16092,12 +16102,14 @@ interface URLSearchParams {
1609216102
*/
1609316103
set(name: string, value: string): void;
1609416104
sort(): void;
16105+
toString(): string;
1609516106
forEach(callbackfn: (value: string, key: string, parent: URLSearchParams) => void, thisArg?: any): void;
1609616107
}
1609716108

1609816109
declare var URLSearchParams: {
1609916110
prototype: URLSearchParams;
1610016111
new(init?: string[][] | Record<string, string> | string | URLSearchParams): URLSearchParams;
16112+
toString(): string;
1610116113
};
1610216114

1610316115
/** This WebVR API interface represents any VR device supported by this API. It includes generic information such as device IDs and descriptions, as well as methods for starting to present a VR scene, retrieving eye parameters and display capabilities, and other important functionality. */

baselines/webworker.generated.d.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3211,6 +3211,7 @@ interface URL {
32113211
host: string;
32123212
hostname: string;
32133213
href: string;
3214+
toString(): string;
32143215
readonly origin: string;
32153216
password: string;
32163217
pathname: string;
@@ -3255,12 +3256,14 @@ interface URLSearchParams {
32553256
*/
32563257
set(name: string, value: string): void;
32573258
sort(): void;
3259+
toString(): string;
32583260
forEach(callbackfn: (value: string, key: string, parent: URLSearchParams) => void, thisArg?: any): void;
32593261
}
32603262

32613263
declare var URLSearchParams: {
32623264
prototype: URLSearchParams;
32633265
new(init?: string[][] | Record<string, string> | string | URLSearchParams): URLSearchParams;
3266+
toString(): string;
32643267
};
32653268

32663269
interface WEBGL_color_buffer_float {
@@ -5389,12 +5392,12 @@ interface WorkerLocation {
53895392
readonly host: string;
53905393
readonly hostname: string;
53915394
readonly href: string;
5395+
toString(): string;
53925396
readonly origin: string;
53935397
readonly pathname: string;
53945398
readonly port: string;
53955399
readonly protocol: string;
53965400
readonly search: string;
5397-
toString(): string;
53985401
}
53995402

54005403
declare var WorkerLocation: {

src/emitter.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,10 @@ export function emitWebIdl(webidl: Browser.WebIdl, flavor: Flavor) {
626626
const readOnlyModifier = p["read-only"] === 1 && prefix === "" ? "readonly " : "";
627627
printer.printLine(`${prefix}${readOnlyModifier}${p.name}${requiredModifier}: ${pType};`);
628628
}
629+
630+
if (p.stringifier) {
631+
printer.printLine("toString(): string;")
632+
}
629633
}
630634

631635
function emitComments(entity: { comment?: string; deprecated?: 1 }, print: (s: string) => void) {
@@ -666,7 +670,16 @@ export function emitWebIdl(webidl: Browser.WebIdl, flavor: Flavor) {
666670
case "querySelector": return emitQuerySelectorOverloads(m);
667671
case "querySelectorAll": return emitQuerySelectorAllOverloads(m);
668672
}
669-
emitSignatures(m, prefix, m.name, printLine);
673+
674+
// ignore toString() provided from browser.webidl.preprocessed.json
675+
// to prevent duplication
676+
if (m.name !== "toString") {
677+
emitSignatures(m, prefix, m.name, printLine);
678+
679+
if (m.stringifier) {
680+
printLine("toString(): string;")
681+
}
682+
}
670683
}
671684

672685
function emitSignature(s: Browser.Signature, prefix: string | undefined, name: string | undefined, printLine: (s: string) => void) {
@@ -698,6 +711,12 @@ export function emitWebIdl(webidl: Browser.WebIdl, flavor: Flavor) {
698711
.sort(compareName)
699712
.forEach(m => emitMethod(prefix, m, conflictedMembers));
700713
}
714+
if (i["anonymous-methods"]) {
715+
const stringifier = i["anonymous-methods"].method.find(m => m.stringifier);
716+
if (stringifier) {
717+
printer.printLine("toString(): string;");
718+
}
719+
}
701720

702721
// The window interface inherited some methods from "Object",
703722
// which need to explicitly exposed

src/widlprocess.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ function convertInterfaceCommon(i: webidl2.InterfaceType | webidl2.InterfaceMixi
145145
properties!.property[member.name] = prop;
146146
}
147147
}
148-
else if (member.type === "operation" && member.idlType) {
148+
else if (member.type === "operation") {
149149
const operation = convertOperation(member, result.exposed);
150150
const { method } = result.methods;
151151
if (!member.name) {
@@ -205,18 +205,23 @@ function getNamedConstructor(extAttrs: webidl2.ExtendedAttribute[], parent: stri
205205
}
206206

207207
function convertOperation(operation: webidl2.OperationMemberType, inheritedExposure: string | undefined): Browser.AnonymousMethod | Browser.Method {
208-
if (!operation.idlType) {
208+
const isStringifier = operation.special === "stringifier";
209+
const type =
210+
operation.idlType ? convertIdlType(operation.idlType) :
211+
isStringifier ? { type: "DOMString" } :
212+
undefined;
213+
if (!type) {
209214
throw new Error("Unexpected anonymous operation");
210215
}
211216
return {
212217
name: operation.name || undefined,
213218
signature: [{
214-
...convertIdlType(operation.idlType),
219+
...type,
215220
param: operation.arguments.map(convertArgument)
216221
}],
217222
getter: operation.special === "getter" ? 1 : undefined,
218223
static: operation.special === "static" ? 1 : undefined,
219-
stringifier: operation.special === "stringifier" ? 1 : undefined,
224+
stringifier: isStringifier ? 1 : undefined,
220225
exposed: getExtAttrConcatenated(operation.extAttrs, "Exposed") || inheritedExposure
221226
};
222227
}
@@ -249,6 +254,7 @@ function convertAttribute(attribute: webidl2.AttributeMemberType, inheritedExpos
249254
name: attribute.name,
250255
...convertIdlType(attribute.idlType),
251256
static: attribute.special === "static" ? 1 : undefined,
257+
stringifier: attribute.special === "stringifier" ? 1 : undefined,
252258
"read-only": attribute.readonly ? 1 : undefined,
253259
"event-handler": isEventHandler ? attribute.name.slice(2) : undefined,
254260
exposed: getExtAttrConcatenated(attribute.extAttrs, "Exposed") || inheritedExposure

0 commit comments

Comments
 (0)