@@ -22,6 +22,7 @@ namespace ts.JsTyping {
2222 name ?: string ;
2323 optionalDependencies ?: MapLike < string > ;
2424 peerDependencies ?: MapLike < string > ;
25+ types ?: string ;
2526 typings ?: string ;
2627 }
2728
@@ -83,14 +84,11 @@ namespace ts.JsTyping {
8384
8485 const filesToWatch : string [ ] = [ ] ;
8586
86- forEach ( typeAcquisition . include , addInferredTyping ) ;
87+ if ( typeAcquisition . include ) addInferredTypings ( typeAcquisition . include , "Explicitly included types" ) ;
8788 const exclude = typeAcquisition . exclude || [ ] ;
8889
8990 // Directories to search for package.json, bower.json and other typing information
90- const possibleSearchDirs = createMap < true > ( ) ;
91- for ( const f of fileNames ) {
92- possibleSearchDirs . set ( getDirectoryPath ( f ) , true ) ;
93- }
91+ const possibleSearchDirs = arrayToSet ( fileNames , getDirectoryPath ) ;
9492 possibleSearchDirs . set ( projectRootPath , true ) ;
9593 possibleSearchDirs . forEach ( ( _true , searchDir ) => {
9694 const packageJsonPath = combinePaths ( searchDir , "package.json" ) ;
@@ -109,13 +107,8 @@ namespace ts.JsTyping {
109107
110108 // add typings for unresolved imports
111109 if ( unresolvedImports ) {
112- const x = unresolvedImports . map ( moduleId => nodeCoreModules . has ( moduleId ) ? "node" : moduleId ) ;
113- if ( x . length && log ) log ( `Inferred typings from unresolved imports: ${ JSON . stringify ( x ) } ` ) ;
114- for ( const typingName of x ) {
115- if ( ! inferredTypings . has ( typingName ) ) {
116- inferredTypings . set ( typingName , undefined ) ;
117- }
118- }
110+ const module = deduplicate ( unresolvedImports . map ( moduleId => nodeCoreModules . has ( moduleId ) ? "node" : moduleId ) ) ;
111+ addInferredTypings ( module , "Inferred typings from unresolved imports" ) ;
119112 }
120113 // Add the cached typing locations for inferred typings that are already installed
121114 packageNameToTypingLocation . forEach ( ( typingLocation , name ) => {
@@ -126,7 +119,8 @@ namespace ts.JsTyping {
126119
127120 // Remove typings that the user has added to the exclude list
128121 for ( const excludeTypingName of exclude ) {
129- inferredTypings . delete ( excludeTypingName ) ;
122+ const didDelete = inferredTypings . delete ( excludeTypingName ) ;
123+ if ( didDelete && log ) log ( `Typing for ${ excludeTypingName } is in exclude list, will be ignored.` ) ;
130124 }
131125
132126 const newTypingNames : string [ ] = [ ] ;
@@ -148,6 +142,10 @@ namespace ts.JsTyping {
148142 inferredTypings . set ( typingName , undefined ) ;
149143 }
150144 }
145+ function addInferredTypings ( typingNames : ReadonlyArray < string > , message : string ) {
146+ if ( log ) log ( `${ message } : ${ JSON . stringify ( typingNames ) } ` ) ;
147+ forEach ( typingNames , addInferredTyping ) ;
148+ }
151149
152150 /**
153151 * Get the typing info from common package manager json files like package.json or bower.json
@@ -158,20 +156,9 @@ namespace ts.JsTyping {
158156 }
159157
160158 filesToWatch . push ( jsonPath ) ;
161- if ( log ) log ( `Searching for typing names in '${ jsonPath } ' dependencies` ) ;
162- const jsonConfig : PackageJson = readConfigFile ( jsonPath , ( path : string ) => host . readFile ( path ) ) . config ;
163- addInferredTypingsFromKeys ( jsonConfig . dependencies ) ;
164- addInferredTypingsFromKeys ( jsonConfig . devDependencies ) ;
165- addInferredTypingsFromKeys ( jsonConfig . optionalDependencies ) ;
166- addInferredTypingsFromKeys ( jsonConfig . peerDependencies ) ;
167-
168- function addInferredTypingsFromKeys ( map : MapLike < string > | undefined ) : void {
169- for ( const key in map ) {
170- if ( ts . hasProperty ( map , key ) ) {
171- addInferredTyping ( key ) ;
172- }
173- }
174- }
159+ const jsonConfig : PackageJson = readConfigFile ( jsonPath , path => host . readFile ( path ) ) . config ;
160+ const jsonTypingNames = flatMap ( [ jsonConfig . dependencies , jsonConfig . devDependencies , jsonConfig . optionalDependencies , jsonConfig . peerDependencies ] , getOwnKeys ) ;
161+ addInferredTypings ( jsonTypingNames , `Typing names in '${ jsonPath } ' dependencies` ) ;
175162 }
176163
177164 /**
@@ -189,10 +176,7 @@ namespace ts.JsTyping {
189176 return safeList . get ( cleanedTypingName ) ;
190177 } ) ;
191178 if ( fromFileNames . length ) {
192- if ( log ) log ( `Inferred typings from file names: ${ JSON . stringify ( fromFileNames ) } ` ) ;
193- for ( const safe of fromFileNames ) {
194- addInferredTyping ( safe ) ;
195- }
179+ addInferredTypings ( fromFileNames , "Inferred typings from file names" ) ;
196180 }
197181
198182 const hasJsxFile = some ( fileNames , f => fileExtensionIs ( f , Extension . Jsx ) ) ;
@@ -217,6 +201,7 @@ namespace ts.JsTyping {
217201 // depth of 2, so we access `node_modules/foo` but not `node_modules/foo/bar`
218202 const fileNames = host . readDirectory ( packagesFolderPath , [ ".json" ] , /*excludes*/ undefined , /*includes*/ undefined , /*depth*/ 2 ) ;
219203 if ( log ) log ( `Searching for typing names in ${ packagesFolderPath } ; all files: ${ JSON . stringify ( fileNames ) } ` ) ;
204+ const packageNames : string [ ] = [ ] ;
220205 for ( const fileName of fileNames ) {
221206 const normalizedFileName = normalizePath ( fileName ) ;
222207 const baseFileName = getBaseFileName ( normalizedFileName ) ;
@@ -239,14 +224,17 @@ namespace ts.JsTyping {
239224 if ( ! packageJson . name ) {
240225 continue ;
241226 }
242- if ( packageJson . typings ) {
243- const absolutePath = getNormalizedAbsolutePath ( packageJson . typings , getDirectoryPath ( normalizedFileName ) ) ;
227+ const ownTypes = packageJson . types || packageJson . typings ;
228+ if ( ownTypes ) {
229+ const absolutePath = getNormalizedAbsolutePath ( ownTypes , getDirectoryPath ( normalizedFileName ) ) ;
230+ if ( log ) log ( ` Package '${ packageJson . name } ' provides its own types.` ) ;
244231 inferredTypings . set ( packageJson . name , absolutePath ) ;
245232 }
246233 else {
247- addInferredTyping ( packageJson . name ) ;
234+ packageNames . push ( packageJson . name ) ;
248235 }
249236 }
237+ addInferredTypings ( packageNames , " Found package names" ) ;
250238 }
251239
252240 }
0 commit comments