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
13 changes: 13 additions & 0 deletions src/transformation/visitors/optional-chaining.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
19 changes: 19 additions & 0 deletions test/unit/optionalChaining.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});