Skip to content

Commit 3cced7d

Browse files
authored
Transform String(x) to tostring(x) (#1602)
1 parent 7c6c729 commit 3cced7d

File tree

4 files changed

+29
-3
lines changed

4 files changed

+29
-3
lines changed

src/transformation/builtins/global.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { TransformationContext } from "../context";
44
import { LuaLibFeature, transformLuaLibFunction } from "../utils/lualib";
55
import { isNumberType } from "../utils/typescript";
66
import { transformArguments } from "../visitors/call";
7+
import { transformStringConstructorCall } from "./string";
78

89
export function tryTransformBuiltinGlobalCall(
910
context: TransformationContext,
@@ -21,6 +22,8 @@ export function tryTransformBuiltinGlobalCall(
2122
return transformLuaLibFunction(context, LuaLibFeature.Symbol, node, ...getParameters());
2223
case "NumberConstructor":
2324
return transformLuaLibFunction(context, LuaLibFeature.Number, node, ...getParameters());
25+
case "StringConstructor":
26+
return transformStringConstructorCall(node, ...getParameters());
2427
case "isNaN":
2528
case "isFinite":
2629
const numberParameters = isNumberType(context, expressionType)

src/transformation/builtins/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { transformMathCall, transformMathProperty } from "./math";
1414
import { transformNumberConstructorCall, transformNumberPrototypeCall, transformNumberProperty } from "./number";
1515
import { transformObjectConstructorCall, tryTransformObjectPrototypeCall } from "./object";
1616
import { transformPromiseConstructorCall } from "./promise";
17-
import { transformStringConstructorCall, transformStringProperty, transformStringPrototypeCall } from "./string";
17+
import { transformStringConstructorMethodCall, transformStringProperty, transformStringPrototypeCall } from "./string";
1818
import { transformSymbolConstructorCall } from "./symbol";
1919
import { unsupportedBuiltinOptionalCall } from "../utils/diagnostics";
2020
import { LuaTarget } from "../../CompilerOptions";
@@ -101,7 +101,7 @@ function tryTransformBuiltinGlobalMethodCall(
101101
result = transformMathCall(context, node, calledMethod);
102102
break;
103103
case "StringConstructor":
104-
result = transformStringConstructorCall(context, node, calledMethod);
104+
result = transformStringConstructorMethodCall(context, node, calledMethod);
105105
break;
106106
case "ObjectConstructor":
107107
result = transformObjectConstructorCall(context, node, calledMethod);

src/transformation/builtins/string.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ export function transformStringPrototypeCall(
160160
}
161161
}
162162

163-
export function transformStringConstructorCall(
163+
export function transformStringConstructorMethodCall(
164164
context: TransformationContext,
165165
node: ts.CallExpression,
166166
calledMethod: ts.PropertyAccessExpression
@@ -202,3 +202,11 @@ export function transformStringProperty(
202202
context.diagnostics.push(unsupportedProperty(node.name, "string", node.name.text));
203203
}
204204
}
205+
206+
export function transformStringConstructorCall(
207+
originalNode: ts.CallExpression,
208+
...args: lua.Expression[]
209+
): lua.Expression | undefined {
210+
const tostring = lua.createIdentifier("tostring", originalNode.expression);
211+
return lua.createCallExpression(tostring, args, originalNode);
212+
}

test/unit/builtins/string.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,21 @@ test("string intersected method", () => {
372372
`.expectToMatchJsResult();
373373
});
374374

375+
test("tostring number with String constructor", () => {
376+
util.testFunction`
377+
const n = 123
378+
return "abc:" + String(n);
379+
`.expectToEqual("abc:123");
380+
});
381+
382+
test("tostring table with String constructor", () => {
383+
const result = util.testFunction`
384+
const t = {}
385+
return "abc:" + String(t);
386+
`.getLuaExecutionResult();
387+
expect(result).toContain("abc:table: 0x");
388+
});
389+
375390
// Issue #1218: https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1218
376391
test.each(['"foo"', "undefined"])("prototype call on nullable string (%p)", value => {
377392
util.testFunction`

0 commit comments

Comments
 (0)