Skip to content

Commit b0ef3b0

Browse files
authored
Fix context failed to be resolved due to parentheses (#1095)
* Fix context failed to be resolved due to parentheses * Added test with more parentheses
1 parent f6fea1e commit b0ef3b0

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

src/transpilation/transformers.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export function getTransformers(
3636

3737
...(transformersFromOptions.after ?? []),
3838
...(customTransformers.after ?? []),
39+
stripParenthesisExpressionsTransformer,
3940
luaTransformer,
4041
],
4142
};
@@ -53,6 +54,30 @@ export const noImplicitSelfTransformer: ts.TransformerFactory<ts.SourceFile | ts
5354
: transformSourceFile(node);
5455
};
5556

57+
export const stripParenthesisExpressionsTransformer: ts.TransformerFactory<ts.SourceFile> = context => sourceFile => {
58+
// Remove parenthesis expressions before transforming to Lua, so transpiler is not hindered by extra ParenthesizedExpression nodes
59+
function unwrapParentheses(node: ts.Expression) {
60+
while (ts.isParenthesizedExpression(node)) {
61+
node = node.expression;
62+
}
63+
return node;
64+
}
65+
function visit(node: ts.Node): ts.Node {
66+
// For now only call expressions strip their expressions of parentheses, there could be more cases where this is required
67+
if (ts.isCallExpression(node)) {
68+
return ts.factory.updateCallExpression(
69+
node,
70+
unwrapParentheses(node.expression),
71+
node.typeArguments,
72+
node.arguments
73+
);
74+
}
75+
76+
return ts.visitEachChild(node, visit, context);
77+
}
78+
return ts.visitNode(sourceFile, visit);
79+
};
80+
5681
function loadTransformersFromOptions(program: ts.Program, diagnostics: ts.Diagnostic[]): ts.CustomTransformers {
5782
const customTransformers: Required<ts.CustomTransformers> = {
5883
before: [],

test/unit/classes/classes.spec.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,3 +761,41 @@ test("constructor class name available with constructor", () => {
761761
.setReturnExport("className")
762762
.expectToEqual("MyClass");
763763
});
764+
765+
// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/959
766+
test("methods accessed via this index pass correct context", () => {
767+
util.testModule`
768+
class Example {
769+
baz = 3;
770+
foo() {
771+
this["bar"]()
772+
}
773+
774+
bar() {
775+
return this.baz;
776+
}
777+
}
778+
779+
const inst = new Example();
780+
export const result = inst.foo();
781+
`.expectToMatchJsResult();
782+
});
783+
784+
// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/959
785+
test.each(['(this["bar"])', '((((this["bar"]))))'])("methods in parentheses pass correct context %s", callPath => {
786+
util.testModule`
787+
class Example {
788+
baz = 3;
789+
foo() {
790+
${callPath}()
791+
}
792+
793+
bar() {
794+
return this.baz;
795+
}
796+
}
797+
798+
const inst = new Example();
799+
export const result = inst.foo();
800+
`.expectToMatchJsResult();
801+
});

0 commit comments

Comments
 (0)