Skip to content

Allow empty lists on command line #9090

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
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
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 === "") {
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
34 changes: 30 additions & 4 deletions tests/cases/unittests/commandLineParsing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,23 @@ 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
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
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 +245,19 @@ namespace ts {
});
});

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