@@ -9,16 +9,11 @@ namespace ts {
9
9
/* @internal */ export let ioWriteTime = 0 ;
10
10
11
11
/** The version of the TypeScript compiler release */
12
+ export const version = "1.9.0" ;
12
13
13
14
const emptyArray : any [ ] = [ ] ;
14
15
15
- const defaultLibrarySearchPaths = [
16
- "types/" ,
17
- "node_modules/" ,
18
- "node_modules/@types/" ,
19
- ] ;
20
-
21
- export const version = "1.9.0" ;
16
+ const defaultTypeRoots = [ "node_modules/@types" ] ;
22
17
23
18
export function findConfigFile ( searchPath : string , fileExists : ( fileName : string ) => boolean ) : string {
24
19
while ( true ) {
@@ -183,6 +178,11 @@ namespace ts {
183
178
184
179
const typeReferenceExtensions = [ ".d.ts" ] ;
185
180
181
+ function getEffectiveTypeRoots ( options : CompilerOptions , host : ModuleResolutionHost ) {
182
+ return options . typeRoots ||
183
+ defaultTypeRoots . map ( d => combinePaths ( options . configFilePath ? getDirectoryPath ( options . configFilePath ) : host . getCurrentDirectory ( ) , d ) ) ;
184
+ }
185
+
186
186
/**
187
187
* @param {string | undefined } containingFile - file that contains type reference directive, can be undefined if containing file is unknown.
188
188
* This is possible in case if resolution is performed for directives specified via 'types' parameter. In this case initial path for secondary lookups
@@ -197,39 +197,36 @@ namespace ts {
197
197
traceEnabled
198
198
} ;
199
199
200
- // use typesRoot and fallback to directory that contains tsconfig or current directory if typesRoot is not set
201
- const rootDir = options . typesRoot || ( options . configFilePath ? getDirectoryPath ( options . configFilePath ) : ( host . getCurrentDirectory && host . getCurrentDirectory ( ) ) ) ;
202
-
200
+ const typeRoots = getEffectiveTypeRoots ( options , host ) ;
203
201
if ( traceEnabled ) {
204
202
if ( containingFile === undefined ) {
205
- if ( rootDir === undefined ) {
203
+ if ( typeRoots === undefined ) {
206
204
trace ( host , Diagnostics . Resolving_type_reference_directive_0_containing_file_not_set_root_directory_not_set , typeReferenceDirectiveName ) ;
207
205
}
208
206
else {
209
- trace ( host , Diagnostics . Resolving_type_reference_directive_0_containing_file_not_set_root_directory_1 , typeReferenceDirectiveName , rootDir ) ;
207
+ trace ( host , Diagnostics . Resolving_type_reference_directive_0_containing_file_not_set_root_directory_1 , typeReferenceDirectiveName , typeRoots ) ;
210
208
}
211
209
}
212
210
else {
213
- if ( rootDir === undefined ) {
211
+ if ( typeRoots === undefined ) {
214
212
trace ( host , Diagnostics . Resolving_type_reference_directive_0_containing_file_1_root_directory_not_set , typeReferenceDirectiveName , containingFile ) ;
215
213
}
216
214
else {
217
- trace ( host , Diagnostics . Resolving_type_reference_directive_0_containing_file_1_root_directory_2 , typeReferenceDirectiveName , containingFile , rootDir ) ;
215
+ trace ( host , Diagnostics . Resolving_type_reference_directive_0_containing_file_1_root_directory_2 , typeReferenceDirectiveName , containingFile , typeRoots ) ;
218
216
}
219
217
}
220
218
}
221
219
222
220
const failedLookupLocations : string [ ] = [ ] ;
223
221
224
222
// Check primary library paths
225
- if ( rootDir !== undefined ) {
226
- const effectivePrimarySearchPaths = options . typesSearchPaths || defaultLibrarySearchPaths ;
227
- for ( const searchPath of effectivePrimarySearchPaths ) {
228
- const primaryPath = combinePaths ( rootDir , searchPath ) ;
229
- if ( traceEnabled ) {
230
- trace ( host , Diagnostics . Resolving_with_primary_search_path_0 , primaryPath ) ;
231
- }
232
- const candidate = combinePaths ( primaryPath , typeReferenceDirectiveName ) ;
223
+ if ( typeRoots . length ) {
224
+ if ( traceEnabled ) {
225
+ trace ( host , Diagnostics . Resolving_with_primary_search_path_0 , typeRoots . join ( ", " ) ) ;
226
+ }
227
+ const primarySearchPaths = typeRoots ;
228
+ for ( const typeRoot of primarySearchPaths ) {
229
+ const candidate = combinePaths ( typeRoot , typeReferenceDirectiveName ) ;
233
230
const candidateDirectory = getDirectoryPath ( candidate ) ;
234
231
const resolvedFile = loadNodeModuleFromDirectory ( typeReferenceExtensions , candidate , failedLookupLocations ,
235
232
! directoryProbablyExists ( candidateDirectory , host ) , moduleResolutionState ) ;
@@ -256,9 +253,6 @@ namespace ts {
256
253
if ( containingFile ) {
257
254
initialLocationForSecondaryLookup = getDirectoryPath ( containingFile ) ;
258
255
}
259
- else {
260
- initialLocationForSecondaryLookup = rootDir ;
261
- }
262
256
263
257
if ( initialLocationForSecondaryLookup !== undefined ) {
264
258
// check secondary locations
@@ -937,19 +931,6 @@ namespace ts {
937
931
}
938
932
}
939
933
940
- function getDefaultTypeDirectiveNames ( rootPath : string ) : string [ ] {
941
- const localTypes = combinePaths ( rootPath , "types" ) ;
942
- const npmTypes = combinePaths ( rootPath , "node_modules/@types" ) ;
943
- let result : string [ ] = [ ] ;
944
- if ( sys . directoryExists ( localTypes ) ) {
945
- result = result . concat ( sys . getDirectories ( localTypes ) ) ;
946
- }
947
- if ( sys . directoryExists ( npmTypes ) ) {
948
- result = result . concat ( sys . getDirectories ( npmTypes ) ) ;
949
- }
950
- return result ;
951
- }
952
-
953
934
function getDefaultLibLocation ( ) : string {
954
935
return getDirectoryPath ( normalizePath ( sys . getExecutingFilePath ( ) ) ) ;
955
936
}
@@ -958,7 +939,6 @@ namespace ts {
958
939
const realpath = sys . realpath && ( ( path : string ) => sys . realpath ( path ) ) ;
959
940
960
941
return {
961
- getDefaultTypeDirectiveNames,
962
942
getSourceFile,
963
943
getDefaultLibLocation,
964
944
getDefaultLibFileName : options => combinePaths ( getDefaultLibLocation ( ) , getDefaultLibFileName ( options ) ) ,
@@ -971,6 +951,7 @@ namespace ts {
971
951
readFile : fileName => sys . readFile ( fileName ) ,
972
952
trace : ( s : string ) => sys . write ( s + newLine ) ,
973
953
directoryExists : directoryName => sys . directoryExists ( directoryName ) ,
954
+ getDirectories : ( path : string ) => sys . getDirectories ( path ) ,
974
955
realpath
975
956
} ;
976
957
}
@@ -1034,21 +1015,35 @@ namespace ts {
1034
1015
return resolutions ;
1035
1016
}
1036
1017
1037
- export function getDefaultTypeDirectiveNames ( options : CompilerOptions , rootFiles : string [ ] , host : CompilerHost ) : string [ ] {
1018
+ function getInferredTypesRoot ( options : CompilerOptions , rootFiles : string [ ] , host : CompilerHost ) {
1019
+ return computeCommonSourceDirectoryOfFilenames ( rootFiles , host . getCurrentDirectory ( ) , f => host . getCanonicalFileName ( f ) ) ;
1020
+ }
1021
+
1022
+ /**
1023
+ * Given a set of options and a set of root files, returns the set of type directive names
1024
+ * that should be included for this program automatically.
1025
+ * This list could either come from the config file,
1026
+ * or from enumerating the types root + initial secondary types lookup location.
1027
+ * More type directives might appear in the program later as a result of loading actual source files;
1028
+ * this list is only the set of defaults that are implicitly included.
1029
+ */
1030
+ export function getAutomaticTypeDirectiveNames ( options : CompilerOptions , rootFiles : string [ ] , host : CompilerHost ) : string [ ] {
1038
1031
// Use explicit type list from tsconfig.json
1039
1032
if ( options . types ) {
1040
1033
return options . types ;
1041
1034
}
1042
1035
1043
- // or load all types from the automatic type import fields
1044
- if ( host && host . getDefaultTypeDirectiveNames ) {
1045
- const commonRoot = computeCommonSourceDirectoryOfFilenames ( rootFiles , host . getCurrentDirectory ( ) , f => host . getCanonicalFileName ( f ) ) ;
1046
- if ( commonRoot ) {
1047
- return host . getDefaultTypeDirectiveNames ( commonRoot ) ;
1036
+ // Walk the primary type lookup locations
1037
+ let result : string [ ] = [ ] ;
1038
+ if ( host . directoryExists && host . getDirectories ) {
1039
+ const typeRoots = getEffectiveTypeRoots ( options , host ) ;
1040
+ for ( const root of typeRoots ) {
1041
+ if ( host . directoryExists ( root ) ) {
1042
+ result = result . concat ( host . getDirectories ( root ) ) ;
1043
+ }
1048
1044
}
1049
1045
}
1050
-
1051
- return undefined ;
1046
+ return result ;
1052
1047
}
1053
1048
1054
1049
export function createProgram ( rootNames : string [ ] , options : CompilerOptions , host ?: CompilerHost , oldProgram ?: Program ) : Program {
@@ -1100,11 +1095,13 @@ namespace ts {
1100
1095
if ( ! tryReuseStructureFromOldProgram ( ) ) {
1101
1096
forEach ( rootNames , name => processRootFile ( name , /*isDefaultLib*/ false ) ) ;
1102
1097
1103
- // load type declarations specified via 'types' argument
1104
- const typeReferences : string [ ] = getDefaultTypeDirectiveNames ( options , rootNames , host ) ;
1098
+ // load type declarations specified via 'types' argument or implicitly from types/ and node_modules/@types folders
1099
+ const typeReferences : string [ ] = getAutomaticTypeDirectiveNames ( options , rootNames , host ) ;
1105
1100
1106
1101
if ( typeReferences ) {
1107
- const resolutions = resolveTypeReferenceDirectiveNamesWorker ( typeReferences , /*containingFile*/ undefined ) ;
1102
+ const inferredRoot = getInferredTypesRoot ( options , rootNames , host ) ;
1103
+ const containingFilename = combinePaths ( inferredRoot , "__inferred type names__.ts" ) ;
1104
+ const resolutions = resolveTypeReferenceDirectiveNamesWorker ( typeReferences , containingFilename ) ;
1108
1105
for ( let i = 0 ; i < typeReferences . length ; i ++ ) {
1109
1106
processTypeReferenceDirective ( typeReferences [ i ] , resolutions [ i ] ) ;
1110
1107
}
@@ -1212,10 +1209,9 @@ namespace ts {
1212
1209
( oldOptions . jsx !== options . jsx ) ||
1213
1210
( oldOptions . allowJs !== options . allowJs ) ||
1214
1211
( oldOptions . rootDir !== options . rootDir ) ||
1215
- ( oldOptions . typesSearchPaths !== options . typesSearchPaths ) ||
1216
1212
( oldOptions . configFilePath !== options . configFilePath ) ||
1217
1213
( oldOptions . baseUrl !== options . baseUrl ) ||
1218
- ( oldOptions . typesRoot !== options . typesRoot ) ||
1214
+ ! arrayIsEqualTo ( oldOptions . typeRoots , oldOptions . typeRoots ) ||
1219
1215
! arrayIsEqualTo ( oldOptions . rootDirs , options . rootDirs ) ||
1220
1216
! mapIsEqualTo ( oldOptions . paths , options . paths ) ) {
1221
1217
return false ;
@@ -1970,7 +1966,7 @@ namespace ts {
1970
1966
}
1971
1967
}
1972
1968
else {
1973
- fileProcessingDiagnostics . add ( createDiagnostic ( refFile , refPos , refEnd , Diagnostics . Cannot_find_name_0 , typeReferenceDirective ) ) ;
1969
+ fileProcessingDiagnostics . add ( createDiagnostic ( refFile , refPos , refEnd , Diagnostics . Cannot_find_type_definition_file_for_0 , typeReferenceDirective ) ) ;
1974
1970
}
1975
1971
1976
1972
if ( saveResolution ) {
0 commit comments