From 453ca030f7c252ec5b082b5e5ec09f11af1e02b0 Mon Sep 17 00:00:00 2001 From: Perryvw Date: Thu, 19 Aug 2021 22:12:29 +0200 Subject: [PATCH 1/2] Fix context failed to be resolved due to parentheses --- src/transpilation/transformers.ts | 25 ++++++++++++++++++++ test/unit/classes/classes.spec.ts | 38 +++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/src/transpilation/transformers.ts b/src/transpilation/transformers.ts index e043704b8..7b5aacddd 100644 --- a/src/transpilation/transformers.ts +++ b/src/transpilation/transformers.ts @@ -36,6 +36,7 @@ export function getTransformers( ...(transformersFromOptions.after ?? []), ...(customTransformers.after ?? []), + stripParenthesisExpressionsTransformer, luaTransformer, ], }; @@ -53,6 +54,30 @@ export const noImplicitSelfTransformer: ts.TransformerFactory = context => sourceFile => { + // Remove parenthesis expressions before transforming to Lua, so transpiler is not hindered by extra ParenthesizedExpression nodes + function unwrapParentheses(node: ts.Expression) { + while (ts.isParenthesizedExpression(node)) { + node = node.expression; + } + return node; + } + function visit(node: ts.Node): ts.Node { + // For now only call expressions strip their expressions of parentheses, there could be more cases where this is required + if (ts.isCallExpression(node)) { + return ts.factory.updateCallExpression( + node, + unwrapParentheses(node.expression), + node.typeArguments, + node.arguments + ); + } + + return ts.visitEachChild(node, visit, context); + } + return ts.visitNode(sourceFile, visit); +}; + function loadTransformersFromOptions(program: ts.Program, diagnostics: ts.Diagnostic[]): ts.CustomTransformers { const customTransformers: Required = { before: [], diff --git a/test/unit/classes/classes.spec.ts b/test/unit/classes/classes.spec.ts index f4c14df34..3840dd31d 100644 --- a/test/unit/classes/classes.spec.ts +++ b/test/unit/classes/classes.spec.ts @@ -761,3 +761,41 @@ test("constructor class name available with constructor", () => { .setReturnExport("className") .expectToEqual("MyClass"); }); + +// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/959 +test("methods accessed via this index pass correct context", () => { + util.testModule` + class Example { + baz = 3; + foo() { + this["bar"]() + } + + bar() { + return this.baz; + } + } + + const inst = new Example(); + export const result = inst.foo(); + `.expectToMatchJsResult(); +}); + +// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/959 +test("methods in parentheses pass correct context", () => { + util.testModule` + class Example { + baz = 3; + foo() { + (this["bar"])() + } + + bar() { + return this.baz; + } + } + + const inst = new Example(); + export const result = inst.foo(); + `.expectToMatchJsResult(); +}); From ebb7831249a31125ef04cd056d140045a470a3b8 Mon Sep 17 00:00:00 2001 From: Perryvw Date: Fri, 20 Aug 2021 12:51:49 +0200 Subject: [PATCH 2/2] Added test with more parentheses --- test/unit/classes/classes.spec.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/unit/classes/classes.spec.ts b/test/unit/classes/classes.spec.ts index 3840dd31d..f9ab4fe61 100644 --- a/test/unit/classes/classes.spec.ts +++ b/test/unit/classes/classes.spec.ts @@ -782,12 +782,12 @@ test("methods accessed via this index pass correct context", () => { }); // https://github.com/TypeScriptToLua/TypeScriptToLua/issues/959 -test("methods in parentheses pass correct context", () => { +test.each(['(this["bar"])', '((((this["bar"]))))'])("methods in parentheses pass correct context %s", callPath => { util.testModule` class Example { baz = 3; foo() { - (this["bar"])() + ${callPath}() } bar() {