From 3660ac5aa7b9703fd7fc85c39a9d28e60570dc89 Mon Sep 17 00:00:00 2001 From: SarahIsWeird Date: Tue, 18 Feb 2025 15:32:41 +0100 Subject: [PATCH 1/6] Make WebStorm shut up (insert shrek meme here) --- game-scripts-tests/this_array_in_object.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/game-scripts-tests/this_array_in_object.ts b/game-scripts-tests/this_array_in_object.ts index e15c0e1..8f4f347 100644 --- a/game-scripts-tests/this_array_in_object.ts +++ b/game-scripts-tests/this_array_in_object.ts @@ -3,6 +3,7 @@ export default ({ expect }: typeof import('vitest')) => { 0: `test[0]`, foo: [ function () { + // @ts-ignore return this[0] } ], From 87bf5c08a2e111d09b499ca5534929954cf52e74 Mon Sep 17 00:00:00 2001 From: SarahIsWeird Date: Tue, 18 Feb 2025 16:48:57 +0100 Subject: [PATCH 2/6] Add failing test for `this` in argument default --- .../this_in_default_argument.ts | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 game-scripts-tests/this_in_default_argument.ts diff --git a/game-scripts-tests/this_in_default_argument.ts b/game-scripts-tests/this_in_default_argument.ts new file mode 100644 index 0000000..fc378b7 --- /dev/null +++ b/game-scripts-tests/this_in_default_argument.ts @@ -0,0 +1,35 @@ +export default ({ expect }: typeof import('vitest')) => { + class Foo { + declare foo: number; + + constructor() { + this.foo = 1 + } + + bar() { + return 2 + } + + baz(a = this.foo, b = this.bar()) { + return a + b + } + } + + const obj = { + foo: 1, + bar() { + return 2 + }, + // @ts-ignore + baz(a = this.foo, b = this.bar()) { + return a + b + } + } + + const foo = new Foo(); + expect(foo.baz(3, 4)).toBe(7) // Is this the real life? + expect(foo.baz()).toBe(3) // Is this just fantasy? + + expect(obj.baz(3, 4)).toBe(7) // Caught in a landslide + expect(obj.baz()).toBe(3) // No escape from reality +} From 6f8bf68c63fc609f454d4e4115c202c8a3021754 Mon Sep 17 00:00:00 2001 From: SarahIsWeird Date: Tue, 18 Feb 2025 17:00:16 +0100 Subject: [PATCH 3/6] Fix `this` reference in default argument --- src/processScript/transform.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/processScript/transform.ts b/src/processScript/transform.ts index 16776e3..99eed3e 100644 --- a/src/processScript/transform.ts +++ b/src/processScript/transform.ts @@ -821,6 +821,19 @@ export function transform( let methodReferencesThis = false as boolean + for (const param of classMethod.params) { + traverse(param, { + ThisExpression(path) { + // We can't reference _abc_THIS_ here, because there would be + // nowhere to place the assignment to that! + // Hence, we just do the super thing directly. + path.replaceWith(t.callExpression( + t.memberExpression(t.super(), t.identifier(`valueOf`)), [] + )) + }, + }, scope) + } + traverse(classMethod.body, { ThisExpression(path) { methodReferencesThis = true From acf9e46431b6da9df8d47733aaa6cd0082abcf95 Mon Sep 17 00:00:00 2001 From: SarahIsWeird Date: Tue, 18 Feb 2025 17:05:04 +0100 Subject: [PATCH 4/6] Make the linter happy --- game-scripts-tests/this_array_in_object.ts | 2 +- game-scripts-tests/this_in_default_argument.ts | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/game-scripts-tests/this_array_in_object.ts b/game-scripts-tests/this_array_in_object.ts index 8f4f347..596060e 100644 --- a/game-scripts-tests/this_array_in_object.ts +++ b/game-scripts-tests/this_array_in_object.ts @@ -3,7 +3,7 @@ export default ({ expect }: typeof import('vitest')) => { 0: `test[0]`, foo: [ function () { - // @ts-ignore + // @ts-expect-error return this[0] } ], diff --git a/game-scripts-tests/this_in_default_argument.ts b/game-scripts-tests/this_in_default_argument.ts index fc378b7..f7d0de5 100644 --- a/game-scripts-tests/this_in_default_argument.ts +++ b/game-scripts-tests/this_in_default_argument.ts @@ -1,6 +1,6 @@ export default ({ expect }: typeof import('vitest')) => { class Foo { - declare foo: number; + declare foo: number constructor() { this.foo = 1 @@ -20,13 +20,13 @@ export default ({ expect }: typeof import('vitest')) => { bar() { return 2 }, - // @ts-ignore + // @ts-expect-error baz(a = this.foo, b = this.bar()) { return a + b } } - const foo = new Foo(); + const foo = new Foo() expect(foo.baz(3, 4)).toBe(7) // Is this the real life? expect(foo.baz()).toBe(3) // Is this just fantasy? From 12a6f1d51b204e0baf77e12801c3e7e0a7c7ab9e Mon Sep 17 00:00:00 2001 From: SarahIsWeird Date: Tue, 18 Feb 2025 17:08:02 +0100 Subject: [PATCH 5/6] Make the linter happy 2: Electric Bogaloo --- game-scripts-tests/this_array_in_object.ts | 1 - game-scripts-tests/this_in_default_argument.ts | 1 - 2 files changed, 2 deletions(-) diff --git a/game-scripts-tests/this_array_in_object.ts b/game-scripts-tests/this_array_in_object.ts index 596060e..e15c0e1 100644 --- a/game-scripts-tests/this_array_in_object.ts +++ b/game-scripts-tests/this_array_in_object.ts @@ -3,7 +3,6 @@ export default ({ expect }: typeof import('vitest')) => { 0: `test[0]`, foo: [ function () { - // @ts-expect-error return this[0] } ], diff --git a/game-scripts-tests/this_in_default_argument.ts b/game-scripts-tests/this_in_default_argument.ts index f7d0de5..61e18b4 100644 --- a/game-scripts-tests/this_in_default_argument.ts +++ b/game-scripts-tests/this_in_default_argument.ts @@ -20,7 +20,6 @@ export default ({ expect }: typeof import('vitest')) => { bar() { return 2 }, - // @ts-expect-error baz(a = this.foo, b = this.bar()) { return a + b } From b0ba71fca2aaaeb31aab154222d0db9731d7f57a Mon Sep 17 00:00:00 2001 From: SarahIsWeird Date: Tue, 18 Feb 2025 17:13:44 +0100 Subject: [PATCH 6/6] Make the linter happy 3: --- .../this_in_default_argument.ts | 53 ++++++++++--------- src/processScript/transform.ts | 2 +- 2 files changed, 28 insertions(+), 27 deletions(-) diff --git a/game-scripts-tests/this_in_default_argument.ts b/game-scripts-tests/this_in_default_argument.ts index 61e18b4..f903b3e 100644 --- a/game-scripts-tests/this_in_default_argument.ts +++ b/game-scripts-tests/this_in_default_argument.ts @@ -1,34 +1,35 @@ export default ({ expect }: typeof import('vitest')) => { - class Foo { - declare foo: number + class Foo { + declare foo: number - constructor() { - this.foo = 1 - } + constructor() { + this.foo = 1 + } - bar() { - return 2 - } + bar() { + return 2 + } - baz(a = this.foo, b = this.bar()) { - return a + b - } - } + baz(a = this.foo, b = this.bar()) { + return a + b + } + } - const obj = { - foo: 1, - bar() { - return 2 - }, - baz(a = this.foo, b = this.bar()) { - return a + b - } - } + const obj = { + foo: 1, + bar() { + return 2 + }, + baz(a = this.foo, b = this.bar()) { + return a + b + } + } - const foo = new Foo() - expect(foo.baz(3, 4)).toBe(7) // Is this the real life? - expect(foo.baz()).toBe(3) // Is this just fantasy? + const foo = (new Foo) - expect(obj.baz(3, 4)).toBe(7) // Caught in a landslide - expect(obj.baz()).toBe(3) // No escape from reality + expect(foo.baz(3, 4)).toBe(7) // Is this the real life? + expect(foo.baz()).toBe(3) // Is this just fantasy? + + expect(obj.baz(3, 4)).toBe(7) // Caught in a landslide + expect(obj.baz()).toBe(3) // No escape from reality } diff --git a/src/processScript/transform.ts b/src/processScript/transform.ts index 99eed3e..609f020 100644 --- a/src/processScript/transform.ts +++ b/src/processScript/transform.ts @@ -830,7 +830,7 @@ export function transform( path.replaceWith(t.callExpression( t.memberExpression(t.super(), t.identifier(`valueOf`)), [] )) - }, + } }, scope) }