-
-
Notifications
You must be signed in to change notification settings - Fork 186
Expand file tree
/
Copy pathtypeof.ts
More file actions
60 lines (54 loc) · 2.18 KB
/
Copy pathtypeof.ts
File metadata and controls
60 lines (54 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import * as ts from "typescript";
import * as lua from "../../LuaAST";
import { LuaLibFeature } from "../../LuaLib";
import { FunctionVisitor, TransformationContext } from "../context";
import { transformLuaLibFunction } from "../utils/lualib";
import { transformBinaryOperation } from "./binary-expression";
export const transformTypeOfExpression: FunctionVisitor<ts.TypeOfExpression> = (node, context) => {
const innerExpression = context.transformExpression(node.expression);
return transformLuaLibFunction(context, LuaLibFeature.TypeOf, node, innerExpression);
};
export function transformTypeOfBinaryExpression(
context: TransformationContext,
node: ts.BinaryExpression
): lua.Expression | undefined {
const operator = node.operatorToken.kind;
if (
operator !== ts.SyntaxKind.EqualsEqualsToken &&
operator !== ts.SyntaxKind.EqualsEqualsEqualsToken &&
operator !== ts.SyntaxKind.ExclamationEqualsToken &&
operator !== ts.SyntaxKind.ExclamationEqualsEqualsToken
) {
return;
}
let literalExpression: ts.Expression;
let typeOfExpression: ts.TypeOfExpression;
if (ts.isTypeOfExpression(node.left)) {
typeOfExpression = node.left;
literalExpression = node.right;
} else if (ts.isTypeOfExpression(node.right)) {
typeOfExpression = node.right;
literalExpression = node.left;
} else {
return;
}
const comparedExpression = context.transformExpression(literalExpression);
if (!lua.isStringLiteral(comparedExpression)) return;
if (comparedExpression.value === "object") {
comparedExpression.value = "table";
} else if (comparedExpression.value === "undefined") {
comparedExpression.value = "nil";
}
const innerExpression = context.transformExpression(typeOfExpression.expression);
const typeCall = lua.createCallExpression(lua.createIdentifier("type"), [innerExpression], typeOfExpression);
const { precedingStatements, result } = transformBinaryOperation(
context,
typeCall,
comparedExpression,
[],
operator,
node
);
context.addPrecedingStatements(precedingStatements);
return result;
}