-
-
Notifications
You must be signed in to change notification settings - Fork 186
Expand file tree
/
Copy pathdelete.ts
More file actions
40 lines (33 loc) · 1.99 KB
/
Copy pathdelete.ts
File metadata and controls
40 lines (33 loc) · 1.99 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
import * as ts from "typescript";
import * as lua from "../../LuaAST";
import { FunctionVisitor } from "../context";
import { transformLuaLibFunction, LuaLibFeature } from "../utils/lualib";
import { unsupportedProperty } from "../utils/diagnostics";
import { isArrayType, isNumberType } from "../utils/typescript";
import { addToNumericExpression } from "../utils/lua-ast";
import { transformOptionalDeleteExpression } from "./optional-chaining";
export const transformDeleteExpression: FunctionVisitor<ts.DeleteExpression> = (node, context) => {
if (ts.isOptionalChain(node.expression)) {
return transformOptionalDeleteExpression(context, node, node.expression);
}
let ownerExpression: lua.Expression | undefined;
let propertyExpression: lua.Expression | undefined;
if (ts.isPropertyAccessExpression(node.expression)) {
if (ts.isPrivateIdentifier(node.expression.name)) throw new Error("PrivateIdentifier is not supported");
ownerExpression = context.transformExpression(node.expression.expression);
propertyExpression = lua.createStringLiteral(node.expression.name.text);
} else if (ts.isElementAccessExpression(node.expression)) {
ownerExpression = context.transformExpression(node.expression.expression);
propertyExpression = context.transformExpression(node.expression.argumentExpression);
const type = context.checker.getTypeAtLocation(node.expression.expression);
const argumentType = context.checker.getTypeAtLocation(node.expression.argumentExpression);
if (isArrayType(context, type) && isNumberType(context, argumentType)) {
propertyExpression = addToNumericExpression(propertyExpression, 1);
}
}
if (!ownerExpression || !propertyExpression) {
context.diagnostics.push(unsupportedProperty(node, "delete", ts.SyntaxKind[node.kind]));
return lua.createNilLiteral();
}
return transformLuaLibFunction(context, LuaLibFeature.Delete, node, ownerExpression, propertyExpression);
};