Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Allow empty lists on command line
  • Loading branch information
weswigham committed Jun 10, 2016
commit a3a1c49739e413e03c05b498e06db3c9008989ee
22 changes: 16 additions & 6 deletions src/compiler/commandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -488,13 +488,20 @@ namespace ts {
}

/* @internal */
export function parseListTypeOption(opt: CommandLineOptionOfListType, value: string, errors: Diagnostic[]): (string | number)[] {
const values = trimString((value || "")).split(",");
export function parseListTypeOption(opt: CommandLineOptionOfListType, value = "", errors: Diagnostic[]): (string | number)[] | undefined {
value = trimString(value);
if (startsWith(value, "-")) {
return undefined;
}
if (value === "" || value === ",") {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get the --lib part but the comma seems wierd though. not sure there is much value.

return [];
}
const values = value.split(",");
switch (opt.element.type) {
case "number":
return ts.map(values, parseInt);
return map(values, parseInt);
case "string":
return ts.map(values, v => v || "");
return map(values, v => v || "");
default:
return filter(map(values, v => parseCustomTypeOption(<CommandLineOptionOfCustomType>opt.element, v, errors)), v => !!v);
}
Expand Down Expand Up @@ -555,8 +562,11 @@ namespace ts {
i++;
break;
case "list":
options[opt.name] = parseListTypeOption(<CommandLineOptionOfListType>opt, args[i], errors);
i++;
const result = parseListTypeOption(<CommandLineOptionOfListType>opt, args[i], errors);
options[opt.name] = result || [];
if (result) {
i++;
}
break;
// If not a primitive, the possible types are specified in what is effectively a map of options.
default:
Expand Down
47 changes: 43 additions & 4 deletions tests/cases/unittests/commandLineParsing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,24 @@ namespace ts {
file: undefined,
start: undefined,
length: undefined,
}, {
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'",
category: ts.Diagnostics.Argument_for_0_option_must_be_Colon_1.category,
code: ts.Diagnostics.Argument_for_0_option_must_be_Colon_1.code,
}],
fileNames: ["0.ts"],
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't all 3 cases be errors? I don't think lib: [] is valid, so somebody along the line should throw an error.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I learned in offline discussion with @weswigham that lib: [] IS valid, so carry on! :)

options: {
lib: []
}
});
});


it("Parse empty string of --lib ", () => {
// 0.ts --lib
// This test is an error because the empty string is falsey
assertParseResult(["0.ts", "--lib", ""],
{
errors: [{
messageText: "Compiler option 'lib' expects an argument.",
category: ts.Diagnostics.Compiler_option_0_expects_an_argument.category,
code: ts.Diagnostics.Compiler_option_0_expects_an_argument.code,

file: undefined,
start: undefined,
Expand All @@ -232,6 +246,31 @@ namespace ts {
});
});

it("Parse single comma of --lib ", () => {
// 0.ts --lib
assertParseResult(["0.ts", "--lib", ","],
{
errors: [],
fileNames: ["0.ts"],
options: {
lib: []
}
});
});

it("Parse immediately following command line argument of --lib ", () => {
// 0.ts --lib
assertParseResult(["0.ts", "--lib", "--sourcemap"],
{
errors: [],
fileNames: ["0.ts"],
options: {
lib: [],
sourceMap: true
}
});
});

it("Parse --lib option with extra comma ", () => {
// --lib es5, es7 0.ts
assertParseResult(["--lib", "es5,", "es7", "0.ts"],
Expand Down