From ad3b458c6ab3fc04ad71a9672aee25573e4e8813 Mon Sep 17 00:00:00 2001 From: SarahIsWeird Date: Mon, 14 Apr 2025 22:19:40 +0200 Subject: [PATCH 1/6] Add failing test for #256 --- .../this_in_lambda_in_object.ts | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 game-scripts-tests/this_in_lambda_in_object.ts diff --git a/game-scripts-tests/this_in_lambda_in_object.ts b/game-scripts-tests/this_in_lambda_in_object.ts new file mode 100644 index 0000000..4ee75d4 --- /dev/null +++ b/game-scripts-tests/this_in_lambda_in_object.ts @@ -0,0 +1,23 @@ +export default ({ expect }: typeof import('vitest')) => { + const myObject = { + a: 0, + b: function () { + this.a++ + return this.a + }, + c: function (value: number = this.a) { + this.a = value + 1 + }, + d: function ({ value = this.a }: { value?: number }) { + this.a = value + 1 + } + } + + expect(myObject.a).toBe(0) + myObject.b() + expect(myObject.a).toBe(1) + myObject.c() + expect(myObject.a).toBe(2) + myObject.d({}) + expect(myObject.a).toBe(3) +} From 85c85798f4efc083a8e9134bc0c2e984b65571ec Mon Sep 17 00:00:00 2001 From: SarahIsWeird Date: Mon, 14 Apr 2025 22:44:35 +0200 Subject: [PATCH 2/6] Replace `this` in object property function exprs --- src/processScript/transform.ts | 41 +++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/src/processScript/transform.ts b/src/processScript/transform.ts index 609f020..9724d6e 100644 --- a/src/processScript/transform.ts +++ b/src/processScript/transform.ts @@ -1,13 +1,23 @@ /* eslint-disable jsdoc/check-param-names */ -import type { NodePath, Scope } from "@babel/traverse" -import type { ArrayExpression, Block, BlockStatement, CallExpression, File, FunctionDeclaration, Identifier, Node, ObjectExpression } from "@babel/types" -import type { LaxPartial } from "@samual/lib" -import traverse from "@babel/traverse" -import t from "@babel/types" -import { assert } from "@samual/lib/assert" -import { clearObject } from "@samual/lib/clearObject" -import { validDBMethods } from "../constants" -import { getReferencePathsToGlobal } from "./shared" +import type { NodePath, Scope } from '@babel/traverse'; +import traverse from '@babel/traverse'; +import type { + ArrayExpression, + Block, + BlockStatement, + CallExpression, + File, + FunctionDeclaration, + Identifier, + Node, + ObjectExpression +} from '@babel/types'; +import t from '@babel/types'; +import type { LaxPartial } from '@samual/lib'; +import { assert } from '@samual/lib/assert'; +import { clearObject } from '@samual/lib/clearObject'; +import { validDBMethods } from '../constants'; +import { getReferencePathsToGlobal } from './shared'; export type TransformOptions = LaxPartial<{ /** 11 a-z 0-9 characters */ uniqueId: string @@ -749,10 +759,19 @@ export function transform( if (object.type == `ObjectExpression`) { for (const property of (object as ObjectExpression).properties) { - if (property.type != `ObjectMethod`) + if (property.type == `ObjectMethod`) { + thisIsReferenced ||= replaceAllThisWith(property, scope, thisId) continue + } + + if (property.type == `ObjectProperty` && property.value.type == `FunctionExpression`) { + // Replace in things like this: function (foo = this.a) + for (const param of property.value.params) { + thisIsReferenced ||= replaceAllThisWith(param, scope, thisId) + } - thisIsReferenced ||= replaceAllThisWith(property, scope, thisId) + thisIsReferenced ||= replaceAllThisWith(property.value.body, scope, thisId) + } } } else { for (const element of (object as ArrayExpression).elements) { From 115debca1c63a7df7ee4e8ff31b08bb6f848eeb4 Mon Sep 17 00:00:00 2001 From: SarahIsWeird Date: Mon, 14 Apr 2025 22:57:31 +0200 Subject: [PATCH 3/6] Fix WebStorm auto-messing-up imports >:( --- src/processScript/transform.ts | 28 +++++++++------------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/src/processScript/transform.ts b/src/processScript/transform.ts index 9724d6e..3d0cc98 100644 --- a/src/processScript/transform.ts +++ b/src/processScript/transform.ts @@ -1,23 +1,13 @@ /* eslint-disable jsdoc/check-param-names */ -import type { NodePath, Scope } from '@babel/traverse'; -import traverse from '@babel/traverse'; -import type { - ArrayExpression, - Block, - BlockStatement, - CallExpression, - File, - FunctionDeclaration, - Identifier, - Node, - ObjectExpression -} from '@babel/types'; -import t from '@babel/types'; -import type { LaxPartial } from '@samual/lib'; -import { assert } from '@samual/lib/assert'; -import { clearObject } from '@samual/lib/clearObject'; -import { validDBMethods } from '../constants'; -import { getReferencePathsToGlobal } from './shared'; +import type { NodePath, Scope } from "@babel/traverse" +import type { ArrayExpression, Block, BlockStatement, CallExpression, File, FunctionDeclaration, Identifier, Node, ObjectExpression } from "@babel/types" +import type { LaxPartial } from "@samual/lib" +import traverse from "@babel/traverse" +import t from "@babel/types" +import { assert } from "@samual/lib/assert" +import { clearObject } from "@samual/lib/clearObject" +import { validDBMethods } from "../constants" +import { getReferencePathsToGlobal } from "./shared" export type TransformOptions = LaxPartial<{ /** 11 a-z 0-9 characters */ uniqueId: string From 67dc9bb8cdc25e10376660d0d593e6949de0ec4f Mon Sep 17 00:00:00 2001 From: SarahIsWeird Date: Mon, 14 Apr 2025 23:00:37 +0200 Subject: [PATCH 4/6] Fix formatting --- .../this_in_lambda_in_object.ts | 40 +++++++++---------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/game-scripts-tests/this_in_lambda_in_object.ts b/game-scripts-tests/this_in_lambda_in_object.ts index 4ee75d4..9bb7d9c 100644 --- a/game-scripts-tests/this_in_lambda_in_object.ts +++ b/game-scripts-tests/this_in_lambda_in_object.ts @@ -1,23 +1,23 @@ export default ({ expect }: typeof import('vitest')) => { - const myObject = { - a: 0, - b: function () { - this.a++ - return this.a - }, - c: function (value: number = this.a) { - this.a = value + 1 - }, - d: function ({ value = this.a }: { value?: number }) { - this.a = value + 1 - } - } + const myObject = { + a: 0, + b: function () { + this.a++ + return this.a + }, + c: function (value: number = this.a) { + this.a = value + 1 + }, + d: function ({ value = this.a }: { value?: number }) { + this.a = value + 1 + } + } - expect(myObject.a).toBe(0) - myObject.b() - expect(myObject.a).toBe(1) - myObject.c() - expect(myObject.a).toBe(2) - myObject.d({}) - expect(myObject.a).toBe(3) + expect(myObject.a).toBe(0) + myObject.b() + expect(myObject.a).toBe(1) + myObject.c() + expect(myObject.a).toBe(2) + myObject.d({}) + expect(myObject.a).toBe(3) } From 0c5554899e76515de810555742c5a28bdca7faa8 Mon Sep 17 00:00:00 2001 From: SarahIsWeird Date: Mon, 14 Apr 2025 23:08:02 +0200 Subject: [PATCH 5/6] Make ESLint happy --- game-scripts-tests/this_in_lambda_in_object.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/game-scripts-tests/this_in_lambda_in_object.ts b/game-scripts-tests/this_in_lambda_in_object.ts index 9bb7d9c..46cb572 100644 --- a/game-scripts-tests/this_in_lambda_in_object.ts +++ b/game-scripts-tests/this_in_lambda_in_object.ts @@ -1,13 +1,17 @@ export default ({ expect }: typeof import('vitest')) => { const myObject = { a: 0, + // Since this is the thing being tested, we need to disable the ESLint rule instead. + /* eslint-disable object-shorthand */ b: function () { this.a++ return this.a }, + /* eslint-disable object-shorthand */ c: function (value: number = this.a) { this.a = value + 1 }, + /* eslint-disable object-shorthand */ d: function ({ value = this.a }: { value?: number }) { this.a = value + 1 } From 6f1e2c51bacab33366bf7a39be126b38b432e2b4 Mon Sep 17 00:00:00 2001 From: SarahIsWeird Date: Mon, 14 Apr 2025 23:09:34 +0200 Subject: [PATCH 6/6] Make ESLint happier --- game-scripts-tests/this_in_lambda_in_object.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/game-scripts-tests/this_in_lambda_in_object.ts b/game-scripts-tests/this_in_lambda_in_object.ts index 46cb572..7aa021a 100644 --- a/game-scripts-tests/this_in_lambda_in_object.ts +++ b/game-scripts-tests/this_in_lambda_in_object.ts @@ -1,17 +1,16 @@ +// Since this is the thing being tested, we need to disable the ESLint rule instead. +/* eslint-disable object-shorthand */ + export default ({ expect }: typeof import('vitest')) => { const myObject = { a: 0, - // Since this is the thing being tested, we need to disable the ESLint rule instead. - /* eslint-disable object-shorthand */ b: function () { this.a++ return this.a }, - /* eslint-disable object-shorthand */ c: function (value: number = this.a) { this.a = value + 1 }, - /* eslint-disable object-shorthand */ d: function ({ value = this.a }: { value?: number }) { this.a = value + 1 }