Skip to content

Commit 1e4a8bb

Browse files
authored
Cli fixes (#883)
* Don't parse values of custom options as files * Remove `did you mean` CLI parsing diagnostic * Add ignored diagnostic code messages as comments
1 parent d1efec1 commit 1e4a8bb

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

src/cli/parse.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -122,23 +122,28 @@ function updateParsedCommandLine(parsedCommandLine: ts.ParsedCommandLine, args:
122122

123123
if (option) {
124124
// Ignore errors caused by tstl specific compiler options
125-
const tsInvalidCompilerOptionErrorCode = 5023;
126125
parsedCommandLine.errors = parsedCommandLine.errors.filter(
127-
e => !(e.code === tsInvalidCompilerOptionErrorCode && String(e.messageText).endsWith(`'${args[i]}'.`))
126+
// TS5023: Unknown compiler option '{0}'.
127+
// TS5025: Unknown compiler option '{0}'. Did you mean '{1}'?
128+
e => !((e.code === 5023 || e.code === 5025) && String(e.messageText).includes(`'${args[i]}'.`))
128129
);
129130

130-
const { error, value, increment } = readCommandLineArgument(option, args[i + 1]);
131+
const { error, value, consumed } = readCommandLineArgument(option, args[i + 1]);
131132
if (error) parsedCommandLine.errors.push(error);
132133
parsedCommandLine.options[option.name] = value;
133-
i += increment;
134+
if (consumed) {
135+
i += 1;
136+
// Values of custom options are parsed as a file name, exclude them
137+
parsedCommandLine.fileNames = parsedCommandLine.fileNames.filter(f => f !== value);
138+
}
134139
}
135140
}
136141

137142
return parsedCommandLine;
138143
}
139144

140145
interface CommandLineArgument extends ReadValueResult {
141-
increment: number;
146+
consumed: boolean;
142147
}
143148

144149
function readCommandLineArgument(option: CommandLineOption, value: any): CommandLineArgument {
@@ -147,19 +152,19 @@ function readCommandLineArgument(option: CommandLineOption, value: any): Command
147152
value = value === "true";
148153
} else {
149154
// Set boolean arguments without supplied value to true
150-
return { value: true, increment: 0 };
155+
return { value: true, consumed: false };
151156
}
152157
}
153158

154159
if (value === undefined) {
155160
return {
156161
error: cliDiagnostics.compilerOptionExpectsAnArgument(option.name),
157162
value: undefined,
158-
increment: 0,
163+
consumed: false,
159164
};
160165
}
161166

162-
return { ...readValue(option, value), increment: 1 };
167+
return { ...readValue(option, value), consumed: true };
163168
}
164169

165170
interface ReadValueResult {

test/cli/parse.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ describe("command line", () => {
99
});
1010

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

1515
expect(result.errors).not.toHaveDiagnostics();
16+
expect(result.fileNames).toEqual(["main.ts"]);
1617
expect(result.options).toEqual({
1718
project: "tsconfig.json",
1819
noHeader: true,

0 commit comments

Comments
 (0)