-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfunctions.converter.ts
129 lines (113 loc) · 3.42 KB
/
functions.converter.ts
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import {
DocFuncBuilder,
DocFuncBuilderParam,
DocFuncBuilderType,
functionsOverrides,
DocFunction,
TSNamespaceBuilder,
} from "../lib";
import Converter from "./converter.interfaces";
export const luaFunctionsConverter = {
type: "functions",
convert: (luaHelpAst) => {
const newLines: string[] = [];
for (const func of DocFunction.fromAstArray(luaHelpAst.functions)) {
// Apply overrides
const o = functionsOverrides[func.name];
if (o) {
if (o.type == "add") {
throw `Your override "${o.name}" is an existing LuaHelp function. Please remove it!`;
}
o.modify(func);
}
const luaPar: DocFuncBuilderParam[] = [];
for (const par of func.params.values()) {
luaPar.push({
description: [
`${par.description}${
par.defaultValue
? ` (default \`${par.defaultValue.asLua()}\`)`
: ""
}`,
...par.additionalDescription,
],
name: par.displayName,
type: par.type,
isOptional: par.isOptional,
});
}
const luaRet = func.returnType
? {
description: [func.returnType.description],
type: func.returnType.type,
isOptional: func.returnType.isOptional,
}
: undefined;
const luaFncDeclaration = new DocFuncBuilder(
func.name,
func.description,
luaPar,
luaRet
);
newLines.push(...luaFncDeclaration.exportLuaDocLines(), "");
}
return newLines;
},
} as Converter;
export const tstlFunctionsConverter = {
type: "functions",
convert: (luaHelpAst) => {
const newLines: string[] = ["/** @noSelfInFile */"];
const globalNs = TSNamespaceBuilder.createGlobal();
for (const func of DocFunction.fromAstArray(luaHelpAst.functions)) {
// Apply overrides
const o = functionsOverrides[func.name];
if (o) {
if (o.type == "add") {
throw `Your override "${o.name}" is an existing LuaHelp function. Please remove it!`;
}
o.modify(func);
}
const tsPar: DocFuncBuilderParam[] = [];
for (const par of func.params.values()) {
tsPar.push({
description: [
`${par.description}${
par.defaultValue
? ` (default \`${par.defaultValue.asTs()}\`)`
: ""
}`,
...par.additionalDescription,
],
name: par.displayName,
type: par.type,
isOptional: par.isOptional,
});
}
const tsRet = func.returnType
? {
description: [func.returnType.description],
type: func.returnType.type,
isOptional: func.returnType.isOptional,
}
: undefined;
const indexes = func.name.split(".");
const tsFncDeclaration = new DocFuncBuilder(
indexes.pop(),
func.description,
tsPar,
tsRet
);
// Navigate to the function's corresponding namespace
let namespace = globalNs;
for (const index of indexes) {
namespace = namespace.navigate(index, true);
}
namespace.pushContent(tsFncDeclaration.exportJsDocCommentLines());
namespace.pushStatement([tsFncDeclaration.exportTsFuncDeclare()]);
}
newLines.push(...globalNs.exportTstl());
newLines.push("");
return newLines;
},
} as Converter;