-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathluahelp-enum.ts
More file actions
91 lines (75 loc) · 2.35 KB
/
Copy pathluahelp-enum.ts
File metadata and controls
91 lines (75 loc) · 2.35 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
import { LuaHelpTreeTableNode } from "@cassolette/luahelpparser";
import { TSNamespaceBuilder } from "./doc-builders";
export class DocTableNode {
public children: DocTableNode[];
public parent?: DocTableNode;
constructor(public name: string, public ast?: LuaHelpTreeTableNode) {
this.children = [];
}
static fromAst(ast: LuaHelpTreeTableNode) {
const tblNode = new DocTableNode(ast.name, ast);
for (const c of ast.children) {
if (c.type !== "table") continue;
tblNode.addChild(DocTableNode.fromAst(c));
}
return tblNode;
}
addChild(childNode: DocTableNode) {
this.children.push(childNode);
childNode.parent = this;
}
/**
* Navigate to a child table node.
*/
navigate(name: string) {
return this.children.find((c) => c.name === name);
}
exportLua(prefix?: string) {
if (prefix) {
prefix += "." + this.name;
} else {
prefix = this.name;
}
const newLines: string[] = [`${prefix} = {}`];
for (const c of this.children) {
newLines.push("", ...c.exportLua(prefix));
}
if (this.ast) {
for (const entry of this.ast.children) {
if (entry.type !== "value") continue;
newLines.push(`${prefix}.${entry.name} = ${entry.value}`);
}
}
return newLines;
}
/**
* Recursively writes content to enum namespaces.
* @param enumNs `tfm.enum.X.X`
* @param enumTypeNs `tfm.Enums.X`
*/
writeTstlNamespace(enumNs: TSNamespaceBuilder, enumTypeNs: TSNamespaceBuilder) {
const isEnum = this.ast?.children[0]?.type === "value";
for (const c of this.children) {
c.writeTstlNamespace(enumNs.navigate(c.name), enumTypeNs);
}
if (!isEnum) {
// Nothing else to do.
return;
}
const enumTypeNsContent: string[] = [];
for (const entry of this.ast.children) {
if (entry.type !== "value") continue;
enumNs.pushStatement([`const ${entry.name} = ${entry.value};`]);
enumTypeNsContent.push(` ${entry.name} = ${entry.value},`);
}
// Create new enum in the Enums namespace.
// Export a const enum type to provide an option to compile enums into literals, or use them
// as types.
const capsName = this.name.charAt(0).toUpperCase() + this.name.slice(1);
enumTypeNs.pushStatement([
`const enum ${capsName}Type {`,
...enumTypeNsContent,
"}",
]);
}
}