@@ -62,7 +62,8 @@ namespace ts {
6262 /* @internal */
6363 export const libMap = createMapFromEntries ( libEntries ) ;
6464
65- const commonOptionsWithBuild : CommandLineOption [ ] = [
65+ /* @internal */
66+ export const commonOptionsWithBuild : CommandLineOption [ ] = [
6667 {
6768 name : "help" ,
6869 shortName : "h" ,
@@ -83,6 +84,18 @@ namespace ts {
8384 category : Diagnostics . Command_line_Options ,
8485 description : Diagnostics . Whether_to_keep_outdated_console_output_in_watch_mode_instead_of_clearing_the_screen ,
8586 } ,
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+ } ,
8699 {
87100 name : "watch" ,
88101 shortName : "w" ,
@@ -561,18 +574,6 @@ namespace ts {
561574 category : Diagnostics . Advanced_Options ,
562575 description : Diagnostics . Include_modules_imported_with_json_extension
563576 } ,
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- } ,
576577
577578 {
578579 name : "out" ,
@@ -903,17 +904,27 @@ namespace ts {
903904 }
904905 }
905906
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 ;
908921 const fileNames : string [ ] = [ ] ;
909- const projectReferences : ProjectReference [ ] | undefined = undefined ;
910922 const errors : Diagnostic [ ] = [ ] ;
911923
912924 parseStrings ( commandLine ) ;
913925 return {
914926 options,
915927 fileNames,
916- projectReferences,
917928 errors
918929 } ;
919930
@@ -926,15 +937,15 @@ namespace ts {
926937 parseResponseFile ( s . slice ( 1 ) ) ;
927938 }
928939 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 ) ;
930941 if ( opt ) {
931942 if ( opt . isTSConfigOnly ) {
932943 errors . push ( createCompilerDiagnostic ( Diagnostics . Option_0_can_only_be_specified_in_tsconfig_json_file , opt . name ) ) ;
933944 }
934945 else {
935946 // Check to see if no argument was provided (e.g. "--locale" is the last command-line argument).
936947 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 ) ) ;
938949 }
939950
940951 switch ( opt . type ) {
@@ -971,7 +982,7 @@ namespace ts {
971982 }
972983 }
973984 else {
974- errors . push ( createCompilerDiagnostic ( Diagnostics . Unknown_compiler_option_0 , s ) ) ;
985+ errors . push ( createCompilerDiagnostic ( unknownOptionDiagnostic , s ) ) ;
975986 }
976987 }
977988 else {
@@ -1014,13 +1025,19 @@ namespace ts {
10141025 }
10151026 }
10161027
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+
10171035 /** @internal */
10181036 export function getOptionFromName ( optionName : string , allowShort ?: boolean ) : CommandLineOption | undefined {
10191037 return getOptionDeclarationFromName ( getOptionNameMap , optionName , allowShort ) ;
10201038 }
10211039
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 {
10241041 optionName = optionName . toLowerCase ( ) ;
10251042 const { optionNameMap, shortOptionNames } = getOptionNameMap ( ) ;
10261043 // Try to translate short option names to their full equivalents.
@@ -1044,25 +1061,11 @@ namespace ts {
10441061 export function parseBuildCommand ( args : string [ ] ) : ParsedBuildCommand {
10451062 let buildOptionNameMap : OptionNameMap | undefined ;
10461063 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 ;
10661069
10671070 if ( projects . length === 0 ) {
10681071 // tsc -b invoked with no extra arguments; act as if invoked with "tsc -b ."
@@ -1071,19 +1074,19 @@ namespace ts {
10711074
10721075 // Nonsensical combinations
10731076 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" ) ) ;
10751078 }
10761079 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" ) ) ;
10781081 }
10791082 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" ) ) ;
10811084 }
10821085 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" ) ) ;
10841087 }
10851088
1086- return { buildOptions, projects, errors : errors || emptyArray } ;
1089+ return { buildOptions, projects, errors } ;
10871090 }
10881091
10891092 function getDiagnosticText ( _message : DiagnosticMessage , ..._args : any [ ] ) : string {
0 commit comments