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