/
commandLineParsing.ts
246 lines (230 loc) · 13.8 KB
/
commandLineParsing.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
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
namespace ts {
describe("unittests:: config:: commandLineParsing:: parseCommandLine", () => {
function assertParseResult(subScenario: string, commandLine: string[], workerDiagnostic?: () => ParseCommandLineWorkerDiagnostics) {
it(subScenario, () => {
const baseline: string[] = [];
baseline.push(commandLine.join(" "));
const parsed = parseCommandLineWorker(workerDiagnostic?.() || compilerOptionsDidYouMeanDiagnostics, commandLine);
baseline.push("CompilerOptions::");
baseline.push(JSON.stringify(parsed.options, /*replacer*/ undefined, " "));
baseline.push("WatchOptions::");
baseline.push(JSON.stringify(parsed.watchOptions, /*replacer*/ undefined, " "));
baseline.push("FileNames::");
baseline.push(parsed.fileNames.join());
baseline.push("Errors::");
baseline.push(formatDiagnostics(parsed.errors, {
getCurrentDirectory: () => "/",
getCanonicalFileName: identity,
getNewLine: () => "\n",
}));
Harness.Baseline.runBaseline(`config/commandLineParsing/parseCommandLine/${subScenario}.js`, baseline.join("\n"));
});
}
// --lib es6 0.ts
assertParseResult("Parse single option of library flag", ["--lib", "es6", "0.ts"]);
assertParseResult("Handles may only be used with --build flags", ["--clean", "--dry", "--force", "--verbose"]);
// --declarations --allowTS
assertParseResult("Handles did you mean for misspelt flags", ["--declarations", "--allowTS"]);
// --lib es5,es2015.symbol.wellknown 0.ts
assertParseResult("Parse multiple options of library flags", ["--lib", "es5,es2015.symbol.wellknown", "0.ts"]);
// --lib es5,invalidOption 0.ts
assertParseResult("Parse invalid option of library flags", ["--lib", "es5,invalidOption", "0.ts"]);
// 0.ts --jsx
assertParseResult("Parse empty options of --jsx", ["0.ts", "--jsx"]);
// 0.ts --
assertParseResult("Parse empty options of --module", ["0.ts", "--module"]);
// 0.ts --newLine
assertParseResult("Parse empty options of --newLine", ["0.ts", "--newLine"]);
// 0.ts --target
assertParseResult("Parse empty options of --target", ["0.ts", "--target"]);
// 0.ts --moduleResolution
assertParseResult("Parse empty options of --moduleResolution", ["0.ts", "--moduleResolution"]);
// 0.ts --lib
assertParseResult("Parse empty options of --lib", ["0.ts", "--lib"]);
// 0.ts --lib
// This test is an error because the empty string is falsey
assertParseResult("Parse empty string of --lib", ["0.ts", "--lib", ""]);
// 0.ts --lib
assertParseResult("Parse immediately following command line argument of --lib", ["0.ts", "--lib", "--sourcemap"]);
// --lib es5, es7 0.ts
assertParseResult("Parse --lib option with extra comma", ["--lib", "es5,", "es7", "0.ts"]);
// --lib es5, es7 0.ts
assertParseResult("Parse --lib option with trailing white-space", ["--lib", "es5, ", "es7", "0.ts"]);
// --lib es5,es2015.symbol.wellknown --target es5 0.ts
assertParseResult("Parse multiple compiler flags with input files at the end", ["--lib", "es5,es2015.symbol.wellknown", "--target", "es5", "0.ts"]);
// --module commonjs --target es5 0.ts --lib es5,es2015.symbol.wellknown
assertParseResult("Parse multiple compiler flags with input files in the middle", ["--module", "commonjs", "--target", "es5", "0.ts", "--lib", "es5,es2015.symbol.wellknown"]);
// --module commonjs --target es5 --lib es5 0.ts --library es2015.array,es2015.symbol.wellknown
assertParseResult("Parse multiple library compiler flags ", ["--module", "commonjs", "--target", "es5", "--lib", "es5", "0.ts", "--lib", "es2015.core, es2015.symbol.wellknown "]);
assertParseResult("Parse explicit boolean flag value", ["--strictNullChecks", "false", "0.ts"]);
assertParseResult("Parse non boolean argument after boolean flag", ["--noImplicitAny", "t", "0.ts"]);
assertParseResult("Parse implicit boolean flag value", ["--strictNullChecks"]);
assertParseResult("parse --incremental", ["--incremental", "0.ts"]);
assertParseResult("parse --tsBuildInfoFile", ["--tsBuildInfoFile", "build.tsbuildinfo", "0.ts"]);
describe("parses command line null for tsconfig only option", () => {
interface VerifyNull {
subScenario: string,
optionName: string;
nonNullValue?: string;
workerDiagnostic?: () => ParseCommandLineWorkerDiagnostics;
}
function verifyNull({ subScenario, optionName, nonNullValue, workerDiagnostic }: VerifyNull) {
describe(subScenario, () => {
assertParseResult(
`${subScenario} allows setting it to null`,
[`--${optionName}`, "null", "0.ts"],
workerDiagnostic
);
if (nonNullValue) {
assertParseResult(
`${subScenario} errors if non null value is passed`,
[`--${optionName}`, nonNullValue, "0.ts"],
workerDiagnostic
);
}
assertParseResult(
`${subScenario} errors if its followed by another option`,
["0.ts", "--strictNullChecks", `--${optionName}`],
workerDiagnostic
);
assertParseResult(
`${subScenario} errors if its last option`,
["0.ts", `--${optionName}`],
workerDiagnostic
);
});
}
interface VerifyNullNonIncludedOption {
subScenario: string,
type: () => "string" | "number" | ESMap<string, number | string>;
nonNullValue?: string;
}
function verifyNullNonIncludedOption({ subScenario, type, nonNullValue }: VerifyNullNonIncludedOption) {
verifyNull({
subScenario,
optionName: "optionName",
nonNullValue,
workerDiagnostic: () => {
const optionDeclarations: CommandLineOption[] = [
...compilerOptionsDidYouMeanDiagnostics.optionDeclarations,
{
name: "optionName",
type: type(),
isTSConfigOnly: true,
category: Diagnostics.Backwards_Compatibility,
description: Diagnostics.Enable_project_compilation,
defaultValueDescription: undefined,
}
];
return {
...compilerOptionsDidYouMeanDiagnostics,
optionDeclarations,
getOptionsNameMap: () => createOptionNameMap(optionDeclarations)
};
}
});
}
describe("option of type boolean", () => {
assertParseResult(
"allows setting option type boolean to false",
["--composite", "false", "0.ts"],
);
verifyNull({
subScenario: "option of type boolean",
optionName: "composite",
nonNullValue: "true",
});
});
verifyNull({
subScenario: "option of type object",
optionName: "paths",
});
verifyNull({
subScenario: "option of type list",
optionName: "rootDirs",
nonNullValue: "abc,xyz",
});
verifyNullNonIncludedOption({
subScenario: "option of type string",
type: () => "string",
nonNullValue: "hello"
});
verifyNullNonIncludedOption({
subScenario: "option of type number",
type: () => "number",
nonNullValue: "10"
});
verifyNullNonIncludedOption({
subScenario: "option of type custom map",
type: () => new Map(getEntries({
node: ModuleResolutionKind.NodeJs,
classic: ModuleResolutionKind.Classic,
})),
nonNullValue: "node"
});
});
assertParseResult("allows tsconfig only option to be set to null", ["--composite", "null", "-tsBuildInfoFile", "null", "0.ts"]);
describe("Watch options", () => {
assertParseResult("parse --watchFile", ["--watchFile", "UseFsEvents", "0.ts"]);
assertParseResult("parse --watchDirectory", ["--watchDirectory", "FixedPollingInterval", "0.ts"]);
assertParseResult("parse --fallbackPolling", ["--fallbackPolling", "PriorityInterval", "0.ts"]);
assertParseResult("parse --synchronousWatchDirectory", ["--synchronousWatchDirectory", "0.ts"]);
assertParseResult("errors on missing argument to --fallbackPolling", ["0.ts", "--fallbackPolling"]);
assertParseResult("parse --excludeDirectories", ["--excludeDirectories", "**/temp", "0.ts"]);
assertParseResult("errors on invalid excludeDirectories", ["--excludeDirectories", "**/../*", "0.ts"]);
assertParseResult("parse --excludeFiles", ["--excludeFiles", "**/temp/*.ts", "0.ts"]);
assertParseResult("errors on invalid excludeFiles", ["--excludeFiles", "**/../*", "0.ts"]);
});
});
describe("unittests:: config:: commandLineParsing:: parseBuildOptions", () => {
function assertParseResult(subScenario: string, commandLine: string[]) {
it(subScenario, () => {
const baseline: string[] = [];
baseline.push(commandLine.join(" "));
const parsed = parseBuildCommand(commandLine);
baseline.push("buildOptions::");
baseline.push(JSON.stringify(parsed.buildOptions, /*replacer*/ undefined, " "));
baseline.push("WatchOptions::");
baseline.push(JSON.stringify(parsed.watchOptions, /*replacer*/ undefined, " "));
baseline.push("Projects::");
baseline.push(parsed.projects.join());
baseline.push("Errors::");
baseline.push(formatDiagnostics(parsed.errors, {
getCurrentDirectory: () => "/",
getCanonicalFileName: identity,
getNewLine: () => "\n",
}));
Harness.Baseline.runBaseline(`config/commandLineParsing/parseBuildOptions/${subScenario}.js`, baseline.join("\n"));
});
}
assertParseResult("parse build without any options ", []);
assertParseResult("Parse multiple options", ["--verbose", "--force", "tests"]);
assertParseResult("Parse option with invalid option", ["--verbose", "--invalidOption"]);
assertParseResult("Parse multiple flags with input projects at the end", ["--force", "--verbose", "src", "tests"]);
assertParseResult("Parse multiple flags with input projects in the middle", ["--force", "src", "tests", "--verbose"]);
assertParseResult("Parse multiple flags with input projects in the beginning", ["src", "tests", "--force", "--verbose"]);
assertParseResult("parse build with --incremental", ["--incremental", "tests"]);
assertParseResult("parse build with --locale en-us", ["--locale", "en-us", "src"]);
assertParseResult("parse build with --tsBuildInfoFile", ["--tsBuildInfoFile", "build.tsbuildinfo", "tests"]);
assertParseResult("reports other common may not be used with --build flags", ["--declaration", "--strict"]);
describe("Combining options that make no sense together", () => {
function verifyInvalidCombination(flag1: keyof BuildOptions, flag2: keyof BuildOptions) {
assertParseResult(`--${flag1} and --${flag2} together is invalid`, [`--${flag1}`, `--${flag2}`]);
}
verifyInvalidCombination("clean", "force");
verifyInvalidCombination("clean", "verbose");
verifyInvalidCombination("clean", "watch");
verifyInvalidCombination("watch", "dry");
});
describe("Watch options", () => {
assertParseResult("parse --watchFile", ["--watchFile", "UseFsEvents", "--verbose"]);
assertParseResult("parse --watchDirectory", ["--watchDirectory", "FixedPollingInterval", "--verbose"]);
assertParseResult("parse --fallbackPolling", ["--fallbackPolling", "PriorityInterval", "--verbose"]);
assertParseResult("parse --synchronousWatchDirectory", ["--synchronousWatchDirectory", "--verbose"]);
assertParseResult("errors on missing argument", ["--verbose", "--fallbackPolling"]);
assertParseResult("errors on invalid excludeDirectories", ["--excludeDirectories", "**/../*"]);
assertParseResult("parse --excludeFiles", ["--excludeFiles", "**/temp/*.ts"]);
assertParseResult("errors on invalid excludeFiles", ["--excludeFiles", "**/../*"]);
});
});
}