@@ -501,6 +501,12 @@ namespace ts {
501
501
category : Diagnostics . Advanced_Options ,
502
502
description : Diagnostics . Enable_tracing_of_the_name_resolution_process
503
503
} ,
504
+ {
505
+ name : "resolveJsonModule" ,
506
+ type : "boolean" ,
507
+ category : Diagnostics . Advanced_Options ,
508
+ description : Diagnostics . Include_modules_imported_with_json_extension
509
+ } ,
504
510
{
505
511
name : "listFiles" ,
506
512
type : "boolean" ,
@@ -953,9 +959,9 @@ namespace ts {
953
959
* Read tsconfig.json file
954
960
* @param fileName The path to the config file
955
961
*/
956
- export function readJsonConfigFile ( fileName : string , readFile : ( path : string ) => string | undefined ) : JsonSourceFile {
962
+ export function readJsonConfigFile ( fileName : string , readFile : ( path : string ) => string | undefined ) : TsConfigSourceFile {
957
963
const textOrDiagnostic = tryReadFile ( fileName , readFile ) ;
958
- return isString ( textOrDiagnostic ) ? parseJsonText ( fileName , textOrDiagnostic ) : < JsonSourceFile > { parseDiagnostics : [ textOrDiagnostic ] } ;
964
+ return isString ( textOrDiagnostic ) ? parseJsonText ( fileName , textOrDiagnostic ) : < TsConfigSourceFile > { parseDiagnostics : [ textOrDiagnostic ] } ;
959
965
}
960
966
961
967
function tryReadFile ( fileName : string , readFile : ( path : string ) => string | undefined ) : string | Diagnostic {
@@ -973,58 +979,62 @@ namespace ts {
973
979
return arrayToMap ( options , option => option . name ) ;
974
980
}
975
981
976
- let _tsconfigRootOptions : Map < CommandLineOption > ;
982
+ let _tsconfigRootOptions : TsConfigOnlyOption ;
977
983
function getTsconfigRootOptionsMap ( ) {
978
984
if ( _tsconfigRootOptions === undefined ) {
979
- _tsconfigRootOptions = commandLineOptionsToMap ( [
980
- {
981
- name : "compilerOptions" ,
982
- type : "object" ,
983
- elementOptions : commandLineOptionsToMap ( optionDeclarations ) ,
984
- extraKeyDiagnosticMessage : Diagnostics . Unknown_compiler_option_0
985
- } ,
986
- {
987
- name : "typingOptions" ,
988
- type : "object" ,
989
- elementOptions : commandLineOptionsToMap ( typeAcquisitionDeclarations ) ,
990
- extraKeyDiagnosticMessage : Diagnostics . Unknown_type_acquisition_option_0
991
- } ,
992
- {
993
- name : "typeAcquisition" ,
994
- type : "object" ,
995
- elementOptions : commandLineOptionsToMap ( typeAcquisitionDeclarations ) ,
996
- extraKeyDiagnosticMessage : Diagnostics . Unknown_type_acquisition_option_0
997
- } ,
998
- {
999
- name : "extends" ,
1000
- type : "string"
1001
- } ,
1002
- {
1003
- name : "files" ,
1004
- type : "list" ,
1005
- element : {
1006
- name : "files" ,
985
+ _tsconfigRootOptions = {
986
+ name : undefined , // should never be needed since this is root
987
+ type : "object" ,
988
+ elementOptions : commandLineOptionsToMap ( [
989
+ {
990
+ name : "compilerOptions" ,
991
+ type : "object" ,
992
+ elementOptions : commandLineOptionsToMap ( optionDeclarations ) ,
993
+ extraKeyDiagnosticMessage : Diagnostics . Unknown_compiler_option_0
994
+ } ,
995
+ {
996
+ name : "typingOptions" ,
997
+ type : "object" ,
998
+ elementOptions : commandLineOptionsToMap ( typeAcquisitionDeclarations ) ,
999
+ extraKeyDiagnosticMessage : Diagnostics . Unknown_type_acquisition_option_0
1000
+ } ,
1001
+ {
1002
+ name : "typeAcquisition" ,
1003
+ type : "object" ,
1004
+ elementOptions : commandLineOptionsToMap ( typeAcquisitionDeclarations ) ,
1005
+ extraKeyDiagnosticMessage : Diagnostics . Unknown_type_acquisition_option_0
1006
+ } ,
1007
+ {
1008
+ name : "extends" ,
1007
1009
type : "string"
1008
- }
1009
- } ,
1010
- {
1011
- name : "include" ,
1012
- type : "list" ,
1013
- element : {
1010
+ } ,
1011
+ {
1012
+ name : "files" ,
1013
+ type : "list" ,
1014
+ element : {
1015
+ name : "files" ,
1016
+ type : "string"
1017
+ }
1018
+ } ,
1019
+ {
1014
1020
name : "include" ,
1015
- type : "string"
1016
- }
1017
- } ,
1018
- {
1019
- name : "exclude" ,
1020
- type : "list" ,
1021
- element : {
1021
+ type : "list" ,
1022
+ element : {
1023
+ name : "include" ,
1024
+ type : "string"
1025
+ }
1026
+ } ,
1027
+ {
1022
1028
name : "exclude" ,
1023
- type : "string"
1024
- }
1025
- } ,
1026
- compileOnSaveCommandLineOption
1027
- ] ) ;
1029
+ type : "list" ,
1030
+ element : {
1031
+ name : "exclude" ,
1032
+ type : "string"
1033
+ }
1034
+ } ,
1035
+ compileOnSaveCommandLineOption
1036
+ ] )
1037
+ } ;
1028
1038
}
1029
1039
return _tsconfigRootOptions ;
1030
1040
}
@@ -1061,31 +1071,38 @@ namespace ts {
1061
1071
* Convert the json syntax tree into the json value
1062
1072
*/
1063
1073
export function convertToObject ( sourceFile : JsonSourceFile , errors : Push < Diagnostic > ) : any {
1064
- return convertToObjectWorker ( sourceFile , errors , /*knownRootOptions*/ undefined , /*jsonConversionNotifier*/ undefined ) ;
1074
+ return convertToObjectWorker ( sourceFile , errors , /*returnValue*/ true , /* knownRootOptions*/ undefined , /*jsonConversionNotifier*/ undefined ) ;
1065
1075
}
1066
1076
1067
1077
/**
1068
- * Convert the json syntax tree into the json value
1078
+ * Convert the json syntax tree into the json value and report errors
1079
+ * This returns the json value (apart from checking errors) only if returnValue provided is true.
1080
+ * Otherwise it just checks the errors and returns undefined
1069
1081
*/
1070
- function convertToObjectWorker (
1082
+ /*@internal */
1083
+ export function convertToObjectWorker (
1071
1084
sourceFile : JsonSourceFile ,
1072
1085
errors : Push < Diagnostic > ,
1073
- knownRootOptions : Map < CommandLineOption > | undefined ,
1086
+ returnValue : boolean ,
1087
+ knownRootOptions : CommandLineOption | undefined ,
1074
1088
jsonConversionNotifier : JsonConversionNotifier | undefined ) : any {
1075
- if ( ! sourceFile . jsonObject ) {
1076
- return { } ;
1089
+ if ( ! sourceFile . statements . length ) {
1090
+ return returnValue ? { } : undefined ;
1077
1091
}
1078
1092
1079
- return convertObjectLiteralExpressionToJson ( sourceFile . jsonObject , knownRootOptions ,
1080
- /*extraKeyDiagnosticMessage*/ undefined , /*parentOption*/ undefined ) ;
1093
+ return convertPropertyValueToJson ( sourceFile . statements [ 0 ] . expression , knownRootOptions ) ;
1094
+
1095
+ function isRootOptionMap ( knownOptions : Map < CommandLineOption > | undefined ) {
1096
+ return knownRootOptions && ( knownRootOptions as TsConfigOnlyOption ) . elementOptions === knownOptions ;
1097
+ }
1081
1098
1082
1099
function convertObjectLiteralExpressionToJson (
1083
1100
node : ObjectLiteralExpression ,
1084
1101
knownOptions : Map < CommandLineOption > | undefined ,
1085
1102
extraKeyDiagnosticMessage : DiagnosticMessage | undefined ,
1086
1103
parentOption : string | undefined
1087
1104
) : any {
1088
- const result : any = { } ;
1105
+ const result : any = returnValue ? { } : undefined ;
1089
1106
for ( const element of node . properties ) {
1090
1107
if ( element . kind !== SyntaxKind . PropertyAssignment ) {
1091
1108
errors . push ( createDiagnosticForNodeInSourceFile ( sourceFile , element , Diagnostics . Property_assignment_expected ) ) ;
@@ -1106,19 +1123,21 @@ namespace ts {
1106
1123
}
1107
1124
const value = convertPropertyValueToJson ( element . initializer , option ) ;
1108
1125
if ( typeof keyText !== "undefined" ) {
1109
- result [ keyText ] = value ;
1126
+ if ( returnValue ) {
1127
+ result [ keyText ] = value ;
1128
+ }
1110
1129
// Notify key value set, if user asked for it
1111
1130
if ( jsonConversionNotifier &&
1112
1131
// Current callbacks are only on known parent option or if we are setting values in the root
1113
- ( parentOption || knownOptions === knownRootOptions ) ) {
1132
+ ( parentOption || isRootOptionMap ( knownOptions ) ) ) {
1114
1133
const isValidOptionValue = isCompilerOptionsValue ( option , value ) ;
1115
1134
if ( parentOption ) {
1116
1135
if ( isValidOptionValue ) {
1117
1136
// Notify option set in the parent if its a valid option value
1118
1137
jsonConversionNotifier . onSetValidOptionKeyValueInParent ( parentOption , option , value ) ;
1119
1138
}
1120
1139
}
1121
- else if ( knownOptions === knownRootOptions ) {
1140
+ else if ( isRootOptionMap ( knownOptions ) ) {
1122
1141
if ( isValidOptionValue ) {
1123
1142
// Notify about the valid root key value being set
1124
1143
jsonConversionNotifier . onSetValidOptionKeyValueInRoot ( keyText , element . name , value , element . initializer ) ;
@@ -1137,8 +1156,8 @@ namespace ts {
1137
1156
function convertArrayLiteralExpressionToJson (
1138
1157
elements : NodeArray < Expression > ,
1139
1158
elementOption : CommandLineOption | undefined
1140
- ) : any [ ] {
1141
- return elements . map ( element => convertPropertyValueToJson ( element , elementOption ) ) ;
1159
+ ) : any [ ] | void {
1160
+ return ( returnValue ? elements . map : elements . forEach ) . call ( elements , ( element : Expression ) => convertPropertyValueToJson ( element , elementOption ) ) ;
1142
1161
}
1143
1162
1144
1163
function convertPropertyValueToJson ( valueExpression : Expression , option : CommandLineOption ) : any {
@@ -1433,12 +1452,12 @@ namespace ts {
1433
1452
* @param basePath A root directory to resolve relative path entries in the config
1434
1453
* file to. e.g. outDir
1435
1454
*/
1436
- export function parseJsonSourceFileConfigFileContent ( sourceFile : JsonSourceFile , host : ParseConfigHost , basePath : string , existingOptions ?: CompilerOptions , configFileName ?: string , resolutionStack ?: Path [ ] , extraFileExtensions ?: ReadonlyArray < FileExtensionInfo > ) : ParsedCommandLine {
1455
+ export function parseJsonSourceFileConfigFileContent ( sourceFile : TsConfigSourceFile , host : ParseConfigHost , basePath : string , existingOptions ?: CompilerOptions , configFileName ?: string , resolutionStack ?: Path [ ] , extraFileExtensions ?: ReadonlyArray < FileExtensionInfo > ) : ParsedCommandLine {
1437
1456
return parseJsonConfigFileContentWorker ( /*json*/ undefined , sourceFile , host , basePath , existingOptions , configFileName , resolutionStack , extraFileExtensions ) ;
1438
1457
}
1439
1458
1440
1459
/*@internal */
1441
- export function setConfigFileInOptions ( options : CompilerOptions , configFile : JsonSourceFile ) {
1460
+ export function setConfigFileInOptions ( options : CompilerOptions , configFile : TsConfigSourceFile ) {
1442
1461
if ( configFile ) {
1443
1462
Object . defineProperty ( options , "configFile" , { enumerable : false , writable : false , value : configFile } ) ;
1444
1463
}
@@ -1466,7 +1485,7 @@ namespace ts {
1466
1485
*/
1467
1486
function parseJsonConfigFileContentWorker (
1468
1487
json : any ,
1469
- sourceFile : JsonSourceFile ,
1488
+ sourceFile : TsConfigSourceFile ,
1470
1489
host : ParseConfigHost ,
1471
1490
basePath : string ,
1472
1491
existingOptions : CompilerOptions = { } ,
@@ -1587,7 +1606,7 @@ namespace ts {
1587
1606
*/
1588
1607
function parseConfig (
1589
1608
json : any ,
1590
- sourceFile : JsonSourceFile ,
1609
+ sourceFile : TsConfigSourceFile ,
1591
1610
host : ParseConfigHost ,
1592
1611
basePath : string ,
1593
1612
configFileName : string ,
@@ -1664,7 +1683,7 @@ namespace ts {
1664
1683
}
1665
1684
1666
1685
function parseOwnConfigOfJsonSourceFile (
1667
- sourceFile : JsonSourceFile ,
1686
+ sourceFile : TsConfigSourceFile ,
1668
1687
host : ParseConfigHost ,
1669
1688
basePath : string ,
1670
1689
configFileName : string | undefined ,
@@ -1711,7 +1730,7 @@ namespace ts {
1711
1730
}
1712
1731
}
1713
1732
} ;
1714
- const json = convertToObjectWorker ( sourceFile , errors , getTsconfigRootOptionsMap ( ) , optionsIterator ) ;
1733
+ const json = convertToObjectWorker ( sourceFile , errors , /*returnValue*/ true , getTsconfigRootOptionsMap ( ) , optionsIterator ) ;
1715
1734
if ( ! typeAcquisition ) {
1716
1735
if ( typingOptionstypeAcquisition ) {
1717
1736
typeAcquisition = ( typingOptionstypeAcquisition . enableAutoDiscovery !== undefined ) ?
@@ -1754,7 +1773,7 @@ namespace ts {
1754
1773
}
1755
1774
1756
1775
function getExtendedConfig (
1757
- sourceFile : JsonSourceFile ,
1776
+ sourceFile : TsConfigSourceFile ,
1758
1777
extendedConfigPath : string ,
1759
1778
host : ParseConfigHost ,
1760
1779
basePath : string ,
@@ -2006,7 +2025,7 @@ namespace ts {
2006
2025
host : ParseConfigHost ,
2007
2026
errors : Push < Diagnostic > ,
2008
2027
extraFileExtensions : ReadonlyArray < FileExtensionInfo > ,
2009
- jsonSourceFile : JsonSourceFile
2028
+ jsonSourceFile : TsConfigSourceFile
2010
2029
) : ExpandResult {
2011
2030
basePath = normalizePath ( basePath ) ;
2012
2031
let validatedIncludeSpecs : ReadonlyArray < string > , validatedExcludeSpecs : ReadonlyArray < string > ;
@@ -2107,7 +2126,7 @@ namespace ts {
2107
2126
} ;
2108
2127
}
2109
2128
2110
- function validateSpecs ( specs : ReadonlyArray < string > , errors : Push < Diagnostic > , allowTrailingRecursion : boolean , jsonSourceFile : JsonSourceFile , specKey : string ) : ReadonlyArray < string > {
2129
+ function validateSpecs ( specs : ReadonlyArray < string > , errors : Push < Diagnostic > , allowTrailingRecursion : boolean , jsonSourceFile : TsConfigSourceFile , specKey : string ) : ReadonlyArray < string > {
2111
2130
return specs . filter ( spec => {
2112
2131
const diag = specToDiagnostic ( spec , allowTrailingRecursion ) ;
2113
2132
if ( diag !== undefined ) {
@@ -2117,18 +2136,10 @@ namespace ts {
2117
2136
} ) ;
2118
2137
2119
2138
function createDiagnostic ( message : DiagnosticMessage , spec : string ) : Diagnostic {
2120
- if ( jsonSourceFile && jsonSourceFile . jsonObject ) {
2121
- for ( const property of getPropertyAssignment ( jsonSourceFile . jsonObject , specKey ) ) {
2122
- if ( isArrayLiteralExpression ( property . initializer ) ) {
2123
- for ( const element of property . initializer . elements ) {
2124
- if ( isStringLiteral ( element ) && element . text === spec ) {
2125
- return createDiagnosticForNodeInSourceFile ( jsonSourceFile , element , message , spec ) ;
2126
- }
2127
- }
2128
- }
2129
- }
2130
- }
2131
- return createCompilerDiagnostic ( message , spec ) ;
2139
+ const element = getTsConfigPropArrayElementValue ( jsonSourceFile , specKey , spec ) ;
2140
+ return element ?
2141
+ createDiagnosticForNodeInSourceFile ( jsonSourceFile , element , message , spec ) :
2142
+ createCompilerDiagnostic ( message , spec ) ;
2132
2143
}
2133
2144
}
2134
2145
0 commit comments