@@ -61,17 +61,20 @@ namespace ts.JsTyping {
6161 unresolvedImports : ReadonlyArray < string > ) :
6262 { cachedTypingPaths : string [ ] , newTypingNames : string [ ] , filesToWatch : string [ ] } {
6363
64- // A typing name to typing file path mapping
65- const inferredTypings = createMap < string > ( ) ;
66-
6764 if ( ! typeAcquisition || ! typeAcquisition . enable ) {
6865 return { cachedTypingPaths : [ ] , newTypingNames : [ ] , filesToWatch : [ ] } ;
6966 }
7067
68+ // A typing name to typing file path mapping
69+ const inferredTypings = createMap < string > ( ) ;
70+
7171 // 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+ }
7578 } ) ;
7679
7780 if ( ! safeList ) {
@@ -80,31 +83,29 @@ namespace ts.JsTyping {
8083 }
8184
8285 const filesToWatch : string [ ] = [ ] ;
83- // Directories to search for package.json, bower.json and other typing information
84- let searchDirs : string [ ] = [ ] ;
85- let exclude : string [ ] = [ ] ;
8686
87- mergeTypings ( typeAcquisition . include ) ;
88- exclude = typeAcquisition . exclude || [ ] ;
87+ forEach ( typeAcquisition . include , addInferredTyping ) ;
88+ const exclude = typeAcquisition . exclude || [ ] ;
8989
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 ) ;
9394 }
94- searchDirs = deduplicate ( possibleSearchDirs ) ;
95- for ( const searchDir of searchDirs ) {
95+ possibleSearchDirs . set ( projectRootPath , true ) ;
96+ possibleSearchDirs . forEach ( ( _true , searchDir ) => {
9697 const packageJsonPath = combinePaths ( searchDir , "package.json" ) ;
9798 getTypingNamesFromJson ( packageJsonPath , filesToWatch ) ;
9899
99100 const bowerJsonPath = combinePaths ( searchDir , "bower.json" ) ;
100101 getTypingNamesFromJson ( bowerJsonPath , filesToWatch ) ;
101102
102103 const bowerComponentsPath = combinePaths ( searchDir , "bower_components" ) ;
103- getTypingNamesFromPackagesFolder ( bowerComponentsPath ) ;
104+ getTypingNamesFromPackagesFolder ( bowerComponentsPath , filesToWatch ) ;
104105
105106 const nodeModulesPath = combinePaths ( searchDir , "node_modules" ) ;
106- getTypingNamesFromPackagesFolder ( nodeModulesPath ) ;
107- }
107+ getTypingNamesFromPackagesFolder ( nodeModulesPath , filesToWatch ) ;
108+ } ) ;
108109 getTypingNamesFromSourceFileNames ( fileNames ) ;
109110
110111 // add typings for unresolved imports
@@ -140,41 +141,33 @@ namespace ts.JsTyping {
140141 } ) ;
141142 return { cachedTypingPaths, newTypingNames, filesToWatch } ;
142143
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 ) ;
155147 }
156148 }
157149
158150 /**
159151 * Get the typing info from common package manager json files like package.json or bower.json
160152 */
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 ;
175156 }
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+ }
178171 }
179172 }
180173
@@ -185,33 +178,37 @@ namespace ts.JsTyping {
185178 * @param fileNames are the names for source files in the project
186179 */
187180 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-
192181 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+ }
194192 }
195193
196194 const hasJsxFile = forEach ( fileNames , f => ensureScriptKind ( f , getScriptKindFromFileName ( f ) ) === ScriptKind . JSX ) ;
197195 if ( hasJsxFile ) {
198- mergeTypings ( [ "react" ] ) ;
196+ addInferredTyping ( "react" ) ;
199197 }
200198 }
201199
202200 /**
203201 * Infer typing names from packages folder (ex: node_module, bower_components)
204202 * @param packagesFolderPath is the path to the packages folder
205203 */
206- function getTypingNamesFromPackagesFolder ( packagesFolderPath : string ) {
204+ function getTypingNamesFromPackagesFolder ( packagesFolderPath : string , filesToWatch : Push < string > ) {
207205 filesToWatch . push ( packagesFolderPath ) ;
208206
209207 // Todo: add support for ModuleResolutionHost too
210208 if ( ! host . directoryExists ( packagesFolderPath ) ) {
211209 return ;
212210 }
213211
214- const typingNames : string [ ] = [ ] ;
215212 const fileNames = host . readDirectory ( packagesFolderPath , [ ".json" ] , /*excludes*/ undefined , /*includes*/ undefined , /*depth*/ 2 ) ;
216213 for ( const fileName of fileNames ) {
217214 const normalizedFileName = normalizePath ( fileName ) ;
@@ -240,10 +237,9 @@ namespace ts.JsTyping {
240237 inferredTypings . set ( packageJson . name , absolutePath ) ;
241238 }
242239 else {
243- typingNames . push ( packageJson . name ) ;
240+ addInferredTyping ( packageJson . name ) ;
244241 }
245242 }
246- mergeTypings ( typingNames ) ;
247243 }
248244
249245 }
0 commit comments