@@ -18,6 +18,8 @@ namespace ts {
1818 "node_modules/@types/" ,
1919 ] ;
2020
21+ const autoImportedTypePaths = [ "types" , "node_modules/@types" ] ;
22+
2123 export const version = "1.9.0" ;
2224
2325 export function findConfigFile ( searchPath : string , fileExists : ( fileName : string ) => boolean ) : string {
@@ -196,8 +198,8 @@ namespace ts {
196198 traceEnabled
197199 } ;
198200
199- // use typesRoot and fallback to directory that contains tsconfig if typesRoot is not set
200- const rootDir = options . typesRoot || ( options . configFilePath ? getDirectoryPath ( options . configFilePath ) : undefined ) ;
201+ // use typesRoot and fallback to directory that contains tsconfig or current directory if typesRoot is not set
202+ const rootDir = options . typesRoot || ( options . configFilePath ? getDirectoryPath ( options . configFilePath ) : ( host . getCurrentDirectory && host . getCurrentDirectory ( ) ) ) ;
201203
202204 if ( traceEnabled ) {
203205 if ( containingFile === undefined ) {
@@ -875,6 +877,19 @@ namespace ts {
875877 }
876878 }
877879
880+ function getDefaultTypeDirectiveNames ( rootPath : string ) : string [ ] {
881+ const localTypes = combinePaths ( rootPath , 'types' ) ;
882+ const npmTypes = combinePaths ( rootPath , 'node_modules/@types' ) ;
883+ let result : string [ ] = [ ] ;
884+ if ( sys . directoryExists ( localTypes ) ) {
885+ result = result . concat ( sys . getDirectories ( localTypes ) ) ;
886+ }
887+ if ( sys . directoryExists ( npmTypes ) ) {
888+ result = result . concat ( sys . getDirectories ( npmTypes ) ) ;
889+ }
890+ return result ;
891+ }
892+
878893 function getDefaultLibLocation ( ) : string {
879894 return getDirectoryPath ( normalizePath ( sys . getExecutingFilePath ( ) ) ) ;
880895 }
@@ -883,6 +898,7 @@ namespace ts {
883898 const realpath = sys . realpath && ( ( path : string ) => sys . realpath ( path ) ) ;
884899
885900 return {
901+ getDefaultTypeDirectiveNames,
886902 getSourceFile,
887903 getDefaultLibLocation,
888904 getDefaultLibFileName : options => combinePaths ( getDefaultLibLocation ( ) , getDefaultLibFileName ( options ) ) ,
@@ -1006,10 +1022,21 @@ namespace ts {
10061022
10071023 if ( ! tryReuseStructureFromOldProgram ( ) ) {
10081024 // load type declarations specified via 'types' argument
1009- if ( options . types && options . types . length ) {
1010- const resolutions = resolveTypeReferenceDirectiveNamesWorker ( options . types , /*containingFile*/ undefined ) ;
1011- for ( let i = 0 ; i < options . types . length ; i ++ ) {
1012- processTypeReferenceDirective ( options . types [ i ] , resolutions [ i ] ) ;
1025+ let typeReferences : string [ ] ;
1026+ if ( options . types ) {
1027+ typeReferences = options . types ;
1028+ }
1029+ else {
1030+ // or load all types from the automatic type import fields
1031+ if ( host . getDefaultTypeDirectiveNames ) {
1032+ typeReferences = host . getDefaultTypeDirectiveNames ( getCommonSourceDirectory ( ) ) ; ;
1033+ }
1034+ }
1035+
1036+ if ( typeReferences ) {
1037+ const resolutions = resolveTypeReferenceDirectiveNamesWorker ( typeReferences , /*containingFile*/ undefined ) ;
1038+ for ( let i = 0 ; i < typeReferences . length ; i ++ ) {
1039+ processTypeReferenceDirective ( typeReferences [ i ] , resolutions [ i ] ) ;
10131040 }
10141041 }
10151042
@@ -1914,20 +1941,7 @@ namespace ts {
19141941 i < file . imports . length ;
19151942
19161943 if ( shouldAddFile ) {
1917- const importedFile = findSourceFile ( resolution . resolvedFileName , toPath ( resolution . resolvedFileName , currentDirectory , getCanonicalFileName ) , /*isDefaultLib*/ false , /*isReference*/ false , file , skipTrivia ( file . text , file . imports [ i ] . pos ) , file . imports [ i ] . end ) ;
1918-
1919- if ( importedFile && resolution . isExternalLibraryImport ) {
1920- // Since currently irrespective of allowJs, we only look for supportedTypeScript extension external module files,
1921- // this check is ok. Otherwise this would be never true for javascript file
1922- if ( ! isExternalModule ( importedFile ) && importedFile . statements . length ) {
1923- const start = getTokenPosOfNode ( file . imports [ i ] , file ) ;
1924- fileProcessingDiagnostics . add ( createFileDiagnostic ( file , start , file . imports [ i ] . end - start , Diagnostics . Exported_external_package_typings_file_0_is_not_a_module_Please_contact_the_package_author_to_update_the_package_definition , importedFile . fileName ) ) ;
1925- }
1926- else if ( importedFile . referencedFiles . length ) {
1927- const firstRef = importedFile . referencedFiles [ 0 ] ;
1928- fileProcessingDiagnostics . add ( createFileDiagnostic ( importedFile , firstRef . pos , firstRef . end - firstRef . pos , Diagnostics . Exported_external_package_typings_file_cannot_contain_tripleslash_references_Please_contact_the_package_author_to_update_the_package_definition ) ) ;
1929- }
1930- }
1944+ findSourceFile ( resolution . resolvedFileName , toPath ( resolution . resolvedFileName , currentDirectory , getCanonicalFileName ) , /*isDefaultLib*/ false , /*isReference*/ false , file , skipTrivia ( file . text , file . imports [ i ] . pos ) , file . imports [ i ] . end ) ;
19311945 }
19321946 }
19331947 }
0 commit comments