@@ -13,23 +13,11 @@ namespace ts.JsTyping {
13
13
readDirectory : ( path : string , extension ?: string , exclude ?: string [ ] , depth ?: number ) => string [ ] ;
14
14
} ;
15
15
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
-
28
16
interface PackageJson {
29
17
_requiredBy ?: string [ ] ;
30
18
dependencies ?: Map < string > ;
31
19
devDependencies ?: Map < string > ;
32
- name : string ;
20
+ name ? : string ;
33
21
optionalDependencies ?: Map < string > ;
34
22
peerDependencies ?: Map < string > ;
35
23
typings ?: string ;
@@ -41,19 +29,21 @@ namespace ts.JsTyping {
41
29
42
30
/**
43
31
* @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
45
33
* @param cachePath is the path to the typings cache
46
34
* @param projectRootPath is the path to the project root directory
47
35
* @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
50
39
*/
51
40
export function discoverTypings (
52
41
host : TypingResolutionHost ,
53
42
fileNames : string [ ] ,
54
43
cachePath : Path ,
55
44
projectRootPath : Path ,
56
45
safeListPath : Path ,
46
+ packageNameToTypingLocation : Map < string > ,
57
47
typingOptions : TypingOptions ,
58
48
compilerOptions : CompilerOptions ) :
59
49
{ cachedTypingPaths : string [ ] , newTypingNames : string [ ] , filesToWatch : string [ ] } {
@@ -69,8 +59,9 @@ namespace ts.JsTyping {
69
59
fileNames = filter ( map ( fileNames , normalizePath ) , f => scriptKindIs ( f , /*LanguageServiceHost*/ undefined , ScriptKind . JS , ScriptKind . JSX ) ) ;
70
60
71
61
if ( ! safeList ) {
72
- const result = readConfigFile ( safeListPath , host . readFile ) ;
62
+ const result = readConfigFile ( safeListPath , ( path : string ) => host . readFile ( path ) ) ;
73
63
if ( result . config ) { safeList = result . config ; }
64
+ else { safeList = { } ; } ;
74
65
}
75
66
76
67
const filesToWatch : string [ ] = [ ] ;
@@ -81,44 +72,27 @@ namespace ts.JsTyping {
81
72
mergeTypings ( typingOptions . include ) ;
82
73
exclude = typingOptions . exclude || [ ] ;
83
74
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 ) ;
93
83
94
- const bowerJsonPath = combinePaths ( searchDir , "bower.json" ) ;
95
- getTypingNamesFromJson ( bowerJsonPath , filesToWatch ) ;
84
+ const bowerJsonPath = combinePaths ( searchDir , "bower.json" ) ;
85
+ getTypingNamesFromJson ( bowerJsonPath , filesToWatch ) ;
96
86
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 ) ;
101
89
}
90
+ getTypingNamesFromSourceFileNames ( fileNames ) ;
102
91
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 ] ;
122
96
}
123
97
}
124
98
@@ -158,7 +132,7 @@ namespace ts.JsTyping {
158
132
* Get the typing info from common package manager json files like package.json or bower.json
159
133
*/
160
134
function getTypingNamesFromJson ( jsonPath : string , filesToWatch : string [ ] ) {
161
- const result = readConfigFile ( jsonPath , host . readFile ) ;
135
+ const result = readConfigFile ( jsonPath , ( path : string ) => host . readFile ( path ) ) ;
162
136
if ( result . config ) {
163
137
const jsonConfig : PackageJson = result . config ;
164
138
filesToWatch . push ( jsonPath ) ;
@@ -215,7 +189,7 @@ namespace ts.JsTyping {
215
189
for ( const fileName of fileNames ) {
216
190
const normalizedFileName = normalizePath ( fileName ) ;
217
191
if ( getBaseFileName ( normalizedFileName ) !== "package.json" ) { continue ; }
218
- const result = readConfigFile ( normalizedFileName , host . readFile ) ;
192
+ const result = readConfigFile ( normalizedFileName , ( path : string ) => host . readFile ( path ) ) ;
219
193
if ( ! result . config ) { continue ; }
220
194
const packageJson : PackageJson = result . config ;
221
195
filesToWatch . push ( normalizedFileName ) ;
0 commit comments