Skip to content

Commit 0590439

Browse files
refactor: Split up MultiCompilerHost
The logic of the MultiCompilerHost almost only operated on one CompilerHost plus respective CompilerOptions and caches, so it makes sense to encapsulate this logic for one instance of these properties, instead of requiring each of these properities to be a Record and having to index them all the time. The resulting code is less repetitive and more readable.
1 parent 4b35f7b commit 0590439

File tree

5 files changed

+116
-165
lines changed

5 files changed

+116
-165
lines changed

packages/core/src/checkPackage.ts

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import type {
99
Resolution,
1010
EntrypointInfo,
1111
} from "./types.js";
12-
import { createMultiCompilerHost, type MultiCompilerHost } from "./multiCompilerHost.js";
12+
import { createCompilerHosts, type CompilerHosts, CompilerHostWrapper } from "./multiCompilerHost.js";
1313
import { getEntrypointResolutionProblems } from "./checks/entrypointResolutionProblems.js";
1414
import { getResolutionBasedFileProblems } from "./checks/resolutionBasedFileProblems.js";
1515
import { getFileProblems } from "./checks/fileProblems.js";
@@ -42,11 +42,11 @@ async function checkPackageWorker(packageFS: FS): Promise<CheckResult> {
4242
return { packageName, packageVersion, types };
4343
}
4444

45-
const host = createMultiCompilerHost(packageFS);
46-
const entrypointResolutions = getEntrypointInfo(packageName, packageFS, host);
47-
const entrypointResolutionProblems = getEntrypointResolutionProblems(entrypointResolutions, host);
48-
const resolutionBasedFileProblems = getResolutionBasedFileProblems(packageName, entrypointResolutions, host);
49-
const fileProblems = getFileProblems(entrypointResolutions, host);
45+
const hosts = createCompilerHosts(packageFS);
46+
const entrypointResolutions = getEntrypointInfo(packageName, packageFS, hosts);
47+
const entrypointResolutionProblems = getEntrypointResolutionProblems(entrypointResolutions, hosts);
48+
const resolutionBasedFileProblems = getResolutionBasedFileProblems(packageName, entrypointResolutions, hosts);
49+
const fileProblems = getFileProblems(entrypointResolutions, hosts);
5050

5151
return {
5252
packageName,
@@ -84,7 +84,7 @@ function getProxyDirectories(rootDir: string, fs: FS) {
8484
.filter((f) => f !== "./");
8585
}
8686

87-
function getEntrypointInfo(packageName: string, fs: FS, host: MultiCompilerHost): Record<string, EntrypointInfo> {
87+
function getEntrypointInfo(packageName: string, fs: FS, hosts: CompilerHosts): Record<string, EntrypointInfo> {
8888
const packageJson = JSON.parse(fs.readFile(`/node_modules/${packageName}/package.json`));
8989
const subpaths = getSubpaths(packageJson.exports);
9090
const entrypoints = subpaths.length ? subpaths : ["."];
@@ -94,10 +94,10 @@ function getEntrypointInfo(packageName: string, fs: FS, host: MultiCompilerHost)
9494
const result: Record<string, EntrypointInfo> = {};
9595
for (const entrypoint of entrypoints) {
9696
const resolutions: Record<ResolutionKind, EntrypointResolutionAnalysis> = {
97-
node10: getEntrypointResolution(packageName, "node10", entrypoint, host),
98-
"node16-cjs": getEntrypointResolution(packageName, "node16-cjs", entrypoint, host),
99-
"node16-esm": getEntrypointResolution(packageName, "node16-esm", entrypoint, host),
100-
bundler: getEntrypointResolution(packageName, "bundler", entrypoint, host),
97+
node10: getEntrypointResolution(packageName, hosts.node10, "node10", entrypoint),
98+
"node16-cjs": getEntrypointResolution(packageName, hosts.node16, "node16-cjs", entrypoint),
99+
"node16-esm": getEntrypointResolution(packageName, hosts.node16, "node16-esm", entrypoint),
100+
bundler: getEntrypointResolution(packageName, hosts.bundler, "bundler", entrypoint),
101101
};
102102
result[entrypoint] = {
103103
subpath: entrypoint,
@@ -111,16 +111,15 @@ function getEntrypointInfo(packageName: string, fs: FS, host: MultiCompilerHost)
111111

112112
function getEntrypointResolution(
113113
packageName: string,
114+
host: CompilerHostWrapper,
114115
resolutionKind: ResolutionKind,
115-
entrypoint: string,
116-
host: MultiCompilerHost
116+
entrypoint: string
117117
): EntrypointResolutionAnalysis {
118118
if (entrypoint.includes("*")) {
119119
return { name: entrypoint, resolutionKind, isWildcard: true };
120120
}
121121
const moduleSpecifier = packageName + entrypoint.substring(1); // remove leading . before slash
122122
const importingFileName = resolutionKind === "node16-esm" ? "/index.mts" : "/index.ts";
123-
const moduleResolution = resolutionKind === "node10" ? "node10" : resolutionKind === "bundler" ? "bundler" : "node16";
124123
const resolutionMode = resolutionKind === "node16-esm" ? ts.ModuleKind.ESNext : ts.ModuleKind.CommonJS;
125124

126125
const resolution = tryResolve();
@@ -129,7 +128,7 @@ function getEntrypointResolution(
129128

130129
const files = resolution
131130
? host
132-
.createProgram(moduleResolution, [resolution.fileName])
131+
.createProgram([resolution.fileName])
133132
.getSourceFiles()
134133
.map((f) => f.fileName)
135134
: undefined;
@@ -146,7 +145,6 @@ function getEntrypointResolution(
146145
const { resolution, trace } = host.resolveModuleName(
147146
moduleSpecifier,
148147
importingFileName,
149-
moduleResolution,
150148
resolutionMode,
151149
noDtsResolution
152150
);
@@ -157,7 +155,7 @@ function getEntrypointResolution(
157155

158156
return {
159157
fileName,
160-
moduleKind: host.getModuleKindForFile(fileName, moduleResolution),
158+
moduleKind: host.getModuleKindForFile(fileName),
161159
isJson: resolution.resolvedModule.extension === ts.Extension.Json,
162160
isTypeScript: ts.hasTSFileExtension(resolution.resolvedModule.resolvedFileName),
163161
trace,

packages/core/src/checks/entrypointResolutionProblems.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import ts from "typescript";
22
import type { EntrypointInfo, EntrypointResolutionProblem } from "../types.js";
3-
import type { MultiCompilerHost } from "../multiCompilerHost.js";
3+
import type { CompilerHosts } from "../multiCompilerHost.js";
44
import { resolvedThroughFallback, visitResolutions } from "../utils.js";
55

66
export function getEntrypointResolutionProblems(
77
entrypointResolutions: Record<string, EntrypointInfo>,
8-
host: MultiCompilerHost
8+
hosts: CompilerHosts
99
): EntrypointResolutionProblem[] {
1010
const problems: EntrypointResolutionProblem[] = [];
1111
visitResolutions(entrypointResolutions, (result, entrypoint) => {
@@ -71,12 +71,12 @@ export function getEntrypointResolutionProblems(
7171
}
7272

7373
if (resolutionKind === "node16-esm" && resolution && implementationResolution) {
74-
const typesSourceFile = host.getSourceFile(resolution.fileName, "node16");
74+
const typesSourceFile = hosts.node16.getSourceFile(resolution.fileName);
7575
if (typesSourceFile) {
7676
ts.bindSourceFile(typesSourceFile, { target: ts.ScriptTarget.Latest, allowJs: true, checkJs: true });
7777
}
7878
const typesExports = typesSourceFile?.symbol?.exports;
79-
const jsSourceFile = typesExports && host.getSourceFile(implementationResolution.fileName, "node16");
79+
const jsSourceFile = typesExports && hosts.node16.getSourceFile(implementationResolution.fileName);
8080
if (jsSourceFile) {
8181
ts.bindSourceFile(jsSourceFile, { target: ts.ScriptTarget.Latest, allowJs: true, checkJs: true });
8282
}

packages/core/src/checks/fileProblems.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import ts from "typescript";
2-
import type { MultiCompilerHost } from "../multiCompilerHost.js";
2+
import type { CompilerHosts } from "../multiCompilerHost.js";
33
import type { EntrypointInfo, FileProblem } from "../types.js";
44
import { isDefined } from "../utils.js";
55

66
export function getFileProblems(
77
entrypointResolutions: Record<string, EntrypointInfo>,
8-
host: MultiCompilerHost
8+
hosts: CompilerHosts
99
): FileProblem[] {
1010
const problems: FileProblem[] = [];
1111
const visibleFiles = new Set(
@@ -18,7 +18,7 @@ export function getFileProblems(
1818

1919
for (const fileName of visibleFiles) {
2020
if (ts.hasJSFileExtension(fileName)) {
21-
const sourceFile = host.getSourceFile(fileName, "node16")!;
21+
const sourceFile = hosts.node16.getSourceFile(fileName)!;
2222
if (
2323
!sourceFile.externalModuleIndicator &&
2424
sourceFile.commonJsModuleIndicator &&

packages/core/src/checks/resolutionBasedFileProblems.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import ts from "typescript";
2-
import type { MultiCompilerHost } from "../multiCompilerHost.js";
2+
import type { CompilerHosts } from "../multiCompilerHost.js";
33
import type { EntrypointInfo, ResolutionBasedFileProblem } from "../types.js";
44
import { allResolutionOptions, getResolutionKinds } from "../utils.js";
55

66
export function getResolutionBasedFileProblems(
77
packageName: string,
88
entrypointResolutions: Record<string, EntrypointInfo>,
9-
host: MultiCompilerHost
9+
hosts: CompilerHosts
1010
): ResolutionBasedFileProblem[] {
1111
const result: ResolutionBasedFileProblem[] = [];
1212
for (const resolutionOption of allResolutionOptions) {
@@ -24,7 +24,8 @@ export function getResolutionBasedFileProblems(
2424
);
2525

2626
for (const fileName of visibleFiles) {
27-
const sourceFile = host.getSourceFile(fileName, resolutionOption)!;
27+
const host = hosts[resolutionOption];
28+
const sourceFile = host.getSourceFile(fileName)!;
2829

2930
if (sourceFile.imports) {
3031
for (const moduleSpecifier of sourceFile.imports) {
@@ -52,7 +53,7 @@ export function getResolutionBasedFileProblems(
5253
pos: moduleSpecifier.pos,
5354
end: moduleSpecifier.end,
5455
resolutionMode,
55-
trace: host.getTrace(resolutionOption, fileName, moduleSpecifier.text, resolutionMode)!,
56+
trace: host.getTrace(fileName, moduleSpecifier.text, resolutionMode)!,
5657
});
5758
}
5859
}
@@ -67,7 +68,7 @@ export function getResolutionBasedFileProblems(
6768
// for looking for a JS file.
6869
if (resolutionOption === "node16") {
6970
if (ts.hasJSFileExtension(fileName)) {
70-
const expectedModuleKind = host.getModuleKindForFile(fileName, resolutionOption);
71+
const expectedModuleKind = host.getModuleKindForFile(fileName);
7172
const syntaxImpliedModuleKind = sourceFile.externalModuleIndicator
7273
? ts.ModuleKind.ESNext
7374
: sourceFile.commonJsModuleIndicator

0 commit comments

Comments
 (0)