@@ -13,14 +13,8 @@ export function createResolverFunctions(kwargs: {
1313 config : _ts . ParsedCommandLine ;
1414 configFilePath : string | undefined ;
1515} ) {
16- const {
17- host,
18- ts,
19- config,
20- cwd,
21- getCanonicalFileName,
22- configFilePath,
23- } = kwargs ;
16+ const { host, ts, config, cwd, getCanonicalFileName, configFilePath } =
17+ kwargs ;
2418 const moduleResolutionCache = ts . createModuleResolutionCache (
2519 cwd ,
2620 getCanonicalFileName ,
@@ -105,69 +99,71 @@ export function createResolverFunctions(kwargs: {
10599 } ;
106100
107101 // language service never calls this, but TS docs recommend that we implement it
108- const getResolvedModuleWithFailedLookupLocationsFromCache : _ts . LanguageServiceHost [ 'getResolvedModuleWithFailedLookupLocationsFromCache' ] = (
109- moduleName ,
110- containingFile
111- ) : _ts . ResolvedModuleWithFailedLookupLocations | undefined => {
112- const ret = ts . resolveModuleNameFromCache (
102+ const getResolvedModuleWithFailedLookupLocationsFromCache : _ts . LanguageServiceHost [ 'getResolvedModuleWithFailedLookupLocationsFromCache' ] =
103+ (
113104 moduleName ,
114- containingFile ,
115- moduleResolutionCache
116- ) ;
117- if ( ret && ret . resolvedModule ) {
118- fixupResolvedModule ( ret . resolvedModule ) ;
119- }
120- return ret ;
121- } ;
122-
123- const resolveTypeReferenceDirectives : _ts . LanguageServiceHost [ 'resolveTypeReferenceDirectives' ] = (
124- typeDirectiveNames : string [ ] ,
125- containingFile : string ,
126- redirectedReference : _ts . ResolvedProjectReference | undefined ,
127- options : _ts . CompilerOptions
128- ) : ( _ts . ResolvedTypeReferenceDirective | undefined ) [ ] => {
129- // Note: seems to be called with empty typeDirectiveNames array for all files.
130- return typeDirectiveNames . map ( ( typeDirectiveName ) => {
131- let { resolvedTypeReferenceDirective } = ts . resolveTypeReferenceDirective (
132- typeDirectiveName ,
105+ containingFile
106+ ) : _ts . ResolvedModuleWithFailedLookupLocations | undefined => {
107+ const ret = ts . resolveModuleNameFromCache (
108+ moduleName ,
133109 containingFile ,
134- config . options ,
135- host ,
136- redirectedReference
110+ moduleResolutionCache
137111 ) ;
138- if ( typeDirectiveName === 'node' && ! resolvedTypeReferenceDirective ) {
139- // Resolve @types /node relative to project first, then __dirname (copy logic from elsewhere / refactor into reusable function)
140- let typesNodePackageJsonPath : string | undefined ;
141- try {
142- typesNodePackageJsonPath = require . resolve (
143- '@types/node/package.json' ,
144- {
145- paths : [ configFilePath ?? cwd , __dirname ] ,
146- }
147- ) ;
148- } catch { } // gracefully do nothing when @types /node is not installed for any reason
149- if ( typesNodePackageJsonPath ) {
150- const typeRoots = [ resolve ( typesNodePackageJsonPath , '../..' ) ] ;
151- ( {
152- resolvedTypeReferenceDirective,
153- } = ts . resolveTypeReferenceDirective (
112+ if ( ret && ret . resolvedModule ) {
113+ fixupResolvedModule ( ret . resolvedModule ) ;
114+ }
115+ return ret ;
116+ } ;
117+
118+ const resolveTypeReferenceDirectives : _ts . LanguageServiceHost [ 'resolveTypeReferenceDirectives' ] =
119+ (
120+ typeDirectiveNames : string [ ] ,
121+ containingFile : string ,
122+ redirectedReference : _ts . ResolvedProjectReference | undefined ,
123+ options : _ts . CompilerOptions
124+ ) : ( _ts . ResolvedTypeReferenceDirective | undefined ) [ ] => {
125+ // Note: seems to be called with empty typeDirectiveNames array for all files.
126+ return typeDirectiveNames . map ( ( typeDirectiveName ) => {
127+ let { resolvedTypeReferenceDirective } =
128+ ts . resolveTypeReferenceDirective (
154129 typeDirectiveName ,
155130 containingFile ,
156- {
157- ...config . options ,
158- typeRoots,
159- } ,
131+ config . options ,
160132 host ,
161133 redirectedReference
162- ) ) ;
134+ ) ;
135+ if ( typeDirectiveName === 'node' && ! resolvedTypeReferenceDirective ) {
136+ // Resolve @types /node relative to project first, then __dirname (copy logic from elsewhere / refactor into reusable function)
137+ let typesNodePackageJsonPath : string | undefined ;
138+ try {
139+ typesNodePackageJsonPath = require . resolve (
140+ '@types/node/package.json' ,
141+ {
142+ paths : [ configFilePath ?? cwd , __dirname ] ,
143+ }
144+ ) ;
145+ } catch { } // gracefully do nothing when @types /node is not installed for any reason
146+ if ( typesNodePackageJsonPath ) {
147+ const typeRoots = [ resolve ( typesNodePackageJsonPath , '../..' ) ] ;
148+ ( { resolvedTypeReferenceDirective } =
149+ ts . resolveTypeReferenceDirective (
150+ typeDirectiveName ,
151+ containingFile ,
152+ {
153+ ...config . options ,
154+ typeRoots,
155+ } ,
156+ host ,
157+ redirectedReference
158+ ) ) ;
159+ }
163160 }
164- }
165- if ( resolvedTypeReferenceDirective ) {
166- fixupResolvedModule ( resolvedTypeReferenceDirective ) ;
167- }
168- return resolvedTypeReferenceDirective ;
169- } ) ;
170- } ;
161+ if ( resolvedTypeReferenceDirective ) {
162+ fixupResolvedModule ( resolvedTypeReferenceDirective ) ;
163+ }
164+ return resolvedTypeReferenceDirective ;
165+ } ) ;
166+ } ;
171167
172168 return {
173169 resolveModuleNames,
0 commit comments