Skip to content

Commit 63a2f83

Browse files
authored
Fix #1647 (#1648)
* fix * Fix * fix
1 parent abc616e commit 63a2f83

File tree

5 files changed

+132
-96
lines changed

5 files changed

+132
-96
lines changed

package-lock.json

+30-55
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@
134134
"semver": "^7.1.3",
135135
"throat": "^6.0.1",
136136
"typedoc": "^0.22.10",
137-
"typescript": "4.5.2",
138-
"typescript-json-schema": "^0.51.0",
137+
"typescript": "4.5.5",
138+
"typescript-json-schema": "^0.53.0",
139139
"util.promisify": "^1.0.1"
140140
},
141141
"peerDependencies": {

src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ export function create(rawOptions: CreateOptions = {}): Service {
605605
const projectLocalResolveHelper =
606606
createProjectLocalResolveHelper(relativeToPath);
607607
const compiler = projectLocalResolveHelper(name || 'typescript', true);
608-
const ts: typeof _ts = attemptRequireWithV8CompileCache(require, compiler);
608+
const ts: TSCommon = attemptRequireWithV8CompileCache(require, compiler);
609609
return { compiler, ts, projectLocalResolveHelper };
610610
}
611611

src/resolver-functions.ts

+54-36
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import { resolve } from 'path';
2-
import type * as _ts from 'typescript';
2+
import type { TSCommon, TSInternal } from './ts-compiler-types';
33
import type { ProjectLocalResolveHelper } from './util';
44

55
/**
66
* @internal
77
* In a factory because these are shared across both CompilerHost and LanguageService codepaths
88
*/
99
export function createResolverFunctions(kwargs: {
10-
ts: typeof _ts;
11-
host: _ts.ModuleResolutionHost;
10+
ts: TSCommon;
11+
host: TSCommon.ModuleResolutionHost;
1212
cwd: string;
1313
getCanonicalFileName: (filename: string) => string;
14-
config: _ts.ParsedCommandLine;
14+
config: TSCommon.ParsedCommandLine;
1515
projectLocalResolveHelper: ProjectLocalResolveHelper;
1616
}) {
1717
const {
@@ -58,7 +58,9 @@ export function createResolverFunctions(kwargs: {
5858
* If we need to emit JS for a file, force TS to consider it non-external
5959
*/
6060
const fixupResolvedModule = (
61-
resolvedModule: _ts.ResolvedModule | _ts.ResolvedTypeReferenceDirective
61+
resolvedModule:
62+
| TSCommon.ResolvedModule
63+
| TSCommon.ResolvedTypeReferenceDirective
6264
) => {
6365
const { resolvedFileName } = resolvedModule;
6466
if (resolvedFileName === undefined) return;
@@ -82,35 +84,36 @@ export function createResolverFunctions(kwargs: {
8284
* Older ts versions do not pass `redirectedReference` nor `options`.
8385
* We must pass `redirectedReference` to newer ts versions, but cannot rely on `options`, hence the weird argument name
8486
*/
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+
};
107110

108111
// 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'] =
110113
(
111114
moduleName,
112115
containingFile
113-
): _ts.ResolvedModuleWithFailedLookupLocations | undefined => {
116+
): TSCommon.ResolvedModuleWithFailedLookupLocations | undefined => {
114117
const ret = ts.resolveModuleNameFromCache(
115118
moduleName,
116119
containingFile,
@@ -122,22 +125,37 @@ export function createResolverFunctions(kwargs: {
122125
return ret;
123126
};
124127

125-
const resolveTypeReferenceDirectives: _ts.LanguageServiceHost['resolveTypeReferenceDirectives'] =
128+
const resolveTypeReferenceDirectives: TSCommon.LanguageServiceHost['resolveTypeReferenceDirectives'] =
126129
(
127-
typeDirectiveNames: string[],
130+
typeDirectiveNames: string[] | readonly TSCommon.FileReference[],
128131
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)[] => {
132136
// Note: seems to be called with empty typeDirectiveNames array for all files.
137+
// TODO consider using `ts.loadWithTypeDirectiveCache`
133138
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();
134150
let { resolvedTypeReferenceDirective } =
135151
ts.resolveTypeReferenceDirective(
136-
typeDirectiveName,
152+
strName,
137153
containingFile,
138154
config.options,
139155
host,
140-
redirectedReference
156+
redirectedReference,
157+
undefined,
158+
mode
141159
);
142160
if (typeDirectiveName === 'node' && !resolvedTypeReferenceDirective) {
143161
// Resolve @types/node relative to project first, then __dirname (copy logic from elsewhere / refactor into reusable function)

0 commit comments

Comments
 (0)