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..7aa021a --- /dev/null +++ b/game-scripts-tests/this_in_lambda_in_object.ts @@ -0,0 +1,26 @@ +// 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, + 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) +} diff --git a/src/processScript/transform.ts b/src/processScript/transform.ts index 609f020..3d0cc98 100644 --- a/src/processScript/transform.ts +++ b/src/processScript/transform.ts @@ -749,10 +749,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) {