@@ -62,7 +62,8 @@ namespace ts {
62
62
/* @internal */
63
63
export const libMap = createMapFromEntries ( libEntries ) ;
64
64
65
- const commonOptionsWithBuild : CommandLineOption [ ] = [
65
+ /* @internal */
66
+ export const commonOptionsWithBuild : CommandLineOption [ ] = [
66
67
{
67
68
name : "help" ,
68
69
shortName : "h" ,
@@ -83,6 +84,18 @@ namespace ts {
83
84
category : Diagnostics . Command_line_Options ,
84
85
description : Diagnostics . Whether_to_keep_outdated_console_output_in_watch_mode_instead_of_clearing_the_screen ,
85
86
} ,
87
+ {
88
+ name : "listFiles" ,
89
+ type : "boolean" ,
90
+ category : Diagnostics . Advanced_Options ,
91
+ description : Diagnostics . Print_names_of_files_part_of_the_compilation
92
+ } ,
93
+ {
94
+ name : "listEmittedFiles" ,
95
+ type : "boolean" ,
96
+ category : Diagnostics . Advanced_Options ,
97
+ description : Diagnostics . Print_names_of_generated_files_part_of_the_compilation
98
+ } ,
86
99
{
87
100
name : "watch" ,
88
101
shortName : "w" ,
@@ -561,18 +574,6 @@ namespace ts {
561
574
category : Diagnostics . Advanced_Options ,
562
575
description : Diagnostics . Include_modules_imported_with_json_extension
563
576
} ,
564
- {
565
- name : "listFiles" ,
566
- type : "boolean" ,
567
- category : Diagnostics . Advanced_Options ,
568
- description : Diagnostics . Print_names_of_files_part_of_the_compilation
569
- } ,
570
- {
571
- name : "listEmittedFiles" ,
572
- type : "boolean" ,
573
- category : Diagnostics . Advanced_Options ,
574
- description : Diagnostics . Print_names_of_generated_files_part_of_the_compilation
575
- } ,
576
577
577
578
{
578
579
name : "out" ,
@@ -903,17 +904,27 @@ namespace ts {
903
904
}
904
905
}
905
906
906
- export function parseCommandLine ( commandLine : ReadonlyArray < string > , readFile ?: ( path : string ) => string | undefined ) : ParsedCommandLine {
907
- const options : CompilerOptions = { } ;
907
+ /* @internal */
908
+ export interface OptionsBase {
909
+ [ option : string ] : CompilerOptionsValue | undefined ;
910
+ }
911
+
912
+ /** Tuple with error messages for 'unknown compiler option', 'option requires type' */
913
+ type ParseCommandLineWorkerDiagnostics = [ DiagnosticMessage , DiagnosticMessage ] ;
914
+
915
+ function parseCommandLineWorker (
916
+ getOptionNameMap : ( ) => OptionNameMap ,
917
+ [ unknownOptionDiagnostic , optionTypeMismatchDiagnostic ] : ParseCommandLineWorkerDiagnostics ,
918
+ commandLine : ReadonlyArray < string > ,
919
+ readFile ?: ( path : string ) => string | undefined ) {
920
+ const options = { } as OptionsBase ;
908
921
const fileNames : string [ ] = [ ] ;
909
- const projectReferences : ProjectReference [ ] | undefined = undefined ;
910
922
const errors : Diagnostic [ ] = [ ] ;
911
923
912
924
parseStrings ( commandLine ) ;
913
925
return {
914
926
options,
915
927
fileNames,
916
- projectReferences,
917
928
errors
918
929
} ;
919
930
@@ -926,15 +937,15 @@ namespace ts {
926
937
parseResponseFile ( s . slice ( 1 ) ) ;
927
938
}
928
939
else if ( s . charCodeAt ( 0 ) === CharacterCodes . minus ) {
929
- const opt = getOptionFromName ( s . slice ( s . charCodeAt ( 1 ) === CharacterCodes . minus ? 2 : 1 ) , /*allowShort*/ true ) ;
940
+ const opt = getOptionDeclarationFromName ( getOptionNameMap , s . slice ( s . charCodeAt ( 1 ) === CharacterCodes . minus ? 2 : 1 ) , /*allowShort*/ true ) ;
930
941
if ( opt ) {
931
942
if ( opt . isTSConfigOnly ) {
932
943
errors . push ( createCompilerDiagnostic ( Diagnostics . Option_0_can_only_be_specified_in_tsconfig_json_file , opt . name ) ) ;
933
944
}
934
945
else {
935
946
// Check to see if no argument was provided (e.g. "--locale" is the last command-line argument).
936
947
if ( ! args [ i ] && opt . type !== "boolean" ) {
937
- errors . push ( createCompilerDiagnostic ( Diagnostics . Compiler_option_0_expects_an_argument , opt . name ) ) ;
948
+ errors . push ( createCompilerDiagnostic ( optionTypeMismatchDiagnostic , opt . name ) ) ;
938
949
}
939
950
940
951
switch ( opt . type ) {
@@ -971,7 +982,7 @@ namespace ts {
971
982
}
972
983
}
973
984
else {
974
- errors . push ( createCompilerDiagnostic ( Diagnostics . Unknown_compiler_option_0 , s ) ) ;
985
+ errors . push ( createCompilerDiagnostic ( unknownOptionDiagnostic , s ) ) ;
975
986
}
976
987
}
977
988
else {
@@ -1014,13 +1025,19 @@ namespace ts {
1014
1025
}
1015
1026
}
1016
1027
1028
+ export function parseCommandLine ( commandLine : ReadonlyArray < string > , readFile ?: ( path : string ) => string | undefined ) : ParsedCommandLine {
1029
+ return parseCommandLineWorker ( getOptionNameMap , [
1030
+ Diagnostics . Unknown_compiler_option_0 ,
1031
+ Diagnostics . Compiler_option_0_expects_an_argument
1032
+ ] , commandLine , readFile ) ;
1033
+ }
1034
+
1017
1035
/** @internal */
1018
1036
export function getOptionFromName ( optionName : string , allowShort ?: boolean ) : CommandLineOption | undefined {
1019
1037
return getOptionDeclarationFromName ( getOptionNameMap , optionName , allowShort ) ;
1020
1038
}
1021
1039
1022
- /*@internal */
1023
- export function getOptionDeclarationFromName ( getOptionNameMap : ( ) => OptionNameMap , optionName : string , allowShort = false ) : CommandLineOption | undefined {
1040
+ function getOptionDeclarationFromName ( getOptionNameMap : ( ) => OptionNameMap , optionName : string , allowShort = false ) : CommandLineOption | undefined {
1024
1041
optionName = optionName . toLowerCase ( ) ;
1025
1042
const { optionNameMap, shortOptionNames } = getOptionNameMap ( ) ;
1026
1043
// Try to translate short option names to their full equivalents.
@@ -1044,25 +1061,11 @@ namespace ts {
1044
1061
export function parseBuildCommand ( args : string [ ] ) : ParsedBuildCommand {
1045
1062
let buildOptionNameMap : OptionNameMap | undefined ;
1046
1063
const returnBuildOptionNameMap = ( ) => ( buildOptionNameMap || ( buildOptionNameMap = createOptionNameMap ( buildOpts ) ) ) ;
1047
-
1048
- const buildOptions : BuildOptions = { } ;
1049
- const projects : string [ ] = [ ] ;
1050
- let errors : Diagnostic [ ] | undefined ;
1051
- for ( const arg of args ) {
1052
- if ( arg . charCodeAt ( 0 ) === CharacterCodes . minus ) {
1053
- const opt = getOptionDeclarationFromName ( returnBuildOptionNameMap , arg . slice ( arg . charCodeAt ( 1 ) === CharacterCodes . minus ? 2 : 1 ) , /*allowShort*/ true ) ;
1054
- if ( opt ) {
1055
- buildOptions [ opt . name as keyof BuildOptions ] = true ;
1056
- }
1057
- else {
1058
- ( errors || ( errors = [ ] ) ) . push ( createCompilerDiagnostic ( Diagnostics . Unknown_build_option_0 , arg ) ) ;
1059
- }
1060
- }
1061
- else {
1062
- // Not a flag, parse as filename
1063
- projects . push ( arg ) ;
1064
- }
1065
- }
1064
+ const { options, fileNames : projects , errors } = parseCommandLineWorker ( returnBuildOptionNameMap , [
1065
+ Diagnostics . Unknown_build_option_0 ,
1066
+ Diagnostics . Build_option_0_requires_a_value_of_type_1
1067
+ ] , args ) ;
1068
+ const buildOptions = options as BuildOptions ;
1066
1069
1067
1070
if ( projects . length === 0 ) {
1068
1071
// tsc -b invoked with no extra arguments; act as if invoked with "tsc -b ."
@@ -1071,19 +1074,19 @@ namespace ts {
1071
1074
1072
1075
// Nonsensical combinations
1073
1076
if ( buildOptions . clean && buildOptions . force ) {
1074
- ( errors || ( errors = [ ] ) ) . push ( createCompilerDiagnostic ( Diagnostics . Options_0_and_1_cannot_be_combined , "clean" , "force" ) ) ;
1077
+ errors . push ( createCompilerDiagnostic ( Diagnostics . Options_0_and_1_cannot_be_combined , "clean" , "force" ) ) ;
1075
1078
}
1076
1079
if ( buildOptions . clean && buildOptions . verbose ) {
1077
- ( errors || ( errors = [ ] ) ) . push ( createCompilerDiagnostic ( Diagnostics . Options_0_and_1_cannot_be_combined , "clean" , "verbose" ) ) ;
1080
+ errors . push ( createCompilerDiagnostic ( Diagnostics . Options_0_and_1_cannot_be_combined , "clean" , "verbose" ) ) ;
1078
1081
}
1079
1082
if ( buildOptions . clean && buildOptions . watch ) {
1080
- ( errors || ( errors = [ ] ) ) . push ( createCompilerDiagnostic ( Diagnostics . Options_0_and_1_cannot_be_combined , "clean" , "watch" ) ) ;
1083
+ errors . push ( createCompilerDiagnostic ( Diagnostics . Options_0_and_1_cannot_be_combined , "clean" , "watch" ) ) ;
1081
1084
}
1082
1085
if ( buildOptions . watch && buildOptions . dry ) {
1083
- ( errors || ( errors = [ ] ) ) . push ( createCompilerDiagnostic ( Diagnostics . Options_0_and_1_cannot_be_combined , "watch" , "dry" ) ) ;
1086
+ errors . push ( createCompilerDiagnostic ( Diagnostics . Options_0_and_1_cannot_be_combined , "watch" , "dry" ) ) ;
1084
1087
}
1085
1088
1086
- return { buildOptions, projects, errors : errors || emptyArray } ;
1089
+ return { buildOptions, projects, errors } ;
1087
1090
}
1088
1091
1089
1092
function getDiagnosticText ( _message : DiagnosticMessage , ..._args : any [ ] ) : string {
0 commit comments