Skip to content

Commit 9e05143

Browse files
authored
Fix incorrect context passing when super is used in contextual call (#1587)
1 parent a93ac8b commit 9e05143

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/transformation/visitors/optional-chaining.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,19 @@ export function transformOptionalChainWithCapture(
166166
return result;
167167
}
168168
);
169+
170+
// handle super calls by passing self as context
171+
function getLeftMostChainItem(node: ts.Node): ts.Node {
172+
if (ts.isPropertyAccessExpression(node)) {
173+
return getLeftMostChainItem(node.expression);
174+
} else {
175+
return node;
176+
}
177+
}
178+
if (getLeftMostChainItem(tsLeftExpression).kind === ts.SyntaxKind.SuperKeyword) {
179+
capturedThisValue = lua.createIdentifier("self");
180+
}
181+
169182
// handle context
170183
if (rightContextualCall) {
171184
if (capturedThisValue) {

test/unit/optionalChaining.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,3 +438,22 @@ describe("Non-null chain", () => {
438438
`.expectToMatchJsResult();
439439
});
440440
});
441+
442+
// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1585
443+
test("optional chaining of super call (#1585)", () => {
444+
util.testFunction`
445+
class Parent {
446+
private name = "foo";
447+
M2() { return this.name; }
448+
}
449+
450+
class Child extends Parent {
451+
M2() {
452+
return super.M2?.();
453+
}
454+
}
455+
456+
const c = new Child();
457+
return c.M2();
458+
`.expectToMatchJsResult();
459+
});

0 commit comments

Comments
 (0)