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: 3 additions & 0 deletions src/transformation/builtins/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { TransformationContext } from "../context";
import { LuaLibFeature, transformLuaLibFunction } from "../utils/lualib";
import { isNumberType } from "../utils/typescript";
import { transformArguments } from "../visitors/call";
import { transformStringConstructorCall } from "./string";

export function tryTransformBuiltinGlobalCall(
context: TransformationContext,
Expand All @@ -21,6 +22,8 @@ export function tryTransformBuiltinGlobalCall(
return transformLuaLibFunction(context, LuaLibFeature.Symbol, node, ...getParameters());
case "NumberConstructor":
return transformLuaLibFunction(context, LuaLibFeature.Number, node, ...getParameters());
case "StringConstructor":
return transformStringConstructorCall(node, ...getParameters());
case "isNaN":
case "isFinite":
const numberParameters = isNumberType(context, expressionType)
Expand Down
4 changes: 2 additions & 2 deletions src/transformation/builtins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { transformMathCall, transformMathProperty } from "./math";
import { transformNumberConstructorCall, transformNumberPrototypeCall, transformNumberProperty } from "./number";
import { transformObjectConstructorCall, tryTransformObjectPrototypeCall } from "./object";
import { transformPromiseConstructorCall } from "./promise";
import { transformStringConstructorCall, transformStringProperty, transformStringPrototypeCall } from "./string";
import { transformStringConstructorMethodCall, transformStringProperty, transformStringPrototypeCall } from "./string";
import { transformSymbolConstructorCall } from "./symbol";
import { unsupportedBuiltinOptionalCall } from "../utils/diagnostics";
import { LuaTarget } from "../../CompilerOptions";
Expand Down Expand Up @@ -101,7 +101,7 @@ function tryTransformBuiltinGlobalMethodCall(
result = transformMathCall(context, node, calledMethod);
break;
case "StringConstructor":
result = transformStringConstructorCall(context, node, calledMethod);
result = transformStringConstructorMethodCall(context, node, calledMethod);
break;
case "ObjectConstructor":
result = transformObjectConstructorCall(context, node, calledMethod);
Expand Down
10 changes: 9 additions & 1 deletion src/transformation/builtins/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export function transformStringPrototypeCall(
}
}

export function transformStringConstructorCall(
export function transformStringConstructorMethodCall(
context: TransformationContext,
node: ts.CallExpression,
calledMethod: ts.PropertyAccessExpression
Expand Down Expand Up @@ -202,3 +202,11 @@ export function transformStringProperty(
context.diagnostics.push(unsupportedProperty(node.name, "string", node.name.text));
}
}

export function transformStringConstructorCall(
originalNode: ts.CallExpression,
...args: lua.Expression[]
): lua.Expression | undefined {
const tostring = lua.createIdentifier("tostring", originalNode.expression);
return lua.createCallExpression(tostring, args, originalNode);
}
15 changes: 15 additions & 0 deletions test/unit/builtins/string.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,21 @@ test("string intersected method", () => {
`.expectToMatchJsResult();
});

test("tostring number with String constructor", () => {
util.testFunction`
const n = 123
return "abc:" + String(n);
`.expectToEqual("abc:123");
});

test("tostring table with String constructor", () => {
const result = util.testFunction`
const t = {}
return "abc:" + String(t);
`.getLuaExecutionResult();
expect(result).toContain("abc:table: 0x");
});

// Issue #1218: https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1218
test.each(['"foo"', "undefined"])("prototype call on nullable string (%p)", value => {
util.testFunction`
Expand Down