Skip to content

Commit 5a2ac97

Browse files
Copilotandrewbranch
andcommitted
Use getEmitModuleFormatOfFile for project reference declaration files
Co-authored-by: andrewbranch <[email protected]>
1 parent d392ebd commit 5a2ac97

File tree

1 file changed

+8
-18
lines changed

1 file changed

+8
-18
lines changed

src/compiler/checker.ts

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3794,6 +3794,14 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
37943794
// are ESM, there cannot be a synthetic default.
37953795
return false;
37963796
}
3797+
// For declaration files from project references, check if the referenced project's
3798+
// emit format is ESM, in which case there cannot be a synthetic default
3799+
if (file.isDeclarationFile && host.getRedirectFromSourceFile(file.path)) {
3800+
const targetModuleKind = host.getEmitModuleFormatOfFile(file);
3801+
if (usageMode === ModuleKind.ESNext && targetModuleKind >= ModuleKind.ES2015) {
3802+
return false;
3803+
}
3804+
}
37973805
}
37983806
if (!allowSyntheticDefaultImports) {
37993807
return false;
@@ -3812,24 +3820,6 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
38123820
// it definitely is a module and does not have a synthetic default
38133821
return false;
38143822
}
3815-
// If this is a declaration file from a project reference, check the referenced project's options
3816-
// to determine if the module format is ESM
3817-
if (file) {
3818-
const redirect = host.getRedirectFromSourceFile(file.path);
3819-
if (redirect?.resolvedRef) {
3820-
const referencedOptions = redirect.resolvedRef.commandLine.options;
3821-
// If the referenced project has allowSyntheticDefaultImports disabled, respect that
3822-
if (!getAllowSyntheticDefaultImports(referencedOptions)) {
3823-
return false;
3824-
}
3825-
// If the referenced project's module format is ESM (ES2015 or later),
3826-
// it cannot have a synthetic default
3827-
const referencedModuleKind = getEmitModuleKind(referencedOptions);
3828-
if (referencedModuleKind >= ModuleKind.ES2015) {
3829-
return false;
3830-
}
3831-
}
3832-
}
38333823
// There are _many_ declaration files not written with esmodules in mind that still get compiled into a format with __esModule set
38343824
// Meaning there may be no default at runtime - however to be on the permissive side, we allow access to a synthetic default member
38353825
// as there is no marker to indicate if the accompanying JS has `__esModule` or not, or is even native esm

0 commit comments

Comments
 (0)