@@ -692,8 +692,7 @@ namespace ts {
692692 return typeAcquisition ;
693693 }
694694
695- /* @internal */
696- export function getOptionNameMap ( ) : OptionNameMap {
695+ function getOptionNameMap ( ) : OptionNameMap {
697696 if ( optionNameMapCache ) {
698697 return optionNameMapCache ;
699698 }
@@ -750,7 +749,6 @@ namespace ts {
750749 const options : CompilerOptions = { } ;
751750 const fileNames : string [ ] = [ ] ;
752751 const errors : Diagnostic [ ] = [ ] ;
753- const { optionNameMap, shortOptionNames } = getOptionNameMap ( ) ;
754752
755753 parseStrings ( commandLine ) ;
756754 return {
@@ -762,21 +760,13 @@ namespace ts {
762760 function parseStrings ( args : string [ ] ) {
763761 let i = 0 ;
764762 while ( i < args . length ) {
765- let s = args [ i ] ;
763+ const s = args [ i ] ;
766764 i ++ ;
767765 if ( s . charCodeAt ( 0 ) === CharacterCodes . at ) {
768766 parseResponseFile ( s . slice ( 1 ) ) ;
769767 }
770768 else if ( s . charCodeAt ( 0 ) === CharacterCodes . minus ) {
771- s = s . slice ( s . charCodeAt ( 1 ) === CharacterCodes . minus ? 2 : 1 ) . toLowerCase ( ) ;
772-
773- // Try to translate short option names to their full equivalents.
774- const short = shortOptionNames . get ( s ) ;
775- if ( short !== undefined ) {
776- s = short ;
777- }
778-
779- const opt = optionNameMap . get ( s ) ;
769+ const opt = getOptionFromName ( s . slice ( s . charCodeAt ( 1 ) === CharacterCodes . minus ? 2 : 1 ) , /*allowShort*/ true ) ;
780770 if ( opt ) {
781771 if ( opt . isTSConfigOnly ) {
782772 errors . push ( createCompilerDiagnostic ( Diagnostics . Option_0_can_only_be_specified_in_tsconfig_json_file , opt . name ) ) ;
@@ -864,6 +854,19 @@ namespace ts {
864854 }
865855 }
866856
857+ function getOptionFromName ( optionName : string , allowShort = false ) : CommandLineOption | undefined {
858+ optionName = optionName . toLowerCase ( ) ;
859+ const { optionNameMap, shortOptionNames } = getOptionNameMap ( ) ;
860+ // Try to translate short option names to their full equivalents.
861+ if ( allowShort ) {
862+ const short = shortOptionNames . get ( optionName ) ;
863+ if ( short !== undefined ) {
864+ optionName = short ;
865+ }
866+ }
867+ return optionNameMap . get ( optionName ) ;
868+ }
869+
867870 /**
868871 * Read tsconfig.json file
869872 * @param fileName The path to the config file
@@ -2173,4 +2176,42 @@ namespace ts {
21732176 function caseInsensitiveKeyMapper ( key : string ) {
21742177 return key . toLowerCase ( ) ;
21752178 }
2179+
2180+ /**
2181+ * Produces a cleaned version of compiler options with personally identifiying info (aka, paths) removed.
2182+ * Also converts enum values back to strings.
2183+ */
2184+ /* @internal */
2185+ export function convertCompilerOptionsForTelemetry ( opts : ts . CompilerOptions ) : ts . CompilerOptions {
2186+ const out : ts . CompilerOptions = { } ;
2187+ for ( const key in opts ) if ( opts . hasOwnProperty ( key ) ) {
2188+ const type = getOptionFromName ( key ) ;
2189+ if ( type !== undefined ) { // Ignore unknown options
2190+ out [ key ] = getOptionValueWithEmptyStrings ( opts [ key ] , type ) ;
2191+ }
2192+ }
2193+ return out ;
2194+ }
2195+
2196+ function getOptionValueWithEmptyStrings ( value : any , option : CommandLineOption ) : { } {
2197+ switch ( option . type ) {
2198+ case "object" : // "paths". Can't get any useful information from the value since we blank out strings, so just return "".
2199+ return "" ;
2200+ case "string" : // Could be any arbitrary string -- use empty string instead.
2201+ return "" ;
2202+ case "number" : // Allow numbers, but be sure to check it's actually a number.
2203+ return typeof value === "number" ? value : "" ;
2204+ case "boolean" :
2205+ return typeof value === "boolean" ? value : "" ;
2206+ case "list" :
2207+ const elementType = ( option as CommandLineOptionOfListType ) . element ;
2208+ return ts . isArray ( value ) ? value . map ( v => getOptionValueWithEmptyStrings ( v , elementType ) ) : "" ;
2209+ default :
2210+ return ts . forEachEntry ( option . type , ( optionEnumValue , optionStringValue ) => {
2211+ if ( optionEnumValue === value ) {
2212+ return optionStringValue ;
2213+ }
2214+ } ) ;
2215+ }
2216+ }
21762217}
0 commit comments