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
21 changes: 13 additions & 8 deletions src/cli/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,23 +122,28 @@ function updateParsedCommandLine(parsedCommandLine: ts.ParsedCommandLine, args:

if (option) {
// Ignore errors caused by tstl specific compiler options
const tsInvalidCompilerOptionErrorCode = 5023;
parsedCommandLine.errors = parsedCommandLine.errors.filter(
e => !(e.code === tsInvalidCompilerOptionErrorCode && String(e.messageText).endsWith(`'${args[i]}'.`))
// TS5023: Unknown compiler option '{0}'.
// TS5025: Unknown compiler option '{0}'. Did you mean '{1}'?
e => !((e.code === 5023 || e.code === 5025) && String(e.messageText).includes(`'${args[i]}'.`))
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 don't like these magic numbers. If you want to hardcode them like this at least add a comment saying something like // TS error code 5023: Invalid Compiler option

);

const { error, value, increment } = readCommandLineArgument(option, args[i + 1]);
const { error, value, consumed } = readCommandLineArgument(option, args[i + 1]);
if (error) parsedCommandLine.errors.push(error);
parsedCommandLine.options[option.name] = value;
i += increment;
if (consumed) {
i += 1;
// Values of custom options are parsed as a file name, exclude them
parsedCommandLine.fileNames = parsedCommandLine.fileNames.filter(f => f !== value);
}
}
}

return parsedCommandLine;
}

interface CommandLineArgument extends ReadValueResult {
increment: number;
consumed: boolean;
}

function readCommandLineArgument(option: CommandLineOption, value: any): CommandLineArgument {
Expand All @@ -147,19 +152,19 @@ function readCommandLineArgument(option: CommandLineOption, value: any): Command
value = value === "true";
} else {
// Set boolean arguments without supplied value to true
return { value: true, increment: 0 };
return { value: true, consumed: false };
}
}

if (value === undefined) {
return {
error: cliDiagnostics.compilerOptionExpectsAnArgument(option.name),
value: undefined,
increment: 0,
consumed: false,
};
}

return { ...readValue(option, value), increment: 1 };
return { ...readValue(option, value), consumed: true };
}

interface ReadValueResult {
Expand Down
3 changes: 2 additions & 1 deletion test/cli/parse.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ describe("command line", () => {
});

test("should support standard typescript options", () => {
const commandLine = "--project tsconfig.json --noHeader -t es3 -lt 5.3";
const commandLine = "main.ts --project tsconfig.json --noHeader -t es3 -lt 5.3";
const result = tstl.parseCommandLine(commandLine.split(" "));

expect(result.errors).not.toHaveDiagnostics();
expect(result.fileNames).toEqual(["main.ts"]);
expect(result.options).toEqual({
project: "tsconfig.json",
noHeader: true,
Expand Down