diff --git a/src/transformation/visitors/call.ts b/src/transformation/visitors/call.ts index bd8099afa..24c4864d1 100644 --- a/src/transformation/visitors/call.ts +++ b/src/transformation/visitors/call.ts @@ -10,7 +10,6 @@ import { LuaLibFeature, transformLuaLibFunction } from "../utils/lualib"; import { isValidLuaIdentifier } from "../utils/safe-names"; import { isArrayType, isExpressionWithEvaluationEffect, isInDestructingAssignment } from "../utils/typescript"; import { transformElementAccessArgument } from "./access"; -import { transformIdentifier } from "./identifier"; import { transformLuaTableCallExpression } from "./lua-table"; export type PropertyCallExpression = ts.CallExpression & { expression: ts.PropertyAccessExpression }; @@ -117,7 +116,7 @@ export function transformContextualCallExpression( return lua.createMethodCallExpression( table, - transformIdentifier(context, left.name), + lua.createIdentifier(left.name.text, left.name), transformedArguments, node ); diff --git a/src/transformation/visitors/class/members/constructor.ts b/src/transformation/visitors/class/members/constructor.ts index d0c611465..fe25a21b2 100644 --- a/src/transformation/visitors/class/members/constructor.ts +++ b/src/transformation/visitors/class/members/constructor.ts @@ -1,11 +1,11 @@ import * as ts from "typescript"; import * as lua from "../../../../LuaAST"; -import { createSelfIdentifier } from "../../../utils/lua-ast"; -import { transformParameters, transformFunctionBodyStatements, transformFunctionBodyHeader } from "../../function"; import { TransformationContext } from "../../../context"; +import { createSelfIdentifier } from "../../../utils/lua-ast"; +import { popScope, pushScope, ScopeType } from "../../../utils/scope"; +import { transformFunctionBodyHeader, transformFunctionBodyStatements, transformParameters } from "../../function"; import { transformIdentifier } from "../../identifier"; import { transformClassInstanceFields } from "./fields"; -import { pushScope, ScopeType, popScope } from "../../../utils/scope"; export function createConstructorName(className: lua.Identifier): lua.TableIndexExpression { return lua.createTableIndexExpression( @@ -66,13 +66,15 @@ export function transformConstructorDeclaration( // Add in instance field declarations for (const declaration of constructorFieldsDeclarations) { - const declarationName = transformIdentifier(context, declaration.name as ts.Identifier); - // self.declarationName = declarationName - const assignment = lua.createAssignmentStatement( - lua.createTableIndexExpression(createSelfIdentifier(), lua.createStringLiteral(declarationName.text)), - declarationName - ); - bodyWithFieldInitializers.push(assignment); + if (ts.isIdentifier(declaration.name)) { + // self.declarationName = declarationName + const assignment = lua.createAssignmentStatement( + lua.createTableIndexExpression(createSelfIdentifier(), lua.createStringLiteral(declaration.name.text)), + transformIdentifier(context, declaration.name) + ); + bodyWithFieldInitializers.push(assignment); + } + // else { TypeScript error: A parameter property may not be declared using a binding pattern } } bodyWithFieldInitializers.push(...classInstanceFields); diff --git a/test/unit/identifiers.spec.ts b/test/unit/identifiers.spec.ts index 84583ccd7..27046c81b 100644 --- a/test/unit/identifiers.spec.ts +++ b/test/unit/identifiers.spec.ts @@ -757,3 +757,32 @@ test("exported variable with lua keyword as name is not renamed", () => { expect(util.transpileExecuteAndReturnExport(code, "print")).toBe("foobar"); }); + +// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/846 +test("lua built-in as class method", () => { + util.testModule` + class MyClass { + error() { return "Error!"; } + } + export const result = new MyClass().error(); + `.expectToMatchJsResult(); +}); + +// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/833 +test("lua built-in as object method", () => { + util.testModule` + const obj = { error: () => "Error!" }; + export const result = obj.error(); + `.expectToMatchJsResult(); +}); + +// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/789 +test("lua built-in as in constructor assignment", () => { + util.testModule` + class A { + constructor(public error: string){} + } + + export const result = new A("42").error; + `.expectToMatchJsResult(); +});