@@ -691,8 +691,7 @@ namespace ts {
691
691
return typeAcquisition ;
692
692
}
693
693
694
- /* @internal */
695
- export function getOptionNameMap ( ) : OptionNameMap {
694
+ function getOptionNameMap ( ) : OptionNameMap {
696
695
if ( optionNameMapCache ) {
697
696
return optionNameMapCache ;
698
697
}
@@ -745,7 +744,6 @@ namespace ts {
745
744
const options : CompilerOptions = { } ;
746
745
const fileNames : string [ ] = [ ] ;
747
746
const errors : Diagnostic [ ] = [ ] ;
748
- const { optionNameMap, shortOptionNames } = getOptionNameMap ( ) ;
749
747
750
748
parseStrings ( commandLine ) ;
751
749
return {
@@ -757,21 +755,13 @@ namespace ts {
757
755
function parseStrings ( args : string [ ] ) {
758
756
let i = 0 ;
759
757
while ( i < args . length ) {
760
- let s = args [ i ] ;
758
+ const s = args [ i ] ;
761
759
i ++ ;
762
760
if ( s . charCodeAt ( 0 ) === CharacterCodes . at ) {
763
761
parseResponseFile ( s . slice ( 1 ) ) ;
764
762
}
765
763
else if ( s . charCodeAt ( 0 ) === CharacterCodes . minus ) {
766
- s = s . slice ( s . charCodeAt ( 1 ) === CharacterCodes . minus ? 2 : 1 ) . toLowerCase ( ) ;
767
-
768
- // Try to translate short option names to their full equivalents.
769
- const short = shortOptionNames . get ( s ) ;
770
- if ( short !== undefined ) {
771
- s = short ;
772
- }
773
-
774
- const opt = optionNameMap . get ( s ) ;
764
+ const opt = getOptionFromName ( s . slice ( s . charCodeAt ( 1 ) === CharacterCodes . minus ? 2 : 1 ) , /*allowShort*/ true ) ;
775
765
if ( opt ) {
776
766
if ( opt . isTSConfigOnly ) {
777
767
errors . push ( createCompilerDiagnostic ( Diagnostics . Option_0_can_only_be_specified_in_tsconfig_json_file , opt . name ) ) ;
@@ -859,6 +849,19 @@ namespace ts {
859
849
}
860
850
}
861
851
852
+ function getOptionFromName ( optionName : string , allowShort = false ) : CommandLineOption | undefined {
853
+ optionName = optionName . toLowerCase ( ) ;
854
+ const { optionNameMap, shortOptionNames } = getOptionNameMap ( ) ;
855
+ // Try to translate short option names to their full equivalents.
856
+ if ( allowShort ) {
857
+ const short = shortOptionNames . get ( optionName ) ;
858
+ if ( short !== undefined ) {
859
+ optionName = short ;
860
+ }
861
+ }
862
+ return optionNameMap . get ( optionName ) ;
863
+ }
864
+
862
865
/**
863
866
* Read tsconfig.json file
864
867
* @param fileName The path to the config file
@@ -1661,4 +1664,42 @@ namespace ts {
1661
1664
function caseInsensitiveKeyMapper ( key : string ) {
1662
1665
return key . toLowerCase ( ) ;
1663
1666
}
1664
- }
1667
+
1668
+ /**
1669
+ * Produces a cleaned version of compiler options with personally identifiying info (aka, paths) removed.
1670
+ * Also converts enum values back to strings.
1671
+ */
1672
+ /* @internal */
1673
+ export function convertCompilerOptionsForTelemetry ( opts : ts . CompilerOptions ) : ts . CompilerOptions {
1674
+ const out : ts . CompilerOptions = { } ;
1675
+ for ( const key in opts ) if ( opts . hasOwnProperty ( key ) ) {
1676
+ const type = getOptionFromName ( key ) ;
1677
+ if ( type !== undefined ) { // Ignore unknown options
1678
+ out [ key ] = getOptionValueWithEmptyStrings ( opts [ key ] , type ) ;
1679
+ }
1680
+ }
1681
+ return out ;
1682
+ }
1683
+
1684
+ function getOptionValueWithEmptyStrings ( value : any , option : CommandLineOption ) : { } {
1685
+ switch ( option . type ) {
1686
+ case "object" : // "paths". Can't get any useful information from the value since we blank out strings, so just return "".
1687
+ return "" ;
1688
+ case "string" : // Could be any arbitrary string -- use empty string instead.
1689
+ return "" ;
1690
+ case "number" : // Allow numbers, but be sure to check it's actually a number.
1691
+ return typeof value === "number" ? value : "" ;
1692
+ case "boolean" :
1693
+ return typeof value === "boolean" ? value : "" ;
1694
+ case "list" :
1695
+ const elementType = ( option as CommandLineOptionOfListType ) . element ;
1696
+ return ts . isArray ( value ) ? value . map ( v => getOptionValueWithEmptyStrings ( v , elementType ) ) : "" ;
1697
+ default :
1698
+ return ts . forEachEntry ( option . type , ( optionEnumValue , optionStringValue ) => {
1699
+ if ( optionEnumValue === value ) {
1700
+ return optionStringValue ;
1701
+ }
1702
+ } ) ;
1703
+ }
1704
+ }
1705
+ }
0 commit comments