|
7 | 7 | transformCompoundAssignmentExpression, |
8 | 8 | transformCompoundAssignmentStatement, |
9 | 9 | } from "./binary-expression/compound"; |
| 10 | +import { isNumberType } from "../utils/typescript"; |
| 11 | +import { LuaLibFeature, transformLuaLibFunction } from "../utils/lualib"; |
10 | 12 |
|
11 | 13 | export function transformUnaryExpressionStatement( |
12 | 14 | context: TransformationContext, |
@@ -92,16 +94,29 @@ export const transformPrefixUnaryExpression: FunctionVisitor<ts.PrefixUnaryExpre |
92 | 94 | false |
93 | 95 | ); |
94 | 96 |
|
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 | + } |
105 | 120 | case ts.SyntaxKind.ExclamationToken: |
106 | 121 | return lua.createUnaryExpression( |
107 | 122 | context.transformExpression(expression.operand), |
|
0 commit comments