Skip to content

Commit 28f8c43

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 db9517e commit 28f8c43

File tree

5 files changed

+122
-166
lines changed

5 files changed

+122
-166
lines changed

packages/core/src/checkPackage.ts

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { getEntrypointResolutionProblems } from "./checks/entrypointResolutionPr
33
import { getFileProblems } from "./checks/fileProblems.js";
44
import { getResolutionBasedFileProblems } from "./checks/resolutionBasedFileProblems.js";
55
import type { Package } from "./createPackage.js";
6-
import { createMultiCompilerHost, type MultiCompilerHost } from "./multiCompilerHost.js";
6+
import { createCompilerHosts, type CompilerHosts, CompilerHostWrapper } from "./multiCompilerHost.js";
77
import type { CheckResult, EntrypointInfo, EntrypointResolutionAnalysis, Resolution, ResolutionKind } from "./types.js";
88

99
export async function checkPackage(pkg: Package): Promise<CheckResult> {
@@ -20,11 +20,11 @@ export async function checkPackage(pkg: Package): Promise<CheckResult> {
2020
return { packageName, packageVersion, types };
2121
}
2222

23-
const host = createMultiCompilerHost(pkg);
24-
const entrypointResolutions = getEntrypointInfo(packageName, pkg, host);
25-
const entrypointResolutionProblems = getEntrypointResolutionProblems(entrypointResolutions, host);
26-
const resolutionBasedFileProblems = getResolutionBasedFileProblems(packageName, entrypointResolutions, host);
27-
const fileProblems = getFileProblems(entrypointResolutions, host);
23+
const hosts = createCompilerHosts(pkg);
24+
const entrypointResolutions = getEntrypointInfo(packageName, pkg, hosts);
25+
const entrypointResolutionProblems = getEntrypointResolutionProblems(entrypointResolutions, hosts);
26+
const resolutionBasedFileProblems = getResolutionBasedFileProblems(packageName, entrypointResolutions, hosts);
27+
const fileProblems = getFileProblems(entrypointResolutions, hosts);
2828

2929
return {
3030
packageName,
@@ -62,7 +62,7 @@ function getProxyDirectories(rootDir: string, fs: Package) {
6262
.filter((f) => f !== "./");
6363
}
6464

65-
function getEntrypointInfo(packageName: string, fs: Package, host: MultiCompilerHost): Record<string, EntrypointInfo> {
65+
function getEntrypointInfo(packageName: string, fs: Package, hosts: CompilerHosts): Record<string, EntrypointInfo> {
6666
const packageJson = JSON.parse(fs.readFile(`/node_modules/${packageName}/package.json`));
6767
const subpaths = getSubpaths(packageJson.exports);
6868
const entrypoints = subpaths.length ? subpaths : ["."];
@@ -72,10 +72,10 @@ function getEntrypointInfo(packageName: string, fs: Package, host: MultiCompiler
7272
const result: Record<string, EntrypointInfo> = {};
7373
for (const entrypoint of entrypoints) {
7474
const resolutions: Record<ResolutionKind, EntrypointResolutionAnalysis> = {
75-
node10: getEntrypointResolution(packageName, "node10", entrypoint, host),
76-
"node16-cjs": getEntrypointResolution(packageName, "node16-cjs", entrypoint, host),
77-
"node16-esm": getEntrypointResolution(packageName, "node16-esm", entrypoint, host),
78-
bundler: getEntrypointResolution(packageName, "bundler", entrypoint, host),
75+
node10: getEntrypointResolution(packageName, hosts.node10, "node10", entrypoint),
76+
"node16-cjs": getEntrypointResolution(packageName, hosts.node16, "node16-cjs", entrypoint),
77+
"node16-esm": getEntrypointResolution(packageName, hosts.node16, "node16-esm", entrypoint),
78+
bundler: getEntrypointResolution(packageName, hosts.bundler, "bundler", entrypoint),
7979
};
8080
result[entrypoint] = {
8181
subpath: entrypoint,
@@ -89,16 +89,15 @@ function getEntrypointInfo(packageName: string, fs: Package, host: MultiCompiler
8989

9090
function getEntrypointResolution(
9191
packageName: string,
92+
host: CompilerHostWrapper,
9293
resolutionKind: ResolutionKind,
93-
entrypoint: string,
94-
host: MultiCompilerHost
94+
entrypoint: string
9595
): EntrypointResolutionAnalysis {
9696
if (entrypoint.includes("*")) {
9797
return { name: entrypoint, resolutionKind, isWildcard: true };
9898
}
9999
const moduleSpecifier = packageName + entrypoint.substring(1); // remove leading . before slash
100100
const importingFileName = resolutionKind === "node16-esm" ? "/index.mts" : "/index.ts";
101-
const moduleResolution = resolutionKind === "node10" ? "node10" : resolutionKind === "bundler" ? "bundler" : "node16";
102101
const resolutionMode = resolutionKind === "node16-esm" ? ts.ModuleKind.ESNext : ts.ModuleKind.CommonJS;
103102

104103
const resolution = tryResolve();
@@ -107,7 +106,7 @@ function getEntrypointResolution(
107106

108107
const files = resolution
109108
? host
110-
.createProgram(moduleResolution, [resolution.fileName])
109+
.createProgram([resolution.fileName])
111110
.getSourceFiles()
112111
.map((f) => f.fileName)
113112
: undefined;
@@ -124,7 +123,6 @@ function getEntrypointResolution(
124123
const { resolution, trace } = host.resolveModuleName(
125124
moduleSpecifier,
126125
importingFileName,
127-
moduleResolution,
128126
resolutionMode,
129127
noDtsResolution
130128
);
@@ -135,7 +133,7 @@ function getEntrypointResolution(
135133

136134
return {
137135
fileName,
138-
moduleKind: host.getModuleKindForFile(fileName, moduleResolution),
136+
moduleKind: host.getModuleKindForFile(fileName),
139137
isJson: resolution.resolvedModule.extension === ts.Extension.Json,
140138
isTypeScript: ts.hasTSFileExtension(resolution.resolvedModule.resolvedFileName),
141139
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)