6
6
/* @internal */
7
7
namespace ts . JsTyping {
8
8
9
- export interface TypingResolutionHost {
9
+ export interface TypingResolutionHost extends ExtensionHost {
10
10
directoryExists : ( path : string ) => boolean ;
11
11
fileExists : ( fileName : string ) => boolean ;
12
12
readFile : ( path : string , encoding ?: string ) => string ;
@@ -35,6 +35,7 @@ namespace ts.JsTyping {
35
35
* @param packageNameToTypingLocation is the map of package names to their cached typing locations
36
36
* @param typingOptions are used to customize the typing inference process
37
37
* @param compilerOptions are used as a source for typing inference
38
+ * @param cache is the optional extension cache to lookup type discovery extensions in - one will be made if it cannot be provided
38
39
*/
39
40
export function discoverTypings (
40
41
host : TypingResolutionHost ,
@@ -43,7 +44,8 @@ namespace ts.JsTyping {
43
44
safeListPath : Path ,
44
45
packageNameToTypingLocation : Map < string > ,
45
46
typingOptions : TypingOptions ,
46
- compilerOptions : CompilerOptions ) :
47
+ compilerOptions : CompilerOptions ,
48
+ cache : ExtensionCache = createExtensionCache ( compilerOptions , host ) ) :
47
49
{ cachedTypingPaths : string [ ] , newTypingNames : string [ ] , filesToWatch : string [ ] } {
48
50
49
51
// A typing name to typing file path mapping
@@ -88,6 +90,8 @@ namespace ts.JsTyping {
88
90
89
91
const nodeModulesPath = combinePaths ( searchDir , "node_modules" ) ;
90
92
getTypingNamesFromNodeModuleFolder ( nodeModulesPath ) ;
93
+
94
+ getTypingNamesFromExtensions ( searchDir , cache ) ;
91
95
}
92
96
getTypingNamesFromSourceFileNames ( fileNames ) ;
93
97
@@ -223,5 +227,24 @@ namespace ts.JsTyping {
223
227
mergeTypings ( typingNames ) ;
224
228
}
225
229
230
+
231
+ function getTypingNamesFromExtensions ( searchPath : string , cache : ExtensionCache ) {
232
+ // We don't report issues loading type discovery extensions (if the host cares about them, it should load them in advance and pass in a cache)
233
+ const extensions = cache . getCompilerExtensions ( ) [ "type-discovery" ] ;
234
+ for ( const extension of extensions ) {
235
+ let results : string [ ] ;
236
+ try {
237
+ startExtensionProfile ( compilerOptions . extendedDiagnostics , extension . name , "lookup" ) ;
238
+ results = extension . lookup ( searchPath , extension . args ) ;
239
+ completeExtensionProfile ( compilerOptions . extendedDiagnostics , extension . name , "lookup" ) ;
240
+ }
241
+ catch ( e ) {
242
+ // There's presently no way to report errors during discover typings other than a hard failure (which is to be avoided)
243
+ }
244
+ if ( results ) {
245
+ mergeTypings ( results ) ;
246
+ }
247
+ }
248
+ }
226
249
}
227
250
}
0 commit comments