-
-
Notifications
You must be signed in to change notification settings - Fork 185
Expand file tree
/
Copy pathparse.ts
More file actions
291 lines (255 loc) · 9.82 KB
/
parse.ts
File metadata and controls
291 lines (255 loc) · 9.82 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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import * as ts from "typescript";
import { BuildMode, CompilerOptions, LuaLibImportKind, LuaTarget } from "../CompilerOptions";
import * as cliDiagnostics from "./diagnostics";
export interface ParsedCommandLine extends ts.ParsedCommandLine {
options: CompilerOptions;
}
interface CommandLineOptionBase {
name: string;
aliases?: string[];
description: string;
}
interface CommandLineOptionOfEnum extends CommandLineOptionBase {
type: "enum";
choices: string[];
}
interface CommandLineOptionOfPrimitive extends CommandLineOptionBase {
type: "boolean" | "string" | "json-array-of-objects" | "array";
}
type CommandLineOption = CommandLineOptionOfEnum | CommandLineOptionOfPrimitive;
export const optionDeclarations: CommandLineOption[] = [
{
name: "buildMode",
description: "'default' or 'library'. Compiling as library will not resolve external dependencies.",
type: "enum",
choices: Object.values(BuildMode),
},
{
name: "extension",
description: 'File extension for the resulting Lua files. Defaults to ".lua"',
type: "string",
},
{
name: "luaBundle",
description: "The name of the lua file to bundle output lua to. Requires luaBundleEntry.",
type: "string",
},
{
name: "luaBundleEntry",
description: "The entry *.ts file that will be executed when entering the luaBundle. Requires luaBundle.",
type: "string",
},
{
name: "luaLibImport",
description: "Specifies how js standard features missing in lua are imported.",
type: "enum",
choices: Object.values(LuaLibImportKind),
},
{
name: "luaTarget",
aliases: ["lt"],
description: "Specify Lua target version.",
type: "enum",
choices: Object.values(LuaTarget),
},
{
name: "noImplicitGlobalVariables",
description:
'Specify to prevent implicitly turning "normal" variants into global variables in the transpiled output.',
type: "boolean",
},
{
name: "noImplicitSelf",
description: 'If "this" is implicitly considered an any type, do not generate a self parameter.',
type: "boolean",
},
{
name: "noHeader",
description: "Specify if a header will be added to compiled files.",
type: "boolean",
},
{
name: "sourceMapTraceback",
description: "Applies the source map to show source TS files and lines in error tracebacks.",
type: "boolean",
},
{
name: "luaPlugins",
description: "List of TypeScriptToLua plugins.",
type: "json-array-of-objects",
},
{
name: "tstlVerbose",
description: "Provide verbose output useful for diagnosing problems.",
type: "boolean",
},
{
name: "noResolvePaths",
description: "An array of paths that tstl should not resolve and keep as-is.",
type: "array",
},
{
name: "lua51AllowTryCatchInAsyncAwait",
description: "Always allow try/catch in async/await functions for Lua 5.1.",
type: "boolean",
},
{
name: "measurePerformance",
description: "Measure performance of the tstl compiler.",
type: "boolean",
},
];
export function updateParsedConfigFile(parsedConfigFile: ts.ParsedCommandLine): ParsedCommandLine {
let hasRootLevelOptions = false;
for (const [name, rawValue] of Object.entries(parsedConfigFile.raw)) {
const option = optionDeclarations.find(option => option.name === name);
if (!option) continue;
if (parsedConfigFile.raw.tstl === undefined) parsedConfigFile.raw.tstl = {};
parsedConfigFile.raw.tstl[name] = rawValue;
hasRootLevelOptions = true;
}
if (parsedConfigFile.raw.tstl) {
if (hasRootLevelOptions) {
parsedConfigFile.errors.push(cliDiagnostics.tstlOptionsAreMovingToTheTstlObject(parsedConfigFile.raw.tstl));
}
for (const [name, rawValue] of Object.entries(parsedConfigFile.raw.tstl)) {
const option = optionDeclarations.find(option => option.name === name);
if (!option) {
parsedConfigFile.errors.push(cliDiagnostics.unknownCompilerOption(name));
continue;
}
const { error, value } = readValue(option, rawValue, OptionSource.TsConfig);
if (error) parsedConfigFile.errors.push(error);
if (parsedConfigFile.options[name] === undefined) parsedConfigFile.options[name] = value;
}
}
return parsedConfigFile;
}
export function parseCommandLine(args: string[]): ParsedCommandLine {
return updateParsedCommandLine(ts.parseCommandLine(args), args);
}
function updateParsedCommandLine(parsedCommandLine: ts.ParsedCommandLine, args: string[]): ParsedCommandLine {
for (let i = 0; i < args.length; i++) {
if (!args[i].startsWith("-")) continue;
const isShorthand = !args[i].startsWith("--");
const argumentName = args[i].substring(isShorthand ? 1 : 2);
const option = optionDeclarations.find(option => {
if (option.name.toLowerCase() === argumentName.toLowerCase()) return true;
if (isShorthand && option.aliases) {
return option.aliases.some(a => a.toLowerCase() === argumentName.toLowerCase());
}
return false;
});
if (option) {
// Ignore errors caused by tstl specific compiler options
parsedCommandLine.errors = parsedCommandLine.errors.filter(
// TS5023: Unknown compiler option '{0}'.
// TS5025: Unknown compiler option '{0}'. Did you mean '{1}'?
e => !((e.code === 5023 || e.code === 5025) && String(e.messageText).includes(`'${args[i]}'.`))
);
const { error, value, consumed } = readCommandLineArgument(option, args[i + 1]);
if (error) parsedCommandLine.errors.push(error);
parsedCommandLine.options[option.name] = value;
if (consumed) {
// Values of custom options are parsed as a file name, exclude them
parsedCommandLine.fileNames = parsedCommandLine.fileNames.filter(f => f !== args[i + 1]);
i += 1;
}
}
}
return parsedCommandLine;
}
interface CommandLineArgument extends ReadValueResult {
consumed: boolean;
}
function readCommandLineArgument(option: CommandLineOption, value: any): CommandLineArgument {
if (option.type === "boolean") {
if (value === "true" || value === "false") {
value = value === "true";
} else {
// Set boolean arguments without supplied value to true
return { value: true, consumed: false };
}
}
if (value === undefined) {
return {
error: cliDiagnostics.compilerOptionExpectsAnArgument(option.name),
value: undefined,
consumed: false,
};
}
return { ...readValue(option, value, OptionSource.CommandLine), consumed: true };
}
enum OptionSource {
CommandLine,
TsConfig,
}
interface ReadValueResult {
error?: ts.Diagnostic;
value: any;
}
function readValue(option: CommandLineOption, value: unknown, source: OptionSource): ReadValueResult {
if (value === null) return { value };
switch (option.type) {
case "boolean":
case "string": {
if (typeof value !== option.type) {
return {
value: undefined,
error: cliDiagnostics.compilerOptionRequiresAValueOfType(option.name, option.type),
};
}
return { value };
}
case "array":
case "json-array-of-objects": {
const isInvalidNonCliValue = source === OptionSource.TsConfig && !Array.isArray(value);
const isInvalidCliValue = source === OptionSource.CommandLine && typeof value !== "string";
if (isInvalidNonCliValue || isInvalidCliValue) {
return {
value: undefined,
error: cliDiagnostics.compilerOptionRequiresAValueOfType(option.name, option.type),
};
}
const shouldParseValue = source === OptionSource.CommandLine && typeof value === "string";
if (!shouldParseValue) return { value };
if (option.type === "array") {
const array = value.split(",");
return { value: array };
}
try {
const objects = JSON.parse(value);
if (!Array.isArray(objects)) {
return {
value: undefined,
error: cliDiagnostics.compilerOptionRequiresAValueOfType(option.name, option.type),
};
}
return { value: objects };
} catch (e) {
if (!(e instanceof SyntaxError)) throw e;
return {
value: undefined,
error: cliDiagnostics.compilerOptionCouldNotParseJson(option.name, e.message),
};
}
}
case "enum": {
if (typeof value !== "string") {
return {
value: undefined,
error: cliDiagnostics.compilerOptionRequiresAValueOfType(option.name, "string"),
};
}
const enumValue = option.choices.find(c => c.toLowerCase() === value.toLowerCase());
if (enumValue === undefined) {
const optionChoices = option.choices.join(", ");
return {
value: undefined,
error: cliDiagnostics.argumentForOptionMustBe(`--${option.name}`, optionChoices),
};
}
return { value: enumValue };
}
}
}