Skip to content
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
5 changes: 5 additions & 0 deletions src/cli/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ export const compilerOptionRequiresAValueOfType = createCommandLineError(
(name: string, type: string) => `Compiler option '${name}' requires a value of type ${type}.`
);

export const compilerOptionCouldNotParseJson = createCommandLineError(
5025,
(name: string, error: string) => `Compiler option '${name}' failed to parse the given JSON value: '${error}'.`
);

export const optionProjectCannotBeMixedWithSourceFilesOnACommandLine = createCommandLineError(
5042,
() => "Option 'project' cannot be mixed with source files on a command line."
Expand Down
57 changes: 45 additions & 12 deletions src/cli/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ interface CommandLineOptionOfEnum extends CommandLineOptionBase {
}

interface CommandLineOptionOfPrimitive extends CommandLineOptionBase {
type: "boolean" | "string" | "object" | "array";
type: "boolean" | "string" | "json-array-of-objects" | "array";
}

type CommandLineOption = CommandLineOptionOfEnum | CommandLineOptionOfPrimitive;
Expand Down Expand Up @@ -82,7 +82,7 @@ export const optionDeclarations: CommandLineOption[] = [
{
name: "luaPlugins",
description: "List of TypeScriptToLua plugins.",
type: "object",
type: "json-array-of-objects",
},
{
name: "tstlVerbose",
Expand Down Expand Up @@ -124,7 +124,7 @@ export function updateParsedConfigFile(parsedConfigFile: ts.ParsedCommandLine):
continue;
}

const { error, value } = readValue(option, rawValue);
const { error, value } = readValue(option, rawValue, OptionSource.TsConfig);
if (error) parsedConfigFile.errors.push(error);
if (parsedConfigFile.options[name] === undefined) parsedConfigFile.options[name] = value;
}
Expand Down Expand Up @@ -164,9 +164,9 @@ function updateParsedCommandLine(parsedCommandLine: ts.ParsedCommandLine, args:
if (error) parsedCommandLine.errors.push(error);
parsedCommandLine.options[option.name] = value;
if (consumed) {
i += 1;
// Values of custom options are parsed as a file name, exclude them
parsedCommandLine.fileNames = parsedCommandLine.fileNames.filter(f => f !== value);
parsedCommandLine.fileNames = parsedCommandLine.fileNames.filter(f => f !== args[i + 1]);
i += 1;
}
}
}
Expand Down Expand Up @@ -196,21 +196,25 @@ function readCommandLineArgument(option: CommandLineOption, value: any): Command
};
}

return { ...readValue(option, value), consumed: true };
return { ...readValue(option, value, OptionSource.CommandLine), consumed: true };
}

enum OptionSource {
CommandLine,
TsConfig,
}

interface ReadValueResult {
error?: ts.Diagnostic;
value: any;
}

function readValue(option: CommandLineOption, value: unknown): ReadValueResult {
function readValue(option: CommandLineOption, value: unknown, source: OptionSource): ReadValueResult {
if (value === null) return { value };

switch (option.type) {
case "boolean":
case "string":
case "object": {
case "string": {
if (typeof value !== option.type) {
return {
value: undefined,
Expand All @@ -220,15 +224,44 @@ function readValue(option: CommandLineOption, value: unknown): ReadValueResult {

return { value };
}
case "array": {
if (!Array.isArray(value)) {
case "array":
case "json-array-of-objects": {
const isInvalidNonCliValue = source === OptionSource.TsConfig && !Array.isArray(value);
const isInvalidCliValue = source === OptionSource.CommandLine && typeof value !== "string";

if (isInvalidNonCliValue || isInvalidCliValue) {
return {
value: undefined,
error: cliDiagnostics.compilerOptionRequiresAValueOfType(option.name, option.type),
};
}

return { value };
const shouldParseValue = source === OptionSource.CommandLine && typeof value === "string";
if (!shouldParseValue) return { value };

if (option.type === "array") {
const array = value.split(",");
return { value: array };
}

try {
const objects = JSON.parse(value);
if (!Array.isArray(objects)) {
return {
value: undefined,
error: cliDiagnostics.compilerOptionRequiresAValueOfType(option.name, option.type),
};
}

return { value: objects };
} catch (e) {
if (!(e instanceof SyntaxError)) throw e;

return {
value: undefined,
error: cliDiagnostics.compilerOptionCouldNotParseJson(option.name, e.message),
};
}
}
case "enum": {
if (typeof value !== "string") {
Expand Down
14 changes: 14 additions & 0 deletions test/cli/parse.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ describe("command line", () => {
});
});

describe("array-of-objects options", () => {
test.each([["{"], ["{}"], ["0"], ["''"]])("should error on invalid value (%s)", value => {
const result = tstl.parseCommandLine(["--luaPlugins", value]);

expect(result.errors).toHaveDiagnostics();
});
});

describe("boolean options", () => {
test.each([true, false])("should parse booleans (%p)", value => {
const result = tstl.parseCommandLine(["--noHeader", value.toString()]);
Expand Down Expand Up @@ -125,6 +133,12 @@ describe("command line", () => {

["extension", ".lua", { extension: ".lua" }],
["extension", "scar", { extension: "scar" }],

["luaPlugins", '[{ "name": "a" }]', { luaPlugins: [{ name: "a" }] }],
["luaPlugins", '[{ "name": "a" },{ "name": "b" }]', { luaPlugins: [{ name: "a" }, { name: "b" }] }],

["noResolvePaths", "path1", { noResolvePaths: ["path1"] }],
["noResolvePaths", "path1,path2", { noResolvePaths: ["path1", "path2"] }],
])("--%s %s", (optionName, value, expected) => {
const result = tstl.parseCommandLine([`--${optionName}`, value]);

Expand Down