@@ -312,23 +312,31 @@ namespace ts {
312312 if ( hasProperty ( optionNameMap , s ) ) {
313313 let opt = optionNameMap [ s ] ;
314314
315- if ( opt . type === "boolean" ) {
316- // This needs to be treated specially since it doesnt accept argument
317- options [ opt . name ] = true ;
315+ // Check to see if no argument was provided (e.g. "--locale" is the last command-line argument).
316+ if ( ! args [ i ] && opt . type !== "boolean" ) {
317+ errors . push ( createCompilerDiagnostic ( Diagnostics . Compiler_option_0_expects_an_argument , opt . name ) ) ;
318318 }
319- else {
320- // Check to see if no argument was provided (e.g. "--locale" is the last command-line argument).
321- if ( ! args [ i ] ) {
322- errors . push ( createCompilerDiagnostic ( Diagnostics . Compiler_option_0_expects_an_argument , opt . name ) ) ;
323- }
324319
325- let { hasError, value} = parseOption ( opt , args [ i ++ ] , options [ opt . name ] ) ;
326- if ( hasError ) {
327- errors . push ( createCompilerDiagnostic ( ( < CommandLineOptionOfCustomType > opt ) . error ) ) ;
328- }
329- else {
330- options [ opt . name ] = value ;
331- }
320+ switch ( opt . type ) {
321+ case "number" :
322+ options [ opt . name ] = parseInt ( args [ i ++ ] ) ;
323+ break ;
324+ case "boolean" :
325+ options [ opt . name ] = true ;
326+ break ;
327+ case "string" :
328+ options [ opt . name ] = args [ i ++ ] || "" ;
329+ break ;
330+ // If not a primitive, the possible types are specified in what is effectively a map of options.
331+ default :
332+ let map = < Map < number > > opt . type ;
333+ let key = ( args [ i ++ ] || "" ) . toLowerCase ( ) ;
334+ if ( hasProperty ( map , key ) ) {
335+ options [ opt . name ] = map [ key ] ;
336+ }
337+ else {
338+ errors . push ( createCompilerDiagnostic ( ( < CommandLineOptionOfCustomType > opt ) . error ) ) ;
339+ }
332340 }
333341 }
334342 else {
@@ -375,68 +383,6 @@ namespace ts {
375383 }
376384 }
377385
378- /**
379- * Parses non quoted strings separated by comma e.g. "a,b" would result in string array ["a", "b"]
380- * @param s
381- * @param existingValue
382- */
383- function parseMultiValueStringArray ( s : string , existingValue : string [ ] ) {
384- let value : string [ ] = existingValue || [ ] ;
385- let hasError = false ;
386- let currentString = "" ;
387- if ( s ) {
388- for ( let i = 0 ; i < s . length ; i ++ ) {
389- let ch = s . charCodeAt ( i ) ;
390- if ( ch === CharacterCodes . comma ) {
391- pushCurrentStringToResult ( ) ;
392- }
393- else {
394- currentString += s . charAt ( i ) ;
395- }
396- }
397- // push last string
398- pushCurrentStringToResult ( ) ;
399- }
400- return { value, hasError } ;
401-
402- function pushCurrentStringToResult ( ) {
403- if ( currentString ) {
404- value . push ( currentString ) ;
405- currentString = "" ;
406- }
407- else {
408- hasError = true ;
409- }
410- }
411- }
412-
413- /* @internal */
414- export function parseOption ( option : CommandLineOption , stringValue : string , existingValue : CompilerOptionsValueType ) {
415- let hasError : boolean ;
416- let value : CompilerOptionsValueType ;
417- switch ( option . type ) {
418- case "number" :
419- value = parseInt ( stringValue ) ;
420- break ;
421- case "string" :
422- value = stringValue || "" ;
423- break ;
424- case "string[]" :
425- return parseMultiValueStringArray ( stringValue , < string [ ] > existingValue ) ;
426- // If not a primitive, the possible types are specified in what is effectively a map of options.
427- default :
428- let map = < Map < number > > option . type ;
429- let key = ( stringValue || "" ) . toLowerCase ( ) ;
430- if ( hasProperty ( map , key ) ) {
431- value = map [ key ] ;
432- }
433- else {
434- hasError = true ;
435- }
436- }
437- return { hasError, value } ;
438- }
439-
440386 /**
441387 * Read tsconfig.json file
442388 * @param fileName The path to the config file
@@ -466,44 +412,11 @@ namespace ts {
466412 }
467413 }
468414
469- /* @internal */
470- export function parseJsonCompilerOption ( opt : CommandLineOption , jsonValue : any , errors : Diagnostic [ ] ) {
471- let optType = opt . type ;
472- let expectedType = typeof optType === "string" ? optType : "string" ;
473- let hasValidValue = true ;
474- if ( typeof jsonValue === expectedType ) {
475- if ( typeof optType !== "string" ) {
476- let key = jsonValue . toLowerCase ( ) ;
477- if ( hasProperty ( optType , key ) ) {
478- jsonValue = optType [ key ] ;
479- }
480- else {
481- errors . push ( createCompilerDiagnostic ( ( < CommandLineOptionOfCustomType > opt ) . error ) ) ;
482- jsonValue = 0 ;
483- }
484- }
485- }
486- // Check if the value asked was string[] and value provided was not string[]
487- else if ( expectedType !== "string[]" ||
488- ! ( jsonValue instanceof Array ) ||
489- forEach ( < string [ ] > jsonValue , individualValue => typeof individualValue !== "string" ) ) {
490- // Not expectedType
491- errors . push ( createCompilerDiagnostic ( Diagnostics . Compiler_option_0_requires_a_value_of_type_1 , opt . name , expectedType ) ) ;
492- hasValidValue = false ;
493- }
494-
495- return {
496- value : < CompilerOptionsValueType > jsonValue ,
497- hasValidValue
498- } ;
499- }
500-
501415 /**
502416 * Parse the contents of a config file (tsconfig.json).
503417 * @param json The contents of the config file to parse
504418 * @param basePath A root directory to resolve relative path entries in the config
505419 * file to. e.g. outDir
506- * @param existingOptions optional existing options to extend into
507420 */
508421 export function parseJsonConfigFileContent ( json : any , host : ParseConfigHost , basePath : string , existingOptions : CompilerOptions = { } ) : ParsedCommandLine {
509422 let errors : Diagnostic [ ] = [ ] ;
@@ -526,16 +439,31 @@ namespace ts {
526439 for ( let id in jsonOptions ) {
527440 if ( hasProperty ( optionNameMap , id ) ) {
528441 let opt = optionNameMap [ id ] ;
529- let { hasValidValue, value } = parseJsonCompilerOption ( opt , jsonOptions [ id ] , errors ) ;
530- if ( hasValidValue ) {
442+ let optType = opt . type ;
443+ let value = jsonOptions [ id ] ;
444+ let expectedType = typeof optType === "string" ? optType : "string" ;
445+ if ( typeof value === expectedType ) {
446+ if ( typeof optType !== "string" ) {
447+ let key = value . toLowerCase ( ) ;
448+ if ( hasProperty ( optType , key ) ) {
449+ value = optType [ key ] ;
450+ }
451+ else {
452+ errors . push ( createCompilerDiagnostic ( ( < CommandLineOptionOfCustomType > opt ) . error ) ) ;
453+ value = 0 ;
454+ }
455+ }
531456 if ( opt . isFilePath ) {
532- value = normalizePath ( combinePaths ( basePath , < string > value ) ) ;
457+ value = normalizePath ( combinePaths ( basePath , value ) ) ;
533458 if ( value === "" ) {
534459 value = "." ;
535460 }
536461 }
537462 options [ opt . name ] = value ;
538463 }
464+ else {
465+ errors . push ( createCompilerDiagnostic ( Diagnostics . Compiler_option_0_requires_a_value_of_type_1 , id , expectedType ) ) ;
466+ }
539467 }
540468 else {
541469 errors . push ( createCompilerDiagnostic ( Diagnostics . Unknown_compiler_option_0 , id ) ) ;
0 commit comments