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
3 changes: 1 addition & 2 deletions src/transformation/visitors/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down Expand Up @@ -117,7 +116,7 @@ export function transformContextualCallExpression(

return lua.createMethodCallExpression(
table,
transformIdentifier(context, left.name),
lua.createIdentifier(left.name.text, left.name),
transformedArguments,
node
);
Expand Down
22 changes: 12 additions & 10 deletions src/transformation/visitors/class/members/constructor.ts
Original file line number Diff line number Diff line change
@@ -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(
Expand Down Expand Up @@ -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);
Expand Down
29 changes: 29 additions & 0 deletions test/unit/identifiers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});