@@ -980,7 +980,12 @@ namespace ts {
980
980
}
981
981
}
982
982
983
+ function makePadding ( paddingLength : number ) : string {
984
+ return Array ( paddingLength + 1 ) . join ( " " ) ;
985
+ }
986
+
983
987
function writeConfigurations ( ) {
988
+ // Filter applicable options to place in the file
984
989
const categorizedOptions = reduceLeft (
985
990
filter ( optionDeclarations , o => o . category !== Diagnostics . CommandLine_Options && o . category !== Diagnostics . Advanced_Options ) ,
986
991
( memo , value ) => {
@@ -990,44 +995,59 @@ namespace ts {
990
995
}
991
996
return memo ;
992
997
} , < MapLike < CommandLineOption [ ] > > { } ) ;
993
- const knownKesyCount = getOwnKeys ( configurations . compilerOptions ) . length ;
994
998
995
- const newLine = "\n" ;
996
- const tab = " " ;
997
-
998
- let result = "" ;
999
+ // Serialize all options and thier descriptions
1000
+ let marginLength = 0 ;
999
1001
let seenKnownKeys = 0 ;
1000
- result += `{${ newLine } ` ;
1001
- result += `${ tab } "compilerOptions": {${ newLine } ` ;
1002
+ const nameColumn : string [ ] = [ ] ;
1003
+ const descriptionColumn : string [ ] = [ ] ;
1004
+ const knownKesyCount = getOwnKeys ( configurations . compilerOptions ) . length ;
1002
1005
for ( const category in categorizedOptions ) {
1003
- result += `${ tab } ${ tab } // ${ category } ${ newLine } ` ;
1004
- result += `${ newLine } ` ;
1006
+ if ( nameColumn . length !== 0 ) {
1007
+ nameColumn . push ( "" ) ;
1008
+ descriptionColumn . push ( "" ) ;
1009
+ }
1010
+ nameColumn . push ( `/* ${ category } */` ) ;
1011
+ descriptionColumn . push ( "" ) ;
1005
1012
for ( const option of categorizedOptions [ category ] ) {
1006
- result += ` ${ tab } ${ tab } // ${ option . description && getLocaleSpecificMessage ( option . description ) || option . name } ${ newLine } ` ;
1013
+ let optionName ;
1007
1014
if ( configurations . compilerOptions [ option . name ] ) {
1008
- result + = `${ tab } ${ tab } "${ option . name } ": ${ JSON . stringify ( configurations . compilerOptions [ option . name ] ) } ${ ( seenKnownKeys += 1 ) === knownKesyCount ? "" : "," } ${ newLine } ` ;
1015
+ optionName = `"${ option . name } ": ${ JSON . stringify ( configurations . compilerOptions [ option . name ] ) } ${ ( seenKnownKeys += 1 ) === knownKesyCount ? "" : "," } ` ;
1009
1016
}
1010
1017
else {
1011
- result + = `${ tab } ${ tab } // "${ option . name } ": ${ JSON . stringify ( getDefaultValueForOption ( option ) ) } ,${ newLine } ` ;
1018
+ optionName = `// "${ option . name } ": ${ JSON . stringify ( getDefaultValueForOption ( option ) ) } ,` ;
1012
1019
}
1013
- result += `${ newLine } ` ;
1020
+ nameColumn . push ( optionName ) ;
1021
+ descriptionColumn . push ( `/* ${ option . description && getLocaleSpecificMessage ( option . description ) || option . name } */` ) ;
1022
+ marginLength = Math . max ( optionName . length , marginLength ) ;
1014
1023
}
1015
- result += `${ newLine } ` ;
1024
+ }
1025
+
1026
+ // Write the output
1027
+ const tab = makePadding ( 2 ) ;
1028
+ const result : string [ ] = [ ] ;
1029
+ result . push ( `{` ) ;
1030
+ result . push ( `${ tab } "compilerOptions": {` ) ;
1031
+ // Print out each row, aligning all the descriptions on the same column.
1032
+ for ( let i = 0 ; i < nameColumn . length ; i ++ ) {
1033
+ const optionName = nameColumn [ i ] ;
1034
+ const description = descriptionColumn [ i ] ;
1035
+ result . push ( tab + tab + optionName + makePadding ( marginLength - optionName . length + 2 ) + description ) ;
1016
1036
}
1017
1037
if ( configurations . files && configurations . files . length ) {
1018
- result += `${ tab } },${ newLine } ` ;
1019
- result += `${ tab } "files": [${ newLine } ` ;
1038
+ result . push ( `${ tab } },` ) ;
1039
+ result . push ( `${ tab } "files": [` ) ;
1020
1040
for ( let i = 0 ; i < configurations . files . length ; i ++ ) {
1021
- result += `${ tab } ${ tab } ${ JSON . stringify ( configurations . files [ i ] ) } ${ i === configurations . files . length - 1 ? "" : "," } ${ newLine } ` ;
1041
+ result . push ( `${ tab } ${ tab } ${ JSON . stringify ( configurations . files [ i ] ) } ${ i === configurations . files . length - 1 ? "" : "," } ` ) ;
1022
1042
}
1023
- result += `${ tab } ]${ newLine } ` ;
1043
+ result . push ( `${ tab } ]` ) ;
1024
1044
}
1025
1045
else {
1026
- result += `${ tab } }${ newLine } ` ;
1046
+ result . push ( `${ tab } }` ) ;
1027
1047
}
1028
- result += `} ${ newLine } ` ;
1048
+ result . push ( `}` ) ;
1029
1049
1030
- return result ;
1050
+ return result . join ( sys . newLine ) ;
1031
1051
}
1032
1052
}
1033
1053
0 commit comments