Skip to content

Commit ec84670

Browse files
authored
Convert unary + or - expressions to numbers (#1635)
1 parent 54d7ac3 commit ec84670

3 files changed

Lines changed: 48 additions & 14 deletions

File tree

src/transformation/visitors/unary-expression.ts

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import {
77
transformCompoundAssignmentExpression,
88
transformCompoundAssignmentStatement,
99
} from "./binary-expression/compound";
10+
import { isNumberType } from "../utils/typescript";
11+
import { LuaLibFeature, transformLuaLibFunction } from "../utils/lualib";
1012

1113
export function transformUnaryExpressionStatement(
1214
context: TransformationContext,
@@ -92,16 +94,29 @@ export const transformPrefixUnaryExpression: FunctionVisitor<ts.PrefixUnaryExpre
9294
false
9395
);
9496

95-
case ts.SyntaxKind.PlusToken:
96-
// TODO: Wrap with `Number`
97-
return context.transformExpression(expression.operand);
98-
99-
case ts.SyntaxKind.MinusToken:
100-
return lua.createUnaryExpression(
101-
context.transformExpression(expression.operand),
102-
lua.SyntaxKind.NegationOperator
103-
);
104-
97+
case ts.SyntaxKind.PlusToken: {
98+
const operand = context.transformExpression(expression.operand);
99+
const type = context.checker.getTypeAtLocation(expression.operand);
100+
if (isNumberType(context, type)) {
101+
return operand;
102+
} else {
103+
return transformLuaLibFunction(context, LuaLibFeature.Number, expression, operand);
104+
}
105+
}
106+
case ts.SyntaxKind.MinusToken: {
107+
const operand = context.transformExpression(expression.operand);
108+
const type = context.checker.getTypeAtLocation(expression.operand);
109+
if (isNumberType(context, type)) {
110+
return lua.createUnaryExpression(operand, lua.SyntaxKind.NegationOperator);
111+
} else {
112+
return transformLuaLibFunction(
113+
context,
114+
LuaLibFeature.Number,
115+
expression,
116+
lua.createUnaryExpression(operand, lua.SyntaxKind.NegationOperator)
117+
);
118+
}
119+
}
105120
case ts.SyntaxKind.ExclamationToken:
106121
return lua.createUnaryExpression(
107122
context.transformExpression(expression.operand),

test/unit/__snapshots__/expressions.spec.ts.snap

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -589,9 +589,11 @@ return ____exports"
589589
`;
590590
591591
exports[`Unary expressions basic ("+a") 1`] = `
592-
"local ____exports = {}
592+
"local ____lualib = require("lualib_bundle")
593+
local __TS__Number = ____lualib.__TS__Number
594+
local ____exports = {}
593595
function ____exports.__main(self)
594-
local ____ = a
596+
__TS__Number(a)
595597
end
596598
return ____exports"
597599
`;
@@ -605,9 +607,11 @@ return ____exports"
605607
`;
606608
607609
exports[`Unary expressions basic ("-a") 1`] = `
608-
"local ____exports = {}
610+
"local ____lualib = require("lualib_bundle")
611+
local __TS__Number = ____lualib.__TS__Number
612+
local ____exports = {}
609613
function ____exports.__main(self)
610-
local ____ = -a
614+
__TS__Number(-a)
611615
end
612616
return ____exports"
613617
`;

test/unit/builtins/numbers.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,3 +224,18 @@ test.each([
224224
])("Numer constants have correct relative sizes (%p)", comparison => {
225225
util.testExpression(comparison).expectToEqual(true);
226226
});
227+
228+
// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1629
229+
test("unary + casting to number (#1629)", () => {
230+
util.testFunction`
231+
let abc = "123";
232+
return [+abc, +"456"];
233+
`.expectToEqual([123, 456]);
234+
});
235+
236+
test("unary - casting to number", () => {
237+
util.testFunction`
238+
let abc = "123";
239+
return [-abc, -"456"];
240+
`.expectToEqual([-123, -456]);
241+
});

0 commit comments

Comments
 (0)