Skip to content

Commit 9d023a8

Browse files
authored
Merge pull request microsoft#9090 from weswigham/allow-empty-lists-command-line
Allow empty lists on command line
2 parents f9923ef + 8d83cd1 commit 9d023a8

File tree

2 files changed

+46
-10
lines changed

2 files changed

+46
-10
lines changed

src/compiler/commandLineParser.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -493,13 +493,20 @@ namespace ts {
493493
}
494494

495495
/* @internal */
496-
export function parseListTypeOption(opt: CommandLineOptionOfListType, value: string, errors: Diagnostic[]): (string | number)[] {
497-
const values = trimString((value || "")).split(",");
496+
export function parseListTypeOption(opt: CommandLineOptionOfListType, value = "", errors: Diagnostic[]): (string | number)[] | undefined {
497+
value = trimString(value);
498+
if (startsWith(value, "-")) {
499+
return undefined;
500+
}
501+
if (value === "") {
502+
return [];
503+
}
504+
const values = value.split(",");
498505
switch (opt.element.type) {
499506
case "number":
500-
return ts.map(values, parseInt);
507+
return map(values, parseInt);
501508
case "string":
502-
return ts.map(values, v => v || "");
509+
return map(values, v => v || "");
503510
default:
504511
return filter(map(values, v => parseCustomTypeOption(<CommandLineOptionOfCustomType>opt.element, v, errors)), v => !!v);
505512
}
@@ -560,8 +567,11 @@ namespace ts {
560567
i++;
561568
break;
562569
case "list":
563-
options[opt.name] = parseListTypeOption(<CommandLineOptionOfListType>opt, args[i], errors);
564-
i++;
570+
const result = parseListTypeOption(<CommandLineOptionOfListType>opt, args[i], errors);
571+
options[opt.name] = result || [];
572+
if (result) {
573+
i++;
574+
}
565575
break;
566576
// If not a primitive, the possible types are specified in what is effectively a map of options.
567577
default:

tests/cases/unittests/commandLineParsing.ts

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,23 @@ 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+
it("Parse empty string of --lib ", () => {
228+
// 0.ts --lib
229+
// This test is an error because the empty string is falsey
230+
assertParseResult(["0.ts", "--lib", ""],
231+
{
232+
errors: [{
233+
messageText: "Compiler option 'lib' expects an argument.",
234+
category: ts.Diagnostics.Compiler_option_0_expects_an_argument.category,
235+
code: ts.Diagnostics.Compiler_option_0_expects_an_argument.code,
223236

224237
file: undefined,
225238
start: undefined,
@@ -232,6 +245,19 @@ namespace ts {
232245
});
233246
});
234247

248+
it("Parse immediately following command line argument of --lib ", () => {
249+
// 0.ts --lib
250+
assertParseResult(["0.ts", "--lib", "--sourcemap"],
251+
{
252+
errors: [],
253+
fileNames: ["0.ts"],
254+
options: {
255+
lib: [],
256+
sourceMap: true
257+
}
258+
});
259+
});
260+
235261
it("Parse --lib option with extra comma ", () => {
236262
// --lib es5, es7 0.ts
237263
assertParseResult(["--lib", "es5,", "es7", "0.ts"],

0 commit comments

Comments
 (0)