Skip to content

Commit a3a1c49

Browse files
committed
Allow empty lists on command line
1 parent 72d64e1 commit a3a1c49

2 files changed

Lines changed: 59 additions & 10 deletions

File tree

src/compiler/commandLineParser.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -488,13 +488,20 @@ namespace ts {
488488
}
489489

490490
/* @internal */
491-
export function parseListTypeOption(opt: CommandLineOptionOfListType, value: string, errors: Diagnostic[]): (string | number)[] {
492-
const values = trimString((value || "")).split(",");
491+
export function parseListTypeOption(opt: CommandLineOptionOfListType, value = "", errors: Diagnostic[]): (string | number)[] | undefined {
492+
value = trimString(value);
493+
if (startsWith(value, "-")) {
494+
return undefined;
495+
}
496+
if (value === "" || value === ",") {
497+
return [];
498+
}
499+
const values = value.split(",");
493500
switch (opt.element.type) {
494501
case "number":
495-
return ts.map(values, parseInt);
502+
return map(values, parseInt);
496503
case "string":
497-
return ts.map(values, v => v || "");
504+
return map(values, v => v || "");
498505
default:
499506
return filter(map(values, v => parseCustomTypeOption(<CommandLineOptionOfCustomType>opt.element, v, errors)), v => !!v);
500507
}
@@ -555,8 +562,11 @@ namespace ts {
555562
i++;
556563
break;
557564
case "list":
558-
options[opt.name] = parseListTypeOption(<CommandLineOptionOfListType>opt, args[i], errors);
559-
i++;
565+
const result = parseListTypeOption(<CommandLineOptionOfListType>opt, args[i], errors);
566+
options[opt.name] = result || [];
567+
if (result) {
568+
i++;
569+
}
560570
break;
561571
// If not a primitive, the possible types are specified in what is effectively a map of options.
562572
default:

tests/cases/unittests/commandLineParsing.ts

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,24 @@ namespace ts {
216216
file: undefined,
217217
start: undefined,
218218
length: undefined,
219-
}, {
220-
messageText: "Argument for '--lib' option must be: 'es5', 'es6', 'es2015', 'es7', 'es2016', 'es2017', 'dom', 'webworker', 'scripthost', 'es2015.core', 'es2015.collection', 'es2015.generator', 'es2015.iterable', 'es2015.promise', 'es2015.proxy', 'es2015.reflect', 'es2015.symbol', 'es2015.symbol.wellknown', 'es2016.array.include', 'es2017.object', 'es2017.sharedmemory'",
221-
category: ts.Diagnostics.Argument_for_0_option_must_be_Colon_1.category,
222-
code: ts.Diagnostics.Argument_for_0_option_must_be_Colon_1.code,
219+
}],
220+
fileNames: ["0.ts"],
221+
options: {
222+
lib: []
223+
}
224+
});
225+
});
226+
227+
228+
it("Parse empty string of --lib ", () => {
229+
// 0.ts --lib
230+
// This test is an error because the empty string is falsey
231+
assertParseResult(["0.ts", "--lib", ""],
232+
{
233+
errors: [{
234+
messageText: "Compiler option 'lib' expects an argument.",
235+
category: ts.Diagnostics.Compiler_option_0_expects_an_argument.category,
236+
code: ts.Diagnostics.Compiler_option_0_expects_an_argument.code,
223237

224238
file: undefined,
225239
start: undefined,
@@ -232,6 +246,31 @@ namespace ts {
232246
});
233247
});
234248

249+
it("Parse single comma of --lib ", () => {
250+
// 0.ts --lib
251+
assertParseResult(["0.ts", "--lib", ","],
252+
{
253+
errors: [],
254+
fileNames: ["0.ts"],
255+
options: {
256+
lib: []
257+
}
258+
});
259+
});
260+
261+
it("Parse immediately following command line argument of --lib ", () => {
262+
// 0.ts --lib
263+
assertParseResult(["0.ts", "--lib", "--sourcemap"],
264+
{
265+
errors: [],
266+
fileNames: ["0.ts"],
267+
options: {
268+
lib: [],
269+
sourceMap: true
270+
}
271+
});
272+
});
273+
235274
it("Parse --lib option with extra comma ", () => {
236275
// --lib es5, es7 0.ts
237276
assertParseResult(["--lib", "es5,", "es7", "0.ts"],

0 commit comments

Comments
 (0)