@@ -22,6 +22,7 @@ namespace ts.JsTyping {
22
22
name ?: string ;
23
23
optionalDependencies ?: MapLike < string > ;
24
24
peerDependencies ?: MapLike < string > ;
25
+ types ?: string ;
25
26
typings ?: string ;
26
27
}
27
28
@@ -83,14 +84,11 @@ namespace ts.JsTyping {
83
84
84
85
const filesToWatch : string [ ] = [ ] ;
85
86
86
- forEach ( typeAcquisition . include , addInferredTyping ) ;
87
+ if ( typeAcquisition . include ) addInferredTypings ( typeAcquisition . include , "Explicitly included types" ) ;
87
88
const exclude = typeAcquisition . exclude || [ ] ;
88
89
89
90
// 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 ) ;
94
92
possibleSearchDirs . set ( projectRootPath , true ) ;
95
93
possibleSearchDirs . forEach ( ( _true , searchDir ) => {
96
94
const packageJsonPath = combinePaths ( searchDir , "package.json" ) ;
@@ -109,13 +107,8 @@ namespace ts.JsTyping {
109
107
110
108
// add typings for unresolved imports
111
109
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" ) ;
119
112
}
120
113
// Add the cached typing locations for inferred typings that are already installed
121
114
packageNameToTypingLocation . forEach ( ( typingLocation , name ) => {
@@ -126,7 +119,8 @@ namespace ts.JsTyping {
126
119
127
120
// Remove typings that the user has added to the exclude list
128
121
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.` ) ;
130
124
}
131
125
132
126
const newTypingNames : string [ ] = [ ] ;
@@ -148,6 +142,10 @@ namespace ts.JsTyping {
148
142
inferredTypings . set ( typingName , undefined ) ;
149
143
}
150
144
}
145
+ function addInferredTypings ( typingNames : ReadonlyArray < string > , message : string ) {
146
+ if ( log ) log ( `${ message } : ${ JSON . stringify ( typingNames ) } ` ) ;
147
+ forEach ( typingNames , addInferredTyping ) ;
148
+ }
151
149
152
150
/**
153
151
* Get the typing info from common package manager json files like package.json or bower.json
@@ -158,20 +156,9 @@ namespace ts.JsTyping {
158
156
}
159
157
160
158
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` ) ;
175
162
}
176
163
177
164
/**
@@ -189,10 +176,7 @@ namespace ts.JsTyping {
189
176
return safeList . get ( cleanedTypingName ) ;
190
177
} ) ;
191
178
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" ) ;
196
180
}
197
181
198
182
const hasJsxFile = some ( fileNames , f => fileExtensionIs ( f , Extension . Jsx ) ) ;
@@ -217,6 +201,7 @@ namespace ts.JsTyping {
217
201
// depth of 2, so we access `node_modules/foo` but not `node_modules/foo/bar`
218
202
const fileNames = host . readDirectory ( packagesFolderPath , [ ".json" ] , /*excludes*/ undefined , /*includes*/ undefined , /*depth*/ 2 ) ;
219
203
if ( log ) log ( `Searching for typing names in ${ packagesFolderPath } ; all files: ${ JSON . stringify ( fileNames ) } ` ) ;
204
+ const packageNames : string [ ] = [ ] ;
220
205
for ( const fileName of fileNames ) {
221
206
const normalizedFileName = normalizePath ( fileName ) ;
222
207
const baseFileName = getBaseFileName ( normalizedFileName ) ;
@@ -239,14 +224,17 @@ namespace ts.JsTyping {
239
224
if ( ! packageJson . name ) {
240
225
continue ;
241
226
}
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.` ) ;
244
231
inferredTypings . set ( packageJson . name , absolutePath ) ;
245
232
}
246
233
else {
247
- addInferredTyping ( packageJson . name ) ;
234
+ packageNames . push ( packageJson . name ) ;
248
235
}
249
236
}
237
+ addInferredTypings ( packageNames , " Found package names" ) ;
250
238
}
251
239
252
240
}
0 commit comments