moduleDetection: auto makes cjs files parse as modules, module: node sets moduleDetection: force (#49268)

This commit is contained in:
Wesley Wigham
2022-05-26 16:49:55 -07:00
committed by GitHub
parent 5b86612f68
commit 67673f324d
15 changed files with 75 additions and 7 deletions

View File

@@ -2834,12 +2834,14 @@ namespace ts {
}
function setCommonJsModuleIndicator(node: Node) {
if (file.externalModuleIndicator) {
if (file.externalModuleIndicator && file.externalModuleIndicator !== true) {
return false;
}
if (!file.commonJsModuleIndicator) {
file.commonJsModuleIndicator = node;
bindSourceFileAsExternalModule();
if (!file.externalModuleIndicator) {
bindSourceFileAsExternalModule();
}
}
return true;
}

View File

@@ -2747,7 +2747,7 @@ namespace ts {
return hasExportAssignmentSymbol(moduleSymbol);
}
// JS files have a synthetic default if they do not contain ES2015+ module syntax (export = is not valid in js) _and_ do not have an __esModule marker
return !file.externalModuleIndicator && !resolveExportByName(moduleSymbol, escapeLeadingUnderscores("__esModule"), /*sourceNode*/ undefined, dontResolveAlias);
return typeof file.externalModuleIndicator !== "object" && !resolveExportByName(moduleSymbol, escapeLeadingUnderscores("__esModule"), /*sourceNode*/ undefined, dontResolveAlias);
}
function getTargetOfImportClause(node: ImportClause, dontResolveAlias: boolean): Symbol | undefined {

View File

@@ -6302,8 +6302,9 @@ namespace ts {
*/
function isFileForcedToBeModuleByFormat(file: SourceFile): true | undefined {
// Excludes declaration files - they still require an explicit `export {}` or the like
// for back compat purposes.
return file.impliedNodeFormat === ModuleKind.ESNext && !file.isDeclarationFile ? true : undefined;
// for back compat purposes. The only non-declaration files _not_ forced to be a module are `.js` files
// that aren't esm-mode (meaning not in a `type: module` scope).
return (file.impliedNodeFormat === ModuleKind.ESNext || (fileExtensionIsOneOf(file.fileName, [Extension.Cjs, Extension.Cts]))) && !file.isDeclarationFile ? true : undefined;
}
export function getSetExternalModuleIndicator(options: CompilerOptions): (file: SourceFile) => void {
@@ -6312,7 +6313,7 @@ namespace ts {
case ModuleDetectionKind.Force:
// All non-declaration files are modules, declaration files still do the usual isFileProbablyExternalModule
return (file: SourceFile) => {
file.externalModuleIndicator = !file.isDeclarationFile || isFileProbablyExternalModule(file);
file.externalModuleIndicator = isFileProbablyExternalModule(file) || !file.isDeclarationFile || undefined;
};
case ModuleDetectionKind.Legacy:
// Files are modules if they have imports, exports, or import.meta
@@ -6372,7 +6373,8 @@ namespace ts {
}
export function getEmitModuleDetectionKind(options: CompilerOptions) {
return options.moduleDetection || ModuleDetectionKind.Auto;
return options.moduleDetection ||
(getEmitModuleKind(options) === ModuleKind.Node16 || getEmitModuleKind(options) === ModuleKind.NodeNext ? ModuleDetectionKind.Force : ModuleDetectionKind.Auto);
}
export function hasJsonModuleEmitEnabled(options: CompilerOptions) {