Skip to content

Commit f366595

Browse files
authored
Fixed incorrect escaping of fields (#847)
* Fixed incorrect escaping of fields * Removed forgotten debug() and moved comment around * Updated test for invalid syntax identifiers * Reverted test changes
1 parent cd23caf commit f366595

File tree

3 files changed

+42
-12
lines changed

3 files changed

+42
-12
lines changed

src/transformation/visitors/call.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import { LuaLibFeature, transformLuaLibFunction } from "../utils/lualib";
1010
import { isValidLuaIdentifier } from "../utils/safe-names";
1111
import { isArrayType, isExpressionWithEvaluationEffect, isInDestructingAssignment } from "../utils/typescript";
1212
import { transformElementAccessArgument } from "./access";
13-
import { transformIdentifier } from "./identifier";
1413
import { transformLuaTableCallExpression } from "./lua-table";
1514

1615
export type PropertyCallExpression = ts.CallExpression & { expression: ts.PropertyAccessExpression };
@@ -117,7 +116,7 @@ export function transformContextualCallExpression(
117116

118117
return lua.createMethodCallExpression(
119118
table,
120-
transformIdentifier(context, left.name),
119+
lua.createIdentifier(left.name.text, left.name),
121120
transformedArguments,
122121
node
123122
);

src/transformation/visitors/class/members/constructor.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import * as ts from "typescript";
22
import * as lua from "../../../../LuaAST";
3-
import { createSelfIdentifier } from "../../../utils/lua-ast";
4-
import { transformParameters, transformFunctionBodyStatements, transformFunctionBodyHeader } from "../../function";
53
import { TransformationContext } from "../../../context";
4+
import { createSelfIdentifier } from "../../../utils/lua-ast";
5+
import { popScope, pushScope, ScopeType } from "../../../utils/scope";
6+
import { transformFunctionBodyHeader, transformFunctionBodyStatements, transformParameters } from "../../function";
67
import { transformIdentifier } from "../../identifier";
78
import { transformClassInstanceFields } from "./fields";
8-
import { pushScope, ScopeType, popScope } from "../../../utils/scope";
99

1010
export function createConstructorName(className: lua.Identifier): lua.TableIndexExpression {
1111
return lua.createTableIndexExpression(
@@ -66,13 +66,15 @@ export function transformConstructorDeclaration(
6666

6767
// Add in instance field declarations
6868
for (const declaration of constructorFieldsDeclarations) {
69-
const declarationName = transformIdentifier(context, declaration.name as ts.Identifier);
70-
// self.declarationName = declarationName
71-
const assignment = lua.createAssignmentStatement(
72-
lua.createTableIndexExpression(createSelfIdentifier(), lua.createStringLiteral(declarationName.text)),
73-
declarationName
74-
);
75-
bodyWithFieldInitializers.push(assignment);
69+
if (ts.isIdentifier(declaration.name)) {
70+
// self.declarationName = declarationName
71+
const assignment = lua.createAssignmentStatement(
72+
lua.createTableIndexExpression(createSelfIdentifier(), lua.createStringLiteral(declaration.name.text)),
73+
transformIdentifier(context, declaration.name)
74+
);
75+
bodyWithFieldInitializers.push(assignment);
76+
}
77+
// else { TypeScript error: A parameter property may not be declared using a binding pattern }
7678
}
7779

7880
bodyWithFieldInitializers.push(...classInstanceFields);

test/unit/identifiers.spec.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,3 +757,32 @@ test("exported variable with lua keyword as name is not renamed", () => {
757757

758758
expect(util.transpileExecuteAndReturnExport(code, "print")).toBe("foobar");
759759
});
760+
761+
// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/846
762+
test("lua built-in as class method", () => {
763+
util.testModule`
764+
class MyClass {
765+
error() { return "Error!"; }
766+
}
767+
export const result = new MyClass().error();
768+
`.expectToMatchJsResult();
769+
});
770+
771+
// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/833
772+
test("lua built-in as object method", () => {
773+
util.testModule`
774+
const obj = { error: () => "Error!" };
775+
export const result = obj.error();
776+
`.expectToMatchJsResult();
777+
});
778+
779+
// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/789
780+
test("lua built-in as in constructor assignment", () => {
781+
util.testModule`
782+
class A {
783+
constructor(public error: string){}
784+
}
785+
786+
export const result = new A("42").error;
787+
`.expectToMatchJsResult();
788+
});

0 commit comments

Comments
 (0)