Skip to content

Convert unary + or - expressions to numbers #1635

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 24, 2025
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
35 changes: 25 additions & 10 deletions src/transformation/visitors/unary-expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
transformCompoundAssignmentExpression,
transformCompoundAssignmentStatement,
} from "./binary-expression/compound";
import { isNumberType } from "../utils/typescript";
import { LuaLibFeature, transformLuaLibFunction } from "../utils/lualib";

export function transformUnaryExpressionStatement(
context: TransformationContext,
Expand Down Expand Up @@ -92,16 +94,29 @@ export const transformPrefixUnaryExpression: FunctionVisitor<ts.PrefixUnaryExpre
false
);

case ts.SyntaxKind.PlusToken:
// TODO: Wrap with `Number`
return context.transformExpression(expression.operand);

case ts.SyntaxKind.MinusToken:
return lua.createUnaryExpression(
context.transformExpression(expression.operand),
lua.SyntaxKind.NegationOperator
);

case ts.SyntaxKind.PlusToken: {
const operand = context.transformExpression(expression.operand);
const type = context.checker.getTypeAtLocation(expression.operand);
if (isNumberType(context, type)) {
return operand;
} else {
return transformLuaLibFunction(context, LuaLibFeature.Number, expression, operand);
}
}
case ts.SyntaxKind.MinusToken: {
const operand = context.transformExpression(expression.operand);
const type = context.checker.getTypeAtLocation(expression.operand);
if (isNumberType(context, type)) {
return lua.createUnaryExpression(operand, lua.SyntaxKind.NegationOperator);
} else {
return transformLuaLibFunction(
context,
LuaLibFeature.Number,
expression,
lua.createUnaryExpression(operand, lua.SyntaxKind.NegationOperator)
);
}
}
case ts.SyntaxKind.ExclamationToken:
return lua.createUnaryExpression(
context.transformExpression(expression.operand),
Expand Down
12 changes: 8 additions & 4 deletions test/unit/__snapshots__/expressions.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -589,9 +589,11 @@ return ____exports"
`;

exports[`Unary expressions basic ("+a") 1`] = `
"local ____exports = {}
"local ____lualib = require("lualib_bundle")
local __TS__Number = ____lualib.__TS__Number
local ____exports = {}
function ____exports.__main(self)
local ____ = a
__TS__Number(a)
end
return ____exports"
`;
Expand All @@ -605,9 +607,11 @@ return ____exports"
`;

exports[`Unary expressions basic ("-a") 1`] = `
"local ____exports = {}
"local ____lualib = require("lualib_bundle")
local __TS__Number = ____lualib.__TS__Number
local ____exports = {}
function ____exports.__main(self)
local ____ = -a
__TS__Number(-a)
end
return ____exports"
`;
Expand Down
15 changes: 15 additions & 0 deletions test/unit/builtins/numbers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,18 @@ test.each([
])("Numer constants have correct relative sizes (%p)", comparison => {
util.testExpression(comparison).expectToEqual(true);
});

// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1629
test("unary + casting to number (#1629)", () => {
util.testFunction`
let abc = "123";
return [+abc, +"456"];
`.expectToEqual([123, 456]);
});

test("unary - casting to number", () => {
util.testFunction`
let abc = "123";
return [-abc, -"456"];
`.expectToEqual([-123, -456]);
});
Loading