-
-
Notifications
You must be signed in to change notification settings - Fork 185
Expand file tree
/
Copy pathenum.ts
More file actions
95 lines (84 loc) · 4.09 KB
/
enum.ts
File metadata and controls
95 lines (84 loc) · 4.09 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import * as ts from "typescript";
import * as lua from "../../LuaAST";
import { FunctionVisitor, TransformationContext } from "../context";
import { AnnotationKind, getTypeAnnotations } from "../utils/annotations";
import { addExportToIdentifier, getSymbolExportScope } from "../utils/export";
import { createLocalOrExportedOrGlobalDeclaration } from "../utils/lua-ast";
import { isFirstDeclaration } from "../utils/typescript";
import { transformIdentifier } from "./identifier";
import { transformPropertyName } from "./literal";
export function tryGetConstEnumValue(
context: TransformationContext,
node: ts.EnumMember | ts.PropertyAccessExpression | ts.ElementAccessExpression
): lua.Expression | undefined {
const value = context.checker.getConstantValue(node);
if (typeof value === "string") {
return lua.createStringLiteral(value, node);
} else if (typeof value === "number") {
return lua.createNumericLiteral(value, node);
}
}
export const transformEnumDeclaration: FunctionVisitor<ts.EnumDeclaration> = (node, context) => {
if (ts.getCombinedModifierFlags(node) & ts.ModifierFlags.Const && !context.options.preserveConstEnums) {
return undefined;
}
const type = context.checker.getTypeAtLocation(node);
const membersOnly = getTypeAnnotations(type).has(AnnotationKind.CompileMembersOnly);
const result: lua.Statement[] = [];
if (!membersOnly && isFirstDeclaration(context, node)) {
const name = transformIdentifier(context, node.name);
const table = lua.createBinaryExpression(
addExportToIdentifier(context, name),
lua.createTableExpression(),
lua.SyntaxKind.OrOperator
);
result.push(...createLocalOrExportedOrGlobalDeclaration(context, name, table, node));
}
const enumReference = context.transformExpression(node.name);
for (const member of node.members) {
const memberName = transformPropertyName(context, member.name);
let valueExpression: lua.Expression | undefined;
const constEnumValue = tryGetConstEnumValue(context, member);
if (constEnumValue) {
valueExpression = constEnumValue;
} else if (member.initializer) {
if (ts.isIdentifier(member.initializer)) {
const symbol = context.checker.getSymbolAtLocation(member.initializer);
if (
symbol?.valueDeclaration &&
ts.isEnumMember(symbol.valueDeclaration) &&
symbol.valueDeclaration.parent === node
) {
const otherMemberName = transformPropertyName(context, symbol.valueDeclaration.name);
valueExpression = lua.createTableIndexExpression(enumReference, otherMemberName);
}
}
valueExpression ??= context.transformExpression(member.initializer);
} else {
valueExpression = lua.createNilLiteral();
}
if (membersOnly) {
const enumSymbol = context.checker.getSymbolAtLocation(node.name);
const exportScope = enumSymbol ? getSymbolExportScope(context, enumSymbol) : undefined;
result.push(
...createLocalOrExportedOrGlobalDeclaration(
context,
lua.isIdentifier(memberName)
? memberName
: lua.createIdentifier(member.name.getText(), member.name),
valueExpression,
node,
exportScope
)
);
} else {
const memberAccessor = lua.createTableIndexExpression(enumReference, memberName);
result.push(lua.createAssignmentStatement(memberAccessor, valueExpression, member));
if (!lua.isStringLiteral(valueExpression) && !lua.isNilLiteral(valueExpression)) {
const reverseMemberAccessor = lua.createTableIndexExpression(enumReference, memberAccessor);
result.push(lua.createAssignmentStatement(reverseMemberAccessor, memberName, member));
}
}
}
return result;
};