From 4d7a2715d98046c003070bb9071eac7cb354daca Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com> Date: Fri, 17 Jan 2025 15:26:06 -0800 Subject: [PATCH 1/3] Override MessageEventTarget's events' this type --- baselines/audioworklet.generated.d.ts | 4 ++-- baselines/dom.generated.d.ts | 4 ++-- baselines/serviceworker.generated.d.ts | 4 ++-- baselines/sharedworker.generated.d.ts | 4 ++-- baselines/ts5.5/audioworklet.generated.d.ts | 4 ++-- baselines/ts5.5/dom.generated.d.ts | 4 ++-- baselines/ts5.5/serviceworker.generated.d.ts | 4 ++-- baselines/ts5.5/sharedworker.generated.d.ts | 4 ++-- baselines/ts5.5/webworker.generated.d.ts | 8 ++++---- baselines/webworker.generated.d.ts | 8 ++++---- inputfiles/overridingTypes.jsonc | 1 + src/build/emitter.ts | 12 +++++++++--- src/build/types.d.ts | 1 + 13 files changed, 35 insertions(+), 27 deletions(-) diff --git a/baselines/audioworklet.generated.d.ts b/baselines/audioworklet.generated.d.ts index 1d5a8086b..4d5273eca 100644 --- a/baselines/audioworklet.generated.d.ts +++ b/baselines/audioworklet.generated.d.ts @@ -684,9 +684,9 @@ interface MessageEventTargetEventMap { interface MessageEventTarget { /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/DedicatedWorkerGlobalScope/message_event) */ - onmessage: ((this: MessageEventTarget, ev: MessageEvent) => any) | null; + onmessage: ((this: this, ev: MessageEvent) => any) | null; /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/DedicatedWorkerGlobalScope/messageerror_event) */ - onmessageerror: ((this: MessageEventTarget, ev: MessageEvent) => any) | null; + onmessageerror: ((this: this, ev: MessageEvent) => any) | null; addEventListener(type: K, listener: (this: MessageEventTarget, ev: MessageEventTargetEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void; addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void; removeEventListener(type: K, listener: (this: MessageEventTarget, ev: MessageEventTargetEventMap[K]) => any, options?: boolean | EventListenerOptions): void; diff --git a/baselines/dom.generated.d.ts b/baselines/dom.generated.d.ts index bcae88127..5c9706a2f 100644 --- a/baselines/dom.generated.d.ts +++ b/baselines/dom.generated.d.ts @@ -16391,9 +16391,9 @@ interface MessageEventTargetEventMap { interface MessageEventTarget { /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/DedicatedWorkerGlobalScope/message_event) */ - onmessage: ((this: MessageEventTarget, ev: MessageEvent) => any) | null; + onmessage: ((this: this, ev: MessageEvent) => any) | null; /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/DedicatedWorkerGlobalScope/messageerror_event) */ - onmessageerror: ((this: MessageEventTarget, ev: MessageEvent) => any) | null; + onmessageerror: ((this: this, ev: MessageEvent) => any) | null; addEventListener(type: K, listener: (this: MessageEventTarget, ev: MessageEventTargetEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void; addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void; removeEventListener(type: K, listener: (this: MessageEventTarget, ev: MessageEventTargetEventMap[K]) => any, options?: boolean | EventListenerOptions): void; diff --git a/baselines/serviceworker.generated.d.ts b/baselines/serviceworker.generated.d.ts index f848ec0fd..7c9ebd472 100644 --- a/baselines/serviceworker.generated.d.ts +++ b/baselines/serviceworker.generated.d.ts @@ -3761,9 +3761,9 @@ interface MessageEventTargetEventMap { interface MessageEventTarget { /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/DedicatedWorkerGlobalScope/message_event) */ - onmessage: ((this: MessageEventTarget, ev: MessageEvent) => any) | null; + onmessage: ((this: this, ev: MessageEvent) => any) | null; /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/DedicatedWorkerGlobalScope/messageerror_event) */ - onmessageerror: ((this: MessageEventTarget, ev: MessageEvent) => any) | null; + onmessageerror: ((this: this, ev: MessageEvent) => any) | null; addEventListener(type: K, listener: (this: MessageEventTarget, ev: MessageEventTargetEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void; addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void; removeEventListener(type: K, listener: (this: MessageEventTarget, ev: MessageEventTargetEventMap[K]) => any, options?: boolean | EventListenerOptions): void; diff --git a/baselines/sharedworker.generated.d.ts b/baselines/sharedworker.generated.d.ts index f2bb37e7a..c8865b37b 100644 --- a/baselines/sharedworker.generated.d.ts +++ b/baselines/sharedworker.generated.d.ts @@ -3650,9 +3650,9 @@ interface MessageEventTargetEventMap { interface MessageEventTarget { /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/DedicatedWorkerGlobalScope/message_event) */ - onmessage: ((this: MessageEventTarget, ev: MessageEvent) => any) | null; + onmessage: ((this: this, ev: MessageEvent) => any) | null; /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/DedicatedWorkerGlobalScope/messageerror_event) */ - onmessageerror: ((this: MessageEventTarget, ev: MessageEvent) => any) | null; + onmessageerror: ((this: this, ev: MessageEvent) => any) | null; addEventListener(type: K, listener: (this: MessageEventTarget, ev: MessageEventTargetEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void; addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void; removeEventListener(type: K, listener: (this: MessageEventTarget, ev: MessageEventTargetEventMap[K]) => any, options?: boolean | EventListenerOptions): void; diff --git a/baselines/ts5.5/audioworklet.generated.d.ts b/baselines/ts5.5/audioworklet.generated.d.ts index 1d5a8086b..4d5273eca 100644 --- a/baselines/ts5.5/audioworklet.generated.d.ts +++ b/baselines/ts5.5/audioworklet.generated.d.ts @@ -684,9 +684,9 @@ interface MessageEventTargetEventMap { interface MessageEventTarget { /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/DedicatedWorkerGlobalScope/message_event) */ - onmessage: ((this: MessageEventTarget, ev: MessageEvent) => any) | null; + onmessage: ((this: this, ev: MessageEvent) => any) | null; /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/DedicatedWorkerGlobalScope/messageerror_event) */ - onmessageerror: ((this: MessageEventTarget, ev: MessageEvent) => any) | null; + onmessageerror: ((this: this, ev: MessageEvent) => any) | null; addEventListener(type: K, listener: (this: MessageEventTarget, ev: MessageEventTargetEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void; addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void; removeEventListener(type: K, listener: (this: MessageEventTarget, ev: MessageEventTargetEventMap[K]) => any, options?: boolean | EventListenerOptions): void; diff --git a/baselines/ts5.5/dom.generated.d.ts b/baselines/ts5.5/dom.generated.d.ts index d6a806977..d519d0f87 100644 --- a/baselines/ts5.5/dom.generated.d.ts +++ b/baselines/ts5.5/dom.generated.d.ts @@ -16371,9 +16371,9 @@ interface MessageEventTargetEventMap { interface MessageEventTarget { /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/DedicatedWorkerGlobalScope/message_event) */ - onmessage: ((this: MessageEventTarget, ev: MessageEvent) => any) | null; + onmessage: ((this: this, ev: MessageEvent) => any) | null; /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/DedicatedWorkerGlobalScope/messageerror_event) */ - onmessageerror: ((this: MessageEventTarget, ev: MessageEvent) => any) | null; + onmessageerror: ((this: this, ev: MessageEvent) => any) | null; addEventListener(type: K, listener: (this: MessageEventTarget, ev: MessageEventTargetEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void; addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void; removeEventListener(type: K, listener: (this: MessageEventTarget, ev: MessageEventTargetEventMap[K]) => any, options?: boolean | EventListenerOptions): void; diff --git a/baselines/ts5.5/serviceworker.generated.d.ts b/baselines/ts5.5/serviceworker.generated.d.ts index f848ec0fd..7c9ebd472 100644 --- a/baselines/ts5.5/serviceworker.generated.d.ts +++ b/baselines/ts5.5/serviceworker.generated.d.ts @@ -3761,9 +3761,9 @@ interface MessageEventTargetEventMap { interface MessageEventTarget { /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/DedicatedWorkerGlobalScope/message_event) */ - onmessage: ((this: MessageEventTarget, ev: MessageEvent) => any) | null; + onmessage: ((this: this, ev: MessageEvent) => any) | null; /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/DedicatedWorkerGlobalScope/messageerror_event) */ - onmessageerror: ((this: MessageEventTarget, ev: MessageEvent) => any) | null; + onmessageerror: ((this: this, ev: MessageEvent) => any) | null; addEventListener(type: K, listener: (this: MessageEventTarget, ev: MessageEventTargetEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void; addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void; removeEventListener(type: K, listener: (this: MessageEventTarget, ev: MessageEventTargetEventMap[K]) => any, options?: boolean | EventListenerOptions): void; diff --git a/baselines/ts5.5/sharedworker.generated.d.ts b/baselines/ts5.5/sharedworker.generated.d.ts index f2bb37e7a..c8865b37b 100644 --- a/baselines/ts5.5/sharedworker.generated.d.ts +++ b/baselines/ts5.5/sharedworker.generated.d.ts @@ -3650,9 +3650,9 @@ interface MessageEventTargetEventMap { interface MessageEventTarget { /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/DedicatedWorkerGlobalScope/message_event) */ - onmessage: ((this: MessageEventTarget, ev: MessageEvent) => any) | null; + onmessage: ((this: this, ev: MessageEvent) => any) | null; /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/DedicatedWorkerGlobalScope/messageerror_event) */ - onmessageerror: ((this: MessageEventTarget, ev: MessageEvent) => any) | null; + onmessageerror: ((this: this, ev: MessageEvent) => any) | null; addEventListener(type: K, listener: (this: MessageEventTarget, ev: MessageEventTargetEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void; addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void; removeEventListener(type: K, listener: (this: MessageEventTarget, ev: MessageEventTargetEventMap[K]) => any, options?: boolean | EventListenerOptions): void; diff --git a/baselines/ts5.5/webworker.generated.d.ts b/baselines/ts5.5/webworker.generated.d.ts index 37c8fd5ca..ae8f2f78d 100644 --- a/baselines/ts5.5/webworker.generated.d.ts +++ b/baselines/ts5.5/webworker.generated.d.ts @@ -4335,9 +4335,9 @@ interface MessageEventTargetEventMap { interface MessageEventTarget { /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/DedicatedWorkerGlobalScope/message_event) */ - onmessage: ((this: MessageEventTarget, ev: MessageEvent) => any) | null; + onmessage: ((this: this, ev: MessageEvent) => any) | null; /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/DedicatedWorkerGlobalScope/messageerror_event) */ - onmessageerror: ((this: MessageEventTarget, ev: MessageEvent) => any) | null; + onmessageerror: ((this: this, ev: MessageEvent) => any) | null; addEventListener(type: K, listener: (this: MessageEventTarget, ev: MessageEventTargetEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void; addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void; removeEventListener(type: K, listener: (this: MessageEventTarget, ev: MessageEventTargetEventMap[K]) => any, options?: boolean | EventListenerOptions): void; @@ -9732,9 +9732,9 @@ declare function cancelAnimationFrame(handle: number): void; /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/DedicatedWorkerGlobalScope/requestAnimationFrame) */ declare function requestAnimationFrame(callback: FrameRequestCallback): number; /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/DedicatedWorkerGlobalScope/message_event) */ -declare var onmessage: ((this: DedicatedWorkerGlobalScope, ev: MessageEvent) => any) | null; +declare var onmessage: ((this: this, ev: MessageEvent) => any) | null; /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/DedicatedWorkerGlobalScope/messageerror_event) */ -declare var onmessageerror: ((this: DedicatedWorkerGlobalScope, ev: MessageEvent) => any) | null; +declare var onmessageerror: ((this: this, ev: MessageEvent) => any) | null; declare function addEventListener(type: K, listener: (this: DedicatedWorkerGlobalScope, ev: DedicatedWorkerGlobalScopeEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void; declare function addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void; declare function removeEventListener(type: K, listener: (this: DedicatedWorkerGlobalScope, ev: DedicatedWorkerGlobalScopeEventMap[K]) => any, options?: boolean | EventListenerOptions): void; diff --git a/baselines/webworker.generated.d.ts b/baselines/webworker.generated.d.ts index 37c8fd5ca..ae8f2f78d 100644 --- a/baselines/webworker.generated.d.ts +++ b/baselines/webworker.generated.d.ts @@ -4335,9 +4335,9 @@ interface MessageEventTargetEventMap { interface MessageEventTarget { /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/DedicatedWorkerGlobalScope/message_event) */ - onmessage: ((this: MessageEventTarget, ev: MessageEvent) => any) | null; + onmessage: ((this: this, ev: MessageEvent) => any) | null; /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/DedicatedWorkerGlobalScope/messageerror_event) */ - onmessageerror: ((this: MessageEventTarget, ev: MessageEvent) => any) | null; + onmessageerror: ((this: this, ev: MessageEvent) => any) | null; addEventListener(type: K, listener: (this: MessageEventTarget, ev: MessageEventTargetEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void; addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void; removeEventListener(type: K, listener: (this: MessageEventTarget, ev: MessageEventTargetEventMap[K]) => any, options?: boolean | EventListenerOptions): void; @@ -9732,9 +9732,9 @@ declare function cancelAnimationFrame(handle: number): void; /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/DedicatedWorkerGlobalScope/requestAnimationFrame) */ declare function requestAnimationFrame(callback: FrameRequestCallback): number; /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/DedicatedWorkerGlobalScope/message_event) */ -declare var onmessage: ((this: DedicatedWorkerGlobalScope, ev: MessageEvent) => any) | null; +declare var onmessage: ((this: this, ev: MessageEvent) => any) | null; /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/DedicatedWorkerGlobalScope/messageerror_event) */ -declare var onmessageerror: ((this: DedicatedWorkerGlobalScope, ev: MessageEvent) => any) | null; +declare var onmessageerror: ((this: this, ev: MessageEvent) => any) | null; declare function addEventListener(type: K, listener: (this: DedicatedWorkerGlobalScope, ev: DedicatedWorkerGlobalScopeEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void; declare function addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void; declare function removeEventListener(type: K, listener: (this: DedicatedWorkerGlobalScope, ev: DedicatedWorkerGlobalScopeEventMap[K]) => any, options?: boolean | EventListenerOptions): void; diff --git a/inputfiles/overridingTypes.jsonc b/inputfiles/overridingTypes.jsonc index 35d52d3dc..bdbbd7c12 100644 --- a/inputfiles/overridingTypes.jsonc +++ b/inputfiles/overridingTypes.jsonc @@ -2,6 +2,7 @@ "mixins": { "mixin": { "MessageEventTarget": { + "overrideThisType": "this", "events": { "event": [ { diff --git a/src/build/emitter.ts b/src/build/emitter.ts index 0760d7dd7..86414e8cf 100644 --- a/src/build/emitter.ts +++ b/src/build/emitter.ts @@ -768,8 +768,14 @@ export function emitWebIdl( .forEach(emitEnum); } - function emitEventHandlerThis(prefix: string, i: Browser.Interface) { - if (prefix === "") { + function emitEventHandlerThis( + overrideThisType: string | undefined, + prefix: string, + i: Browser.Interface, + ) { + if (overrideThisType) { + return `this: ${overrideThisType}, `; + } else if (prefix === "") { return `this: ${nameWithForwardedTypes(i)}, `; } else { return polluter ? `this: ${polluter.name}, ` : ""; @@ -815,7 +821,7 @@ export function emitWebIdl( const eType = p.eventHandler ? getEventTypeInInterface(p.eventHandler!, i) : "Event"; - pType = `(${emitEventHandlerThis(prefix, i)}ev: ${eType}) => any`; + pType = `(${emitEventHandlerThis(i.overrideThisType, prefix, i)}ev: ${eType}) => any`; if (typeof p.type === "string" && !p.type.endsWith("NonNull")) { pType = `(${pType}) | null`; } diff --git a/src/build/types.d.ts b/src/build/types.d.ts index b3c68a68f..c4583389c 100644 --- a/src/build/types.d.ts +++ b/src/build/types.d.ts @@ -181,6 +181,7 @@ export interface Interface { overrideExposed?: string; tags?: string; "implicit-this"?: 1; + overrideThisType?: string; noInterfaceObject?: boolean; global?: string; typeParameters?: TypeParameter[]; From 588d6d3f0350fdd77c96d6701aa76988b376eb15 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com> Date: Fri, 17 Jan 2025 15:39:56 -0800 Subject: [PATCH 2/3] do not override this on globals --- baselines/ts5.5/webworker.generated.d.ts | 4 ++-- baselines/webworker.generated.d.ts | 4 ++-- src/build/emitter.ts | 8 ++++---- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/baselines/ts5.5/webworker.generated.d.ts b/baselines/ts5.5/webworker.generated.d.ts index ae8f2f78d..4027629be 100644 --- a/baselines/ts5.5/webworker.generated.d.ts +++ b/baselines/ts5.5/webworker.generated.d.ts @@ -9732,9 +9732,9 @@ declare function cancelAnimationFrame(handle: number): void; /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/DedicatedWorkerGlobalScope/requestAnimationFrame) */ declare function requestAnimationFrame(callback: FrameRequestCallback): number; /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/DedicatedWorkerGlobalScope/message_event) */ -declare var onmessage: ((this: this, ev: MessageEvent) => any) | null; +declare var onmessage: ((this: DedicatedWorkerGlobalScope, ev: MessageEvent) => any) | null; /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/DedicatedWorkerGlobalScope/messageerror_event) */ -declare var onmessageerror: ((this: this, ev: MessageEvent) => any) | null; +declare var onmessageerror: ((this: DedicatedWorkerGlobalScope, ev: MessageEvent) => any) | null; declare function addEventListener(type: K, listener: (this: DedicatedWorkerGlobalScope, ev: DedicatedWorkerGlobalScopeEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void; declare function addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void; declare function removeEventListener(type: K, listener: (this: DedicatedWorkerGlobalScope, ev: DedicatedWorkerGlobalScopeEventMap[K]) => any, options?: boolean | EventListenerOptions): void; diff --git a/baselines/webworker.generated.d.ts b/baselines/webworker.generated.d.ts index ae8f2f78d..4027629be 100644 --- a/baselines/webworker.generated.d.ts +++ b/baselines/webworker.generated.d.ts @@ -9732,9 +9732,9 @@ declare function cancelAnimationFrame(handle: number): void; /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/DedicatedWorkerGlobalScope/requestAnimationFrame) */ declare function requestAnimationFrame(callback: FrameRequestCallback): number; /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/DedicatedWorkerGlobalScope/message_event) */ -declare var onmessage: ((this: this, ev: MessageEvent) => any) | null; +declare var onmessage: ((this: DedicatedWorkerGlobalScope, ev: MessageEvent) => any) | null; /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/DedicatedWorkerGlobalScope/messageerror_event) */ -declare var onmessageerror: ((this: this, ev: MessageEvent) => any) | null; +declare var onmessageerror: ((this: DedicatedWorkerGlobalScope, ev: MessageEvent) => any) | null; declare function addEventListener(type: K, listener: (this: DedicatedWorkerGlobalScope, ev: DedicatedWorkerGlobalScopeEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void; declare function addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void; declare function removeEventListener(type: K, listener: (this: DedicatedWorkerGlobalScope, ev: DedicatedWorkerGlobalScopeEventMap[K]) => any, options?: boolean | EventListenerOptions): void; diff --git a/src/build/emitter.ts b/src/build/emitter.ts index 86414e8cf..06406033b 100644 --- a/src/build/emitter.ts +++ b/src/build/emitter.ts @@ -773,10 +773,10 @@ export function emitWebIdl( prefix: string, i: Browser.Interface, ) { - if (overrideThisType) { - return `this: ${overrideThisType}, `; - } else if (prefix === "") { - return `this: ${nameWithForwardedTypes(i)}, `; + if (prefix === "") { + return overrideThisType + ? `this: ${overrideThisType}, ` + : `this: ${nameWithForwardedTypes(i)}, `; } else { return polluter ? `this: ${polluter.name}, ` : ""; } From f4e31bca57122d9bd669da5220c8e20e5f88f0ae Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com> Date: Tue, 21 Jan 2025 09:35:24 -0800 Subject: [PATCH 3/3] Default to this: this and add overrides to exclude There are only 3 right now. --- inputfiles/overridingTypes.jsonc | 8 +++++++- src/build/emitter.ts | 15 ++++++--------- src/build/types.d.ts | 2 +- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/inputfiles/overridingTypes.jsonc b/inputfiles/overridingTypes.jsonc index bdbbd7c12..07518e6c2 100644 --- a/inputfiles/overridingTypes.jsonc +++ b/inputfiles/overridingTypes.jsonc @@ -2,7 +2,6 @@ "mixins": { "mixin": { "MessageEventTarget": { - "overrideThisType": "this", "events": { "event": [ { @@ -41,7 +40,14 @@ } } }, + "AbstractWorker": { + "concreteThisParameter": true + }, + "WindowEventHandlers": { + "concreteThisParameter": true + }, "GlobalEventHandlers": { + "concreteThisParameter": true, "events": { "event": [ { diff --git a/src/build/emitter.ts b/src/build/emitter.ts index 06406033b..a4ab6e6d2 100644 --- a/src/build/emitter.ts +++ b/src/build/emitter.ts @@ -768,15 +768,12 @@ export function emitWebIdl( .forEach(emitEnum); } - function emitEventHandlerThis( - overrideThisType: string | undefined, - prefix: string, - i: Browser.Interface, - ) { + function emitEventHandlerThis(prefix: string, i: Browser.Interface) { if (prefix === "") { - return overrideThisType - ? `this: ${overrideThisType}, ` - : `this: ${nameWithForwardedTypes(i)}, `; + if (i.mixin && !i.concreteThisParameter) { + return `this: this, `; + } + return `this: ${nameWithForwardedTypes(i)}, `; } else { return polluter ? `this: ${polluter.name}, ` : ""; } @@ -821,7 +818,7 @@ export function emitWebIdl( const eType = p.eventHandler ? getEventTypeInInterface(p.eventHandler!, i) : "Event"; - pType = `(${emitEventHandlerThis(i.overrideThisType, prefix, i)}ev: ${eType}) => any`; + pType = `(${emitEventHandlerThis(prefix, i)}ev: ${eType}) => any`; if (typeof p.type === "string" && !p.type.endsWith("NonNull")) { pType = `(${pType}) | null`; } diff --git a/src/build/types.d.ts b/src/build/types.d.ts index c4583389c..ed0e45904 100644 --- a/src/build/types.d.ts +++ b/src/build/types.d.ts @@ -181,7 +181,7 @@ export interface Interface { overrideExposed?: string; tags?: string; "implicit-this"?: 1; - overrideThisType?: string; + concreteThisParameter?: boolean; noInterfaceObject?: boolean; global?: string; typeParameters?: TypeParameter[];