@@ -13,23 +13,11 @@ namespace ts.JsTyping {
1313 readDirectory : ( path : string , extension ?: string , exclude ?: string [ ] , depth ?: number ) => string [ ] ;
1414 } ;
1515
16- interface TsdJson {
17- version : string ;
18- repo : string ;
19- ref : string ;
20- path : string ;
21- installed ?: Map < TsdInstalledItem > ;
22- } ;
23-
24- interface TsdInstalledItem {
25- commit : string ;
26- } ;
27-
2816 interface PackageJson {
2917 _requiredBy ?: string [ ] ;
3018 dependencies ?: Map < string > ;
3119 devDependencies ?: Map < string > ;
32- name : string ;
20+ name ? : string ;
3321 optionalDependencies ?: Map < string > ;
3422 peerDependencies ?: Map < string > ;
3523 typings ?: string ;
@@ -41,19 +29,21 @@ namespace ts.JsTyping {
4129
4230 /**
4331 * @param host is the object providing I/O related operations.
44- * @param fileNames are the file names that belong to the same project.
32+ * @param fileNames are the file names that belong to the same project
4533 * @param cachePath is the path to the typings cache
4634 * @param projectRootPath is the path to the project root directory
4735 * @param safeListPath is the path used to retrieve the safe list
48- * @param typingOptions are used for customizing the typing inference process.
49- * @param compilerOptions are used as a source of typing inference.
36+ * @param packageNameToTypingLocation is the map of package names to their cached typing locations
37+ * @param typingOptions are used to customize the typing inference process
38+ * @param compilerOptions are used as a source for typing inference
5039 */
5140 export function discoverTypings (
5241 host : TypingResolutionHost ,
5342 fileNames : string [ ] ,
5443 cachePath : Path ,
5544 projectRootPath : Path ,
5645 safeListPath : Path ,
46+ packageNameToTypingLocation : Map < string > ,
5747 typingOptions : TypingOptions ,
5848 compilerOptions : CompilerOptions ) :
5949 { cachedTypingPaths : string [ ] , newTypingNames : string [ ] , filesToWatch : string [ ] } {
@@ -69,8 +59,9 @@ namespace ts.JsTyping {
6959 fileNames = filter ( map ( fileNames , normalizePath ) , f => scriptKindIs ( f , /*LanguageServiceHost*/ undefined , ScriptKind . JS , ScriptKind . JSX ) ) ;
7060
7161 if ( ! safeList ) {
72- const result = readConfigFile ( safeListPath , host . readFile ) ;
62+ const result = readConfigFile ( safeListPath , ( path : string ) => host . readFile ( path ) ) ;
7363 if ( result . config ) { safeList = result . config ; }
64+ else { safeList = { } ; } ;
7465 }
7566
7667 const filesToWatch : string [ ] = [ ] ;
@@ -81,44 +72,27 @@ namespace ts.JsTyping {
8172 mergeTypings ( typingOptions . include ) ;
8273 exclude = typingOptions . exclude || [ ] ;
8374
84- if ( typingOptions . enableAutoDiscovery ) {
85- const possibleSearchDirs = map ( fileNames , getDirectoryPath ) ;
86- if ( projectRootPath !== undefined ) {
87- possibleSearchDirs . push ( projectRootPath ) ;
88- }
89- searchDirs = deduplicate ( possibleSearchDirs ) ;
90- for ( const searchDir of searchDirs ) {
91- const packageJsonPath = combinePaths ( searchDir , "package.json" ) ;
92- getTypingNamesFromJson ( packageJsonPath , filesToWatch ) ;
75+ const possibleSearchDirs = map ( fileNames , getDirectoryPath ) ;
76+ if ( projectRootPath !== undefined ) {
77+ possibleSearchDirs . push ( projectRootPath ) ;
78+ }
79+ searchDirs = deduplicate ( possibleSearchDirs ) ;
80+ for ( const searchDir of searchDirs ) {
81+ const packageJsonPath = combinePaths ( searchDir , "package.json" ) ;
82+ getTypingNamesFromJson ( packageJsonPath , filesToWatch ) ;
9383
94- const bowerJsonPath = combinePaths ( searchDir , "bower.json" ) ;
95- getTypingNamesFromJson ( bowerJsonPath , filesToWatch ) ;
84+ const bowerJsonPath = combinePaths ( searchDir , "bower.json" ) ;
85+ getTypingNamesFromJson ( bowerJsonPath , filesToWatch ) ;
9686
97- const nodeModulesPath = combinePaths ( searchDir , "node_modules" ) ;
98- getTypingNamesFromNodeModuleFolder ( nodeModulesPath , filesToWatch ) ;
99- }
100- getTypingNamesFromSourceFileNames ( fileNames ) ;
87+ const nodeModulesPath = combinePaths ( searchDir , "node_modules" ) ;
88+ getTypingNamesFromNodeModuleFolder ( nodeModulesPath , filesToWatch ) ;
10189 }
90+ getTypingNamesFromSourceFileNames ( fileNames ) ;
10291
103- const typingsPath = combinePaths ( cachePath , "typings" ) ;
104- const tsdJsonPath = combinePaths ( cachePath , "tsd.json" ) ;
105- const result = readConfigFile ( tsdJsonPath , host . readFile ) ;
106- if ( result . config ) {
107- const tsdJson : TsdJson = result . config ;
108-
109- // The "installed" property in the tsd.json serves as a registry of installed typings. Each item
110- // of this object has a key of the relative file path, and a value that contains the corresponding
111- // commit hash.
112- if ( tsdJson . installed ) {
113- for ( const cachedTypingPath in tsdJson . installed ) {
114- // Assuming the cachedTypingPath has the format of "[package name]/[file name]"
115- const cachedTypingName = cachedTypingPath . substr ( 0 , cachedTypingPath . indexOf ( "/" ) ) ;
116- // If the inferred[cachedTypingName] is already not null, which means we found a corresponding
117- // d.ts file that coming with the package. That one should take higher priority.
118- if ( hasProperty ( inferredTypings , cachedTypingName ) && ! inferredTypings [ cachedTypingName ] ) {
119- inferredTypings [ cachedTypingName ] = combinePaths ( typingsPath , cachedTypingPath ) ;
120- }
121- }
92+ // Add the cached typing locations for inferred typings that are already installed
93+ for ( const name in packageNameToTypingLocation ) {
94+ if ( hasProperty ( inferredTypings , name ) && ! inferredTypings [ name ] ) {
95+ inferredTypings [ name ] = packageNameToTypingLocation [ name ] ;
12296 }
12397 }
12498
@@ -158,7 +132,7 @@ namespace ts.JsTyping {
158132 * Get the typing info from common package manager json files like package.json or bower.json
159133 */
160134 function getTypingNamesFromJson ( jsonPath : string , filesToWatch : string [ ] ) {
161- const result = readConfigFile ( jsonPath , host . readFile ) ;
135+ const result = readConfigFile ( jsonPath , ( path : string ) => host . readFile ( path ) ) ;
162136 if ( result . config ) {
163137 const jsonConfig : PackageJson = result . config ;
164138 filesToWatch . push ( jsonPath ) ;
@@ -215,7 +189,7 @@ namespace ts.JsTyping {
215189 for ( const fileName of fileNames ) {
216190 const normalizedFileName = normalizePath ( fileName ) ;
217191 if ( getBaseFileName ( normalizedFileName ) !== "package.json" ) { continue ; }
218- const result = readConfigFile ( normalizedFileName , host . readFile ) ;
192+ const result = readConfigFile ( normalizedFileName , ( path : string ) => host . readFile ( path ) ) ;
219193 if ( ! result . config ) { continue ; }
220194 const packageJson : PackageJson = result . config ;
221195 filesToWatch . push ( normalizedFileName ) ;
0 commit comments