Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions game-scripts-tests/this_in_default_argument.ts
Original file line number Diff line number Diff line change
@@ -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
},
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
}
13 changes: 13 additions & 0 deletions src/processScript/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down