Skip to content

Commit 3f0dafe

Browse files
committed
Start type-checking work for incorrect export default
1 parent d2703b2 commit 3f0dafe

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

packages/core/src/checks/entrypointResolutionProblems.ts

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

66
export function getEntrypointResolutionProblems(
77
entrypointResolutions: Record<string, EntrypointInfo>,
@@ -11,6 +11,7 @@ export function getEntrypointResolutionProblems(
1111
visitResolutions(entrypointResolutions, (result, entrypoint) => {
1212
const { subpath } = entrypoint;
1313
const { resolutionKind } = result;
14+
const resolutionOption = getResolutionOption(resolutionKind);
1415
if (result.isWildcard) {
1516
problems.push({
1617
kind: "Wildcard",
@@ -70,13 +71,14 @@ export function getEntrypointResolutionProblems(
7071
});
7172
}
7273

73-
if (resolutionKind === "node16-esm" && resolution && implementationResolution) {
74-
const typesSourceFile = hosts.node16.getSourceFile(resolution.fileName);
74+
if (resolution && implementationResolution) {
75+
const host = hosts[resolutionOption];
76+
const typesSourceFile = host.getSourceFile(resolution.fileName);
7577
if (typesSourceFile) {
7678
ts.bindSourceFile(typesSourceFile, { target: ts.ScriptTarget.Latest, allowJs: true, checkJs: true });
7779
}
7880
const typesExports = typesSourceFile?.symbol?.exports;
79-
const jsSourceFile = typesExports && hosts.node16.getSourceFile(implementationResolution.fileName);
81+
const jsSourceFile = typesExports && host.getSourceFile(implementationResolution.fileName);
8082
if (jsSourceFile) {
8183
ts.bindSourceFile(jsSourceFile, { target: ts.ScriptTarget.Latest, allowJs: true, checkJs: true });
8284
}
@@ -88,12 +90,26 @@ export function getEntrypointResolutionProblems(
8890
jsExports.has(ts.InternalSymbolName.ExportEquals) &&
8991
!jsExports.has(ts.InternalSymbolName.Default)
9092
) {
91-
// Also need to check for `default` property on `jsModule["export="]`?
92-
problems.push({
93-
kind: "FalseExportDefault",
94-
entrypoint: subpath,
95-
resolutionKind,
96-
});
93+
const jsChecker = host
94+
.createProgram([implementationResolution.fileName], {
95+
allowJs: true,
96+
checkJs: true,
97+
noResolve: true,
98+
target: ts.ScriptTarget.Latest,
99+
})
100+
.getTypeChecker();
101+
// Check for `default` property on `jsModule["export="]`
102+
if (
103+
!jsChecker
104+
.getExportsAndPropertiesOfModule(jsChecker.resolveExternalModuleSymbol(jsSourceFile.symbol))
105+
.some((s) => s.name === "default")
106+
) {
107+
problems.push({
108+
kind: "FalseExportDefault",
109+
entrypoint: subpath,
110+
resolutionKind,
111+
});
112+
}
97113
}
98114
}
99115
}

packages/core/src/multiCompilerHost.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,10 @@ export class CompilerHostWrapper {
117117
return this.traceCache[fromFileName]?.[`${resolutionMode ?? 1}:${moduleSpecifier}`];
118118
}
119119

120-
createProgram(rootNames: string[]): ts.Program {
120+
createProgram(rootNames: string[], options = this.compilerOptions): ts.Program {
121121
return ts.createProgram({
122122
rootNames,
123-
options: this.compilerOptions,
123+
options,
124124
host: this.compilerHost,
125125
});
126126
}

0 commit comments

Comments
 (0)