1
1
import { resolve } from 'path' ;
2
- import type * as _ts from 'typescript ' ;
2
+ import type { TSCommon , TSInternal } from './ts-compiler-types ' ;
3
3
import type { ProjectLocalResolveHelper } from './util' ;
4
4
5
5
/**
6
6
* @internal
7
7
* In a factory because these are shared across both CompilerHost and LanguageService codepaths
8
8
*/
9
9
export function createResolverFunctions ( kwargs : {
10
- ts : typeof _ts ;
11
- host : _ts . ModuleResolutionHost ;
10
+ ts : TSCommon ;
11
+ host : TSCommon . ModuleResolutionHost ;
12
12
cwd : string ;
13
13
getCanonicalFileName : ( filename : string ) => string ;
14
- config : _ts . ParsedCommandLine ;
14
+ config : TSCommon . ParsedCommandLine ;
15
15
projectLocalResolveHelper : ProjectLocalResolveHelper ;
16
16
} ) {
17
17
const {
@@ -58,7 +58,9 @@ export function createResolverFunctions(kwargs: {
58
58
* If we need to emit JS for a file, force TS to consider it non-external
59
59
*/
60
60
const fixupResolvedModule = (
61
- resolvedModule : _ts . ResolvedModule | _ts . ResolvedTypeReferenceDirective
61
+ resolvedModule :
62
+ | TSCommon . ResolvedModule
63
+ | TSCommon . ResolvedTypeReferenceDirective
62
64
) => {
63
65
const { resolvedFileName } = resolvedModule ;
64
66
if ( resolvedFileName === undefined ) return ;
@@ -82,35 +84,36 @@ export function createResolverFunctions(kwargs: {
82
84
* Older ts versions do not pass `redirectedReference` nor `options`.
83
85
* We must pass `redirectedReference` to newer ts versions, but cannot rely on `options`, hence the weird argument name
84
86
*/
85
- const resolveModuleNames : _ts . LanguageServiceHost [ 'resolveModuleNames' ] = (
86
- moduleNames : string [ ] ,
87
- containingFile : string ,
88
- reusedNames : string [ ] | undefined ,
89
- redirectedReference : _ts . ResolvedProjectReference | undefined ,
90
- optionsOnlyWithNewerTsVersions : _ts . CompilerOptions
91
- ) : ( _ts . ResolvedModule | undefined ) [ ] => {
92
- return moduleNames . map ( ( moduleName ) => {
93
- const { resolvedModule } = ts . resolveModuleName (
94
- moduleName ,
95
- containingFile ,
96
- config . options ,
97
- host ,
98
- moduleResolutionCache ,
99
- redirectedReference
100
- ) ;
101
- if ( resolvedModule ) {
102
- fixupResolvedModule ( resolvedModule ) ;
103
- }
104
- return resolvedModule ;
105
- } ) ;
106
- } ;
87
+ const resolveModuleNames : TSCommon . LanguageServiceHost [ 'resolveModuleNames' ] =
88
+ (
89
+ moduleNames : string [ ] ,
90
+ containingFile : string ,
91
+ reusedNames : string [ ] | undefined ,
92
+ redirectedReference : TSCommon . ResolvedProjectReference | undefined ,
93
+ optionsOnlyWithNewerTsVersions : TSCommon . CompilerOptions
94
+ ) : ( TSCommon . ResolvedModule | undefined ) [ ] => {
95
+ return moduleNames . map ( ( moduleName ) => {
96
+ const { resolvedModule } = ts . resolveModuleName (
97
+ moduleName ,
98
+ containingFile ,
99
+ config . options ,
100
+ host ,
101
+ moduleResolutionCache ,
102
+ redirectedReference
103
+ ) ;
104
+ if ( resolvedModule ) {
105
+ fixupResolvedModule ( resolvedModule ) ;
106
+ }
107
+ return resolvedModule ;
108
+ } ) ;
109
+ } ;
107
110
108
111
// language service never calls this, but TS docs recommend that we implement it
109
- const getResolvedModuleWithFailedLookupLocationsFromCache : _ts . LanguageServiceHost [ 'getResolvedModuleWithFailedLookupLocationsFromCache' ] =
112
+ const getResolvedModuleWithFailedLookupLocationsFromCache : TSCommon . LanguageServiceHost [ 'getResolvedModuleWithFailedLookupLocationsFromCache' ] =
110
113
(
111
114
moduleName ,
112
115
containingFile
113
- ) : _ts . ResolvedModuleWithFailedLookupLocations | undefined => {
116
+ ) : TSCommon . ResolvedModuleWithFailedLookupLocations | undefined => {
114
117
const ret = ts . resolveModuleNameFromCache (
115
118
moduleName ,
116
119
containingFile ,
@@ -122,22 +125,37 @@ export function createResolverFunctions(kwargs: {
122
125
return ret ;
123
126
} ;
124
127
125
- const resolveTypeReferenceDirectives : _ts . LanguageServiceHost [ 'resolveTypeReferenceDirectives' ] =
128
+ const resolveTypeReferenceDirectives : TSCommon . LanguageServiceHost [ 'resolveTypeReferenceDirectives' ] =
126
129
(
127
- typeDirectiveNames : string [ ] ,
130
+ typeDirectiveNames : string [ ] | readonly TSCommon . FileReference [ ] ,
128
131
containingFile : string ,
129
- redirectedReference : _ts . ResolvedProjectReference | undefined ,
130
- options : _ts . CompilerOptions
131
- ) : ( _ts . ResolvedTypeReferenceDirective | undefined ) [ ] => {
132
+ redirectedReference : TSCommon . ResolvedProjectReference | undefined ,
133
+ options : TSCommon . CompilerOptions ,
134
+ containingFileMode ?: TSCommon . SourceFile [ 'impliedNodeFormat' ] | undefined // new impliedNodeFormat is accepted by compilerHost
135
+ ) : ( TSCommon . ResolvedTypeReferenceDirective | undefined ) [ ] => {
132
136
// Note: seems to be called with empty typeDirectiveNames array for all files.
137
+ // TODO consider using `ts.loadWithTypeDirectiveCache`
133
138
return typeDirectiveNames . map ( ( typeDirectiveName ) => {
139
+ // Copy-pasted from TS source:
140
+ const nameIsString = typeof typeDirectiveName === 'string' ;
141
+ const mode = nameIsString
142
+ ? undefined
143
+ : ( ts as any as TSInternal ) . getModeForFileReference ! (
144
+ typeDirectiveName ,
145
+ containingFileMode
146
+ ) ;
147
+ const strName = nameIsString
148
+ ? typeDirectiveName
149
+ : typeDirectiveName . fileName . toLowerCase ( ) ;
134
150
let { resolvedTypeReferenceDirective } =
135
151
ts . resolveTypeReferenceDirective (
136
- typeDirectiveName ,
152
+ strName ,
137
153
containingFile ,
138
154
config . options ,
139
155
host ,
140
- redirectedReference
156
+ redirectedReference ,
157
+ undefined ,
158
+ mode
141
159
) ;
142
160
if ( typeDirectiveName === 'node' && ! resolvedTypeReferenceDirective ) {
143
161
// Resolve @types /node relative to project first, then __dirname (copy logic from elsewhere / refactor into reusable function)
0 commit comments