@@ -29,17 +29,6 @@ namespace ts.JsTyping {
29
29
return undefined ;
30
30
}
31
31
32
- function isTypingEnabled ( options : TypingOptions ) : boolean {
33
- if ( options ) {
34
- if ( options . enableAutoDiscovery ||
35
- ( options . include && options . include . length > 0 ) ||
36
- ( options . exclude && options . exclude . length > 0 ) ) {
37
- return true ;
38
- }
39
- }
40
- return false ;
41
- }
42
-
43
32
/**
44
33
* @param host is the object providing I/O related operations.
45
34
* @param fileNames are the file names that belong to the same project.
@@ -60,15 +49,15 @@ namespace ts.JsTyping {
60
49
// A typing name to typing file path mapping
61
50
const inferredTypings : Map < string > = { } ;
62
51
63
- if ( ! isTypingEnabled ( typingOptions ) ) {
52
+ if ( ! typingOptions || ! typingOptions . enableAutoDiscovery ) {
64
53
return { cachedTypingPaths : [ ] , newTypingNames : [ ] , filesToWatch : [ ] } ;
65
54
}
66
55
67
56
const cachePath = projectRootPath || globalCachePath ;
68
57
// Only infer typings for .js and .jsx files
69
- fileNames = filter ( map ( fileNames , ts . normalizePath ) , f => scriptKindIs ( f , /*LanguageServiceHost*/ undefined , ScriptKind . JS , ScriptKind . JSX ) ) ;
58
+ fileNames = filter ( map ( fileNames , normalizePath ) , f => scriptKindIs ( f , /*LanguageServiceHost*/ undefined , ScriptKind . JS , ScriptKind . JSX ) ) ;
70
59
71
- const safeListFilePath = ts . combinePaths ( globalCachePath , "safeList.json" ) ;
60
+ const safeListFilePath = combinePaths ( globalCachePath , "safeList.json" ) ;
72
61
if ( ! safeList && host . fileExists ( safeListFilePath ) ) {
73
62
safeList = tryParseJson ( safeListFilePath , host ) ;
74
63
}
@@ -82,28 +71,28 @@ namespace ts.JsTyping {
82
71
exclude = typingOptions . exclude || [ ] ;
83
72
84
73
if ( typingOptions . enableAutoDiscovery ) {
85
- const possibleSearchDirs = map ( fileNames , ts . getDirectoryPath ) ;
74
+ const possibleSearchDirs = map ( fileNames , getDirectoryPath ) ;
86
75
if ( projectRootPath !== undefined ) {
87
76
possibleSearchDirs . push ( projectRootPath ) ;
88
77
}
89
- searchDirs = ts . deduplicate ( possibleSearchDirs ) ;
78
+ searchDirs = deduplicate ( possibleSearchDirs ) ;
90
79
for ( const searchDir of searchDirs ) {
91
- const packageJsonPath = ts . combinePaths ( searchDir , "package.json" ) ;
80
+ const packageJsonPath = combinePaths ( searchDir , "package.json" ) ;
92
81
getTypingNamesFromJson ( packageJsonPath , filesToWatch ) ;
93
82
94
- const bowerJsonPath = ts . combinePaths ( searchDir , "bower.json" ) ;
83
+ const bowerJsonPath = combinePaths ( searchDir , "bower.json" ) ;
95
84
getTypingNamesFromJson ( bowerJsonPath , filesToWatch ) ;
96
85
97
- const nodeModulesPath = ts . combinePaths ( searchDir , "node_modules" ) ;
86
+ const nodeModulesPath = combinePaths ( searchDir , "node_modules" ) ;
98
87
getTypingNamesFromNodeModuleFolder ( nodeModulesPath , filesToWatch ) ;
99
88
}
100
89
101
90
getTypingNamesFromSourceFileNames ( fileNames ) ;
102
91
getTypingNamesFromCompilerOptions ( compilerOptions ) ;
103
92
}
104
93
105
- const typingsPath = ts . combinePaths ( cachePath , "typings" ) ;
106
- const tsdJsonPath = ts . combinePaths ( cachePath , "tsd.json" ) ;
94
+ const typingsPath = combinePaths ( cachePath , "typings" ) ;
95
+ const tsdJsonPath = combinePaths ( cachePath , "tsd.json" ) ;
107
96
const tsdJsonDict = tryParseJson ( tsdJsonPath , host ) ;
108
97
if ( tsdJsonDict ) {
109
98
for ( const notFoundTypingName of notFoundTypingNames ) {
@@ -122,7 +111,7 @@ namespace ts.JsTyping {
122
111
// If the inferred[cachedTypingName] is already not null, which means we found a corresponding
123
112
// d.ts file that coming with the package. That one should take higher priority.
124
113
if ( hasProperty ( inferredTypings , cachedTypingName ) && ! inferredTypings [ cachedTypingName ] ) {
125
- inferredTypings [ cachedTypingName ] = ts . combinePaths ( typingsPath , cachedTypingPath ) ;
114
+ inferredTypings [ cachedTypingName ] = combinePaths ( typingsPath , cachedTypingPath ) ;
126
115
}
127
116
}
128
117
}
@@ -190,7 +179,7 @@ namespace ts.JsTyping {
190
179
*/
191
180
function getTypingNamesFromSourceFileNames ( fileNames : string [ ] ) {
192
181
const jsFileNames = filter ( fileNames , hasJavaScriptFileExtension ) ;
193
- const inferredTypingNames = map ( jsFileNames , f => ts . removeFileExtension ( ts . getBaseFileName ( f . toLowerCase ( ) ) ) ) ;
182
+ const inferredTypingNames = map ( jsFileNames , f => removeFileExtension ( getBaseFileName ( f . toLowerCase ( ) ) ) ) ;
194
183
const cleanedTypingNames = map ( inferredTypingNames , f => f . replace ( / ( (?: \. | - ) m i n (? = \. | $ ) ) | ( (?: - | \. ) \d + ) / g, "" ) ) ;
195
184
if ( safeList === undefined ) {
196
185
mergeTypings ( cleanedTypingNames ) ;
@@ -216,16 +205,13 @@ namespace ts.JsTyping {
216
205
}
217
206
218
207
const typingNames : string [ ] = [ ] ;
219
- const packageJsonFiles =
220
- filter (
221
- host . readDirectory ( nodeModulesPath , /*extension*/ undefined , /*exclude*/ undefined , /*depth*/ 2 ) ,
222
- f => ts . getBaseFileName ( f ) === "package.json" ) ;
223
-
224
- for ( const packageJsonFile of packageJsonFiles ) {
225
- const packageJsonDict = tryParseJson ( packageJsonFile , host ) ;
208
+ const jsonFiles = host . readDirectory ( nodeModulesPath , "*.json" , /*exclude*/ undefined , /*depth*/ 2 ) ;
209
+ for ( const jsonFile of jsonFiles ) {
210
+ if ( getBaseFileName ( jsonFile ) !== "package.json" ) { continue ; }
211
+ const packageJsonDict = tryParseJson ( jsonFile , host ) ;
226
212
if ( ! packageJsonDict ) { continue ; }
227
213
228
- filesToWatch . push ( packageJsonFile ) ;
214
+ filesToWatch . push ( jsonFile ) ;
229
215
230
216
// npm 3 has the package.json contains a "_requiredBy" field
231
217
// we should include all the top level module names for npm 2, and only module names whose
@@ -239,8 +225,8 @@ namespace ts.JsTyping {
239
225
// to download d.ts files from DefinitelyTyped
240
226
const packageName = packageJsonDict [ "name" ] ;
241
227
if ( hasProperty ( packageJsonDict , "typings" ) ) {
242
- const absPath = ts . getNormalizedAbsolutePath ( packageJsonDict . typings , ts . getDirectoryPath ( packageJsonFile ) ) ;
243
- inferredTypings [ packageName ] = absPath ;
228
+ const absolutePath = getNormalizedAbsolutePath ( packageJsonDict . typings , getDirectoryPath ( jsonFile ) ) ;
229
+ inferredTypings [ packageName ] = absolutePath ;
244
230
}
245
231
else {
246
232
typingNames . push ( packageName ) ;
@@ -267,7 +253,7 @@ namespace ts.JsTyping {
267
253
* @param host The object providing I/O related operations.
268
254
*/
269
255
export function updateNotFoundTypingNames ( newTypingNames : string [ ] , cachePath : string , host : TypingResolutionHost ) : void {
270
- const tsdJsonPath = ts . combinePaths ( cachePath , "tsd.json" ) ;
256
+ const tsdJsonPath = combinePaths ( cachePath , "tsd.json" ) ;
271
257
const cacheTsdJsonDict = tryParseJson ( tsdJsonPath , host ) ;
272
258
if ( cacheTsdJsonDict ) {
273
259
const installedTypingFiles = hasProperty ( cacheTsdJsonDict , "installed" )
0 commit comments