@@ -61,17 +61,20 @@ namespace ts.JsTyping {
61
61
unresolvedImports : ReadonlyArray < string > ) :
62
62
{ cachedTypingPaths : string [ ] , newTypingNames : string [ ] , filesToWatch : string [ ] } {
63
63
64
- // A typing name to typing file path mapping
65
- const inferredTypings = createMap < string > ( ) ;
66
-
67
64
if ( ! typeAcquisition || ! typeAcquisition . enable ) {
68
65
return { cachedTypingPaths : [ ] , newTypingNames : [ ] , filesToWatch : [ ] } ;
69
66
}
70
67
68
+ // A typing name to typing file path mapping
69
+ const inferredTypings = createMap < string > ( ) ;
70
+
71
71
// Only infer typings for .js and .jsx files
72
- fileNames = filter ( map ( fileNames , normalizePath ) , f => {
73
- const kind = ensureScriptKind ( f , getScriptKindFromFileName ( f ) ) ;
74
- return kind === ScriptKind . JS || kind === ScriptKind . JSX ;
72
+ fileNames = mapDefined ( fileNames , fileName => {
73
+ const path = normalizePath ( fileName ) ;
74
+ const kind = ensureScriptKind ( path , getScriptKindFromFileName ( path ) ) ;
75
+ if ( kind === ScriptKind . JS || kind === ScriptKind . JSX ) {
76
+ return path ;
77
+ }
75
78
} ) ;
76
79
77
80
if ( ! safeList ) {
@@ -80,31 +83,29 @@ namespace ts.JsTyping {
80
83
}
81
84
82
85
const filesToWatch : string [ ] = [ ] ;
83
- // Directories to search for package.json, bower.json and other typing information
84
- let searchDirs : string [ ] = [ ] ;
85
- let exclude : string [ ] = [ ] ;
86
86
87
- mergeTypings ( typeAcquisition . include ) ;
88
- exclude = typeAcquisition . exclude || [ ] ;
87
+ forEach ( typeAcquisition . include , addInferredTyping ) ;
88
+ const exclude = typeAcquisition . exclude || [ ] ;
89
89
90
- const possibleSearchDirs = map ( fileNames , getDirectoryPath ) ;
91
- if ( projectRootPath ) {
92
- possibleSearchDirs . push ( projectRootPath ) ;
90
+ // Directories to search for package.json, bower.json and other typing information
91
+ const possibleSearchDirs = createMap < true > ( ) ;
92
+ for ( const f of fileNames ) {
93
+ possibleSearchDirs . set ( getDirectoryPath ( f ) , true ) ;
93
94
}
94
- searchDirs = deduplicate ( possibleSearchDirs ) ;
95
- for ( const searchDir of searchDirs ) {
95
+ possibleSearchDirs . set ( projectRootPath , true ) ;
96
+ possibleSearchDirs . forEach ( ( _true , searchDir ) => {
96
97
const packageJsonPath = combinePaths ( searchDir , "package.json" ) ;
97
98
getTypingNamesFromJson ( packageJsonPath , filesToWatch ) ;
98
99
99
100
const bowerJsonPath = combinePaths ( searchDir , "bower.json" ) ;
100
101
getTypingNamesFromJson ( bowerJsonPath , filesToWatch ) ;
101
102
102
103
const bowerComponentsPath = combinePaths ( searchDir , "bower_components" ) ;
103
- getTypingNamesFromPackagesFolder ( bowerComponentsPath ) ;
104
+ getTypingNamesFromPackagesFolder ( bowerComponentsPath , filesToWatch ) ;
104
105
105
106
const nodeModulesPath = combinePaths ( searchDir , "node_modules" ) ;
106
- getTypingNamesFromPackagesFolder ( nodeModulesPath ) ;
107
- }
107
+ getTypingNamesFromPackagesFolder ( nodeModulesPath , filesToWatch ) ;
108
+ } ) ;
108
109
getTypingNamesFromSourceFileNames ( fileNames ) ;
109
110
110
111
// add typings for unresolved imports
@@ -140,41 +141,33 @@ namespace ts.JsTyping {
140
141
} ) ;
141
142
return { cachedTypingPaths, newTypingNames, filesToWatch } ;
142
143
143
- /**
144
- * Merge a given list of typingNames to the inferredTypings map
145
- */
146
- function mergeTypings ( typingNames : ReadonlyArray < string > ) {
147
- if ( ! typingNames ) {
148
- return ;
149
- }
150
-
151
- for ( const typing of typingNames ) {
152
- if ( ! inferredTypings . has ( typing ) ) {
153
- inferredTypings . set ( typing , undefined ) ;
154
- }
144
+ function addInferredTyping ( typingName : string ) {
145
+ if ( ! inferredTypings . has ( typingName ) ) {
146
+ inferredTypings . set ( typingName , undefined ) ;
155
147
}
156
148
}
157
149
158
150
/**
159
151
* Get the typing info from common package manager json files like package.json or bower.json
160
152
*/
161
- function getTypingNamesFromJson ( jsonPath : string , filesToWatch : string [ ] ) {
162
- if ( host . fileExists ( jsonPath ) ) {
163
- filesToWatch . push ( jsonPath ) ;
164
- }
165
- const result = readConfigFile ( jsonPath , ( path : string ) => host . readFile ( path ) ) ;
166
- const jsonConfig : PackageJson = result . config ;
167
- if ( jsonConfig . dependencies ) {
168
- mergeTypings ( getOwnKeys ( jsonConfig . dependencies ) ) ;
169
- }
170
- if ( jsonConfig . devDependencies ) {
171
- mergeTypings ( getOwnKeys ( jsonConfig . devDependencies ) ) ;
172
- }
173
- if ( jsonConfig . optionalDependencies ) {
174
- mergeTypings ( getOwnKeys ( jsonConfig . optionalDependencies ) ) ;
153
+ function getTypingNamesFromJson ( jsonPath : string , filesToWatch : Push < string > ) {
154
+ if ( ! host . fileExists ( jsonPath ) ) {
155
+ return ;
175
156
}
176
- if ( jsonConfig . peerDependencies ) {
177
- mergeTypings ( getOwnKeys ( jsonConfig . peerDependencies ) ) ;
157
+
158
+ filesToWatch . push ( jsonPath ) ;
159
+ const jsonConfig : PackageJson = readConfigFile ( jsonPath , ( path : string ) => host . readFile ( path ) ) . config ;
160
+ addInferredTypingsFromKeys ( jsonConfig . dependencies ) ;
161
+ addInferredTypingsFromKeys ( jsonConfig . devDependencies ) ;
162
+ addInferredTypingsFromKeys ( jsonConfig . optionalDependencies ) ;
163
+ addInferredTypingsFromKeys ( jsonConfig . peerDependencies ) ;
164
+
165
+ function addInferredTypingsFromKeys ( map : MapLike < string > | undefined ) : void {
166
+ for ( const key in map ) {
167
+ if ( ts . hasProperty ( map , key ) ) {
168
+ addInferredTyping ( key ) ;
169
+ }
170
+ }
178
171
}
179
172
}
180
173
@@ -185,33 +178,37 @@ namespace ts.JsTyping {
185
178
* @param fileNames are the names for source files in the project
186
179
*/
187
180
function getTypingNamesFromSourceFileNames ( fileNames : string [ ] ) {
188
- const jsFileNames = filter ( fileNames , hasJavaScriptFileExtension ) ;
189
- const inferredTypingNames = map ( jsFileNames , f => removeFileExtension ( getBaseFileName ( f . toLowerCase ( ) ) ) ) ;
190
- const cleanedTypingNames = map ( inferredTypingNames , f => f . replace ( / ( (?: \. | - ) m i n (? = \. | $ ) ) | ( (?: - | \. ) \d + ) / g, "" ) ) ;
191
-
192
181
if ( safeList !== EmptySafeList ) {
193
- mergeTypings ( ts . mapDefined ( cleanedTypingNames , f => safeList . get ( f ) ) ) ;
182
+ for ( const j of fileNames ) {
183
+ if ( ! hasJavaScriptFileExtension ( j ) ) continue ;
184
+
185
+ const inferredTypingName = removeFileExtension ( getBaseFileName ( j . toLowerCase ( ) ) ) ;
186
+ const cleanedTypingName = inferredTypingName . replace ( / ( (?: \. | - ) m i n (? = \. | $ ) ) | ( (?: - | \. ) \d + ) / g, "" ) ;
187
+ const safe = safeList . get ( cleanedTypingName ) ;
188
+ if ( safe !== undefined ) {
189
+ addInferredTyping ( safe ) ;
190
+ }
191
+ }
194
192
}
195
193
196
194
const hasJsxFile = forEach ( fileNames , f => ensureScriptKind ( f , getScriptKindFromFileName ( f ) ) === ScriptKind . JSX ) ;
197
195
if ( hasJsxFile ) {
198
- mergeTypings ( [ "react" ] ) ;
196
+ addInferredTyping ( "react" ) ;
199
197
}
200
198
}
201
199
202
200
/**
203
201
* Infer typing names from packages folder (ex: node_module, bower_components)
204
202
* @param packagesFolderPath is the path to the packages folder
205
203
*/
206
- function getTypingNamesFromPackagesFolder ( packagesFolderPath : string ) {
204
+ function getTypingNamesFromPackagesFolder ( packagesFolderPath : string , filesToWatch : Push < string > ) {
207
205
filesToWatch . push ( packagesFolderPath ) ;
208
206
209
207
// Todo: add support for ModuleResolutionHost too
210
208
if ( ! host . directoryExists ( packagesFolderPath ) ) {
211
209
return ;
212
210
}
213
211
214
- const typingNames : string [ ] = [ ] ;
215
212
const fileNames = host . readDirectory ( packagesFolderPath , [ ".json" ] , /*excludes*/ undefined , /*includes*/ undefined , /*depth*/ 2 ) ;
216
213
for ( const fileName of fileNames ) {
217
214
const normalizedFileName = normalizePath ( fileName ) ;
@@ -240,10 +237,9 @@ namespace ts.JsTyping {
240
237
inferredTypings . set ( packageJson . name , absolutePath ) ;
241
238
}
242
239
else {
243
- typingNames . push ( packageJson . name ) ;
240
+ addInferredTyping ( packageJson . name ) ;
244
241
}
245
242
}
246
- mergeTypings ( typingNames ) ;
247
243
}
248
244
249
245
}
0 commit comments