diff --git a/src/transformation/visitors/optional-chaining.ts b/src/transformation/visitors/optional-chaining.ts index 893add0f0..6d6deeb8d 100644 --- a/src/transformation/visitors/optional-chaining.ts +++ b/src/transformation/visitors/optional-chaining.ts @@ -166,6 +166,19 @@ export function transformOptionalChainWithCapture( return result; } ); + + // handle super calls by passing self as context + function getLeftMostChainItem(node: ts.Node): ts.Node { + if (ts.isPropertyAccessExpression(node)) { + return getLeftMostChainItem(node.expression); + } else { + return node; + } + } + if (getLeftMostChainItem(tsLeftExpression).kind === ts.SyntaxKind.SuperKeyword) { + capturedThisValue = lua.createIdentifier("self"); + } + // handle context if (rightContextualCall) { if (capturedThisValue) { diff --git a/test/unit/optionalChaining.spec.ts b/test/unit/optionalChaining.spec.ts index 8a30e1647..d99f18be9 100644 --- a/test/unit/optionalChaining.spec.ts +++ b/test/unit/optionalChaining.spec.ts @@ -438,3 +438,22 @@ describe("Non-null chain", () => { `.expectToMatchJsResult(); }); }); + +// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1585 +test("optional chaining of super call (#1585)", () => { + util.testFunction` + class Parent { + private name = "foo"; + M2() { return this.name; } + } + + class Child extends Parent { + M2() { + return super.M2?.(); + } + } + + const c = new Child(); + return c.M2(); + `.expectToMatchJsResult(); +});