Skip to content

Commit af7e232

Browse files
committed
Adds did you mean to the CLI args parser
1 parent 94f8590 commit af7e232

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/compiler/commandLineParser.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,11 +1088,11 @@ namespace ts {
10881088
}
10891089

10901090
/** Tuple with error messages for 'unknown compiler option', 'option requires type' */
1091-
type ParseCommandLineWorkerDiagnostics = [DiagnosticMessage, DiagnosticMessage];
1091+
type ParseCommandLineWorkerDiagnostics = [DiagnosticMessage, DiagnosticMessage, DiagnosticMessage];
10921092

10931093
function parseCommandLineWorker(
10941094
getOptionNameMap: () => OptionNameMap,
1095-
[unknownOptionDiagnostic, optionTypeMismatchDiagnostic]: ParseCommandLineWorkerDiagnostics,
1095+
[unknownOptionDiagnostic, unknownDidYouMeanDiagnostic, optionTypeMismatchDiagnostic]: ParseCommandLineWorkerDiagnostics,
10961096
commandLine: readonly string[],
10971097
readFile?: (path: string) => string | undefined) {
10981098
const options = {} as OptionsBase;
@@ -1160,7 +1160,13 @@ namespace ts {
11601160
}
11611161
}
11621162
else {
1163-
errors.push(createCompilerDiagnostic(unknownOptionDiagnostic, s));
1163+
const possibleOption = getSpellingSuggestion(s, optionDeclarations, opt => `--${opt.name}`);
1164+
if (possibleOption) {
1165+
errors.push(createCompilerDiagnostic(unknownDidYouMeanDiagnostic, s, possibleOption.name));
1166+
}
1167+
else {
1168+
errors.push(createCompilerDiagnostic(unknownOptionDiagnostic, s));
1169+
}
11641170
}
11651171
}
11661172
else {
@@ -1206,6 +1212,7 @@ namespace ts {
12061212
export function parseCommandLine(commandLine: readonly string[], readFile?: (path: string) => string | undefined): ParsedCommandLine {
12071213
return parseCommandLineWorker(getOptionNameMap, [
12081214
Diagnostics.Unknown_compiler_option_0,
1215+
Diagnostics.Unknown_compiler_option_0_Did_you_mean_1,
12091216
Diagnostics.Compiler_option_0_expects_an_argument
12101217
], commandLine, readFile);
12111218
}
@@ -1241,6 +1248,7 @@ namespace ts {
12411248
const returnBuildOptionNameMap = () => (buildOptionNameMap || (buildOptionNameMap = createOptionNameMap(buildOpts)));
12421249
const { options, fileNames: projects, errors } = parseCommandLineWorker(returnBuildOptionNameMap, [
12431250
Diagnostics.Unknown_build_option_0,
1251+
Diagnostics.Unknown_compiler_option_0_Did_you_mean_1,
12441252
Diagnostics.Build_option_0_requires_a_value_of_type_1
12451253
], args);
12461254
const buildOptions = options as BuildOptions;

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3177,6 +3177,10 @@
31773177
"category": "Error",
31783178
"code": 5024
31793179
},
3180+
"Unknown compiler option '{0}'. Did you mean '{1}'?": {
3181+
"category": "Error",
3182+
"code": 5025
3183+
},
31803184
"Could not write file '{0}': {1}.": {
31813185
"category": "Error",
31823186
"code": 5033

0 commit comments

Comments
 (0)