Cherry-pick PR #49814 into release-4.8 (#50578)

Component commits:
e2d22fa5bc Unify default import resolution across specifier target codepaths

b3416997bb Merge main into branch

cf010b517d Use differing type aliases, per request

Co-authored-by: Wesley Wigham <wewigham@microsoft.com>
This commit is contained in:
TypeScript Bot
2022-09-01 06:20:47 -07:00
committed by GitHub
parent 565a444d4b
commit 4b7a7b5ddf
15 changed files with 1347 additions and 61 deletions

View File

@@ -2798,44 +2798,66 @@ namespace ts {
function getTargetOfImportClause(node: ImportClause, dontResolveAlias: boolean): Symbol | undefined {
const moduleSymbol = resolveExternalModuleName(node, node.parent.moduleSpecifier);
if (moduleSymbol) {
let exportDefaultSymbol: Symbol | undefined;
if (isShorthandAmbientModuleSymbol(moduleSymbol)) {
exportDefaultSymbol = moduleSymbol;
return getTargetofModuleDefault(moduleSymbol, node, dontResolveAlias);
}
}
function getTargetofModuleDefault(moduleSymbol: Symbol, node: ImportClause | ImportOrExportSpecifier, dontResolveAlias: boolean) {
let exportDefaultSymbol: Symbol | undefined;
if (isShorthandAmbientModuleSymbol(moduleSymbol)) {
exportDefaultSymbol = moduleSymbol;
}
else {
exportDefaultSymbol = resolveExportByName(moduleSymbol, InternalSymbolName.Default, node, dontResolveAlias);
}
const file = moduleSymbol.declarations?.find(isSourceFile);
const specifier = getModuleSpecifierForImportOrExport(node);
if (!specifier) {
return exportDefaultSymbol;
}
const hasDefaultOnly = isOnlyImportedAsDefault(specifier);
const hasSyntheticDefault = canHaveSyntheticDefault(file, moduleSymbol, dontResolveAlias, specifier);
if (!exportDefaultSymbol && !hasSyntheticDefault && !hasDefaultOnly) {
if (hasExportAssignmentSymbol(moduleSymbol)) {
const compilerOptionName = moduleKind >= ModuleKind.ES2015 ? "allowSyntheticDefaultImports" : "esModuleInterop";
const exportEqualsSymbol = moduleSymbol.exports!.get(InternalSymbolName.ExportEquals);
const exportAssignment = exportEqualsSymbol!.valueDeclaration;
const err = error(node.name, Diagnostics.Module_0_can_only_be_default_imported_using_the_1_flag, symbolToString(moduleSymbol), compilerOptionName);
if (exportAssignment) {
addRelatedInfo(err, createDiagnosticForNode(
exportAssignment,
Diagnostics.This_module_is_declared_with_export_and_can_only_be_used_with_a_default_import_when_using_the_0_flag,
compilerOptionName
));
}
}
else if (isImportClause(node)) {
reportNonDefaultExport(moduleSymbol, node);
}
else {
exportDefaultSymbol = resolveExportByName(moduleSymbol, InternalSymbolName.Default, node, dontResolveAlias);
errorNoModuleMemberSymbol(moduleSymbol, moduleSymbol, node, isImportOrExportSpecifier(node) && node.propertyName || node.name);
}
}
else if (hasSyntheticDefault || hasDefaultOnly) {
// per emit behavior, a synthetic default overrides a "real" .default member if `__esModule` is not present
const resolved = resolveExternalModuleSymbol(moduleSymbol, dontResolveAlias) || resolveSymbol(moduleSymbol, dontResolveAlias);
markSymbolOfAliasDeclarationIfTypeOnly(node, moduleSymbol, resolved, /*overwriteTypeOnly*/ false);
return resolved;
}
markSymbolOfAliasDeclarationIfTypeOnly(node, exportDefaultSymbol, /*finalTarget*/ undefined, /*overwriteTypeOnly*/ false);
return exportDefaultSymbol;
}
const file = moduleSymbol.declarations?.find(isSourceFile);
const hasDefaultOnly = isOnlyImportedAsDefault(node.parent.moduleSpecifier);
const hasSyntheticDefault = canHaveSyntheticDefault(file, moduleSymbol, dontResolveAlias, node.parent.moduleSpecifier);
if (!exportDefaultSymbol && !hasSyntheticDefault && !hasDefaultOnly) {
if (hasExportAssignmentSymbol(moduleSymbol)) {
const compilerOptionName = moduleKind >= ModuleKind.ES2015 ? "allowSyntheticDefaultImports" : "esModuleInterop";
const exportEqualsSymbol = moduleSymbol.exports!.get(InternalSymbolName.ExportEquals);
const exportAssignment = exportEqualsSymbol!.valueDeclaration;
const err = error(node.name, Diagnostics.Module_0_can_only_be_default_imported_using_the_1_flag, symbolToString(moduleSymbol), compilerOptionName);
if (exportAssignment) {
addRelatedInfo(err, createDiagnosticForNode(
exportAssignment,
Diagnostics.This_module_is_declared_with_export_and_can_only_be_used_with_a_default_import_when_using_the_0_flag,
compilerOptionName
));
}
}
else {
reportNonDefaultExport(moduleSymbol, node);
}
}
else if (hasSyntheticDefault || hasDefaultOnly) {
// per emit behavior, a synthetic default overrides a "real" .default member if `__esModule` is not present
const resolved = resolveExternalModuleSymbol(moduleSymbol, dontResolveAlias) || resolveSymbol(moduleSymbol, dontResolveAlias);
markSymbolOfAliasDeclarationIfTypeOnly(node, moduleSymbol, resolved, /*overwriteTypeOnly*/ false);
return resolved;
}
markSymbolOfAliasDeclarationIfTypeOnly(node, exportDefaultSymbol, /*finalTarget*/ undefined, /*overwriteTypeOnly*/ false);
return exportDefaultSymbol;
function getModuleSpecifierForImportOrExport(node: ImportEqualsDeclaration | ImportClause | NamespaceImport | ImportOrExportSpecifier): Expression | undefined {
switch (node.kind) {
case SyntaxKind.ImportClause: return node.parent.moduleSpecifier;
case SyntaxKind.ImportEqualsDeclaration: return isExternalModuleReference(node.moduleReference) ? node.moduleReference.expression : undefined;
case SyntaxKind.NamespaceImport: return node.parent.parent.moduleSpecifier;
case SyntaxKind.ImportSpecifier: return node.parent.parent.parent.moduleSpecifier;
case SyntaxKind.ExportSpecifier: return node.parent.parent.moduleSpecifier;
default: return Debug.assertNever(node);
}
}
@@ -2969,38 +2991,42 @@ namespace ts {
combineValueAndTypeSymbols(symbolFromVariable, symbolFromModule) :
symbolFromModule || symbolFromVariable;
if (!symbol) {
const moduleName = getFullyQualifiedName(moduleSymbol, node);
const declarationName = declarationNameToString(name);
const suggestion = getSuggestedSymbolForNonexistentModule(name, targetSymbol);
if (suggestion !== undefined) {
const suggestionName = symbolToString(suggestion);
const diagnostic = error(name, Diagnostics._0_has_no_exported_member_named_1_Did_you_mean_2, moduleName, declarationName, suggestionName);
if (suggestion.valueDeclaration) {
addRelatedInfo(diagnostic,
createDiagnosticForNode(suggestion.valueDeclaration, Diagnostics._0_is_declared_here, suggestionName)
);
}
}
else {
if (moduleSymbol.exports?.has(InternalSymbolName.Default)) {
error(
name,
Diagnostics.Module_0_has_no_exported_member_1_Did_you_mean_to_use_import_1_from_0_instead,
moduleName,
declarationName
);
}
else {
reportNonExportedMember(node, name, declarationName, moduleSymbol, moduleName);
}
}
errorNoModuleMemberSymbol(moduleSymbol, targetSymbol, node, name);
}
return symbol;
}
}
}
function reportNonExportedMember(node: ImportDeclaration | ExportDeclaration | VariableDeclaration, name: Identifier, declarationName: string, moduleSymbol: Symbol, moduleName: string): void {
function errorNoModuleMemberSymbol(moduleSymbol: Symbol, targetSymbol: Symbol, node: Node, name: Identifier) {
const moduleName = getFullyQualifiedName(moduleSymbol, node);
const declarationName = declarationNameToString(name);
const suggestion = getSuggestedSymbolForNonexistentModule(name, targetSymbol);
if (suggestion !== undefined) {
const suggestionName = symbolToString(suggestion);
const diagnostic = error(name, Diagnostics._0_has_no_exported_member_named_1_Did_you_mean_2, moduleName, declarationName, suggestionName);
if (suggestion.valueDeclaration) {
addRelatedInfo(diagnostic,
createDiagnosticForNode(suggestion.valueDeclaration, Diagnostics._0_is_declared_here, suggestionName)
);
}
}
else {
if (moduleSymbol.exports?.has(InternalSymbolName.Default)) {
error(
name,
Diagnostics.Module_0_has_no_exported_member_1_Did_you_mean_to_use_import_1_from_0_instead,
moduleName,
declarationName
);
}
else {
reportNonExportedMember(node, name, declarationName, moduleSymbol, moduleName);
}
}
}
function reportNonExportedMember(node: Node, name: Identifier, declarationName: string, moduleSymbol: Symbol, moduleName: string): void {
const localSymbol = moduleSymbol.valueDeclaration?.locals?.get(name.escapedText);
const exports = moduleSymbol.exports;
if (localSymbol) {
@@ -3025,7 +3051,7 @@ namespace ts {
}
}
function reportInvalidImportEqualsExportMember(node: ImportDeclaration | ExportDeclaration | VariableDeclaration, name: Identifier, declarationName: string, moduleName: string) {
function reportInvalidImportEqualsExportMember(node: Node, name: Identifier, declarationName: string, moduleName: string) {
if (moduleKind >= ModuleKind.ES2015) {
const message = getESModuleInterop(compilerOptions) ? Diagnostics._0_can_only_be_imported_by_using_a_default_import :
Diagnostics._0_can_only_be_imported_by_turning_on_the_esModuleInterop_flag_and_using_a_default_import;
@@ -3046,6 +3072,13 @@ namespace ts {
}
function getTargetOfImportSpecifier(node: ImportSpecifier | BindingElement, dontResolveAlias: boolean): Symbol | undefined {
if (isImportSpecifier(node) && idText(node.propertyName || node.name) === InternalSymbolName.Default) {
const specifier = getModuleSpecifierForImportOrExport(node);
const moduleSymbol = specifier && resolveExternalModuleName(node, specifier);
if (moduleSymbol) {
return getTargetofModuleDefault(moduleSymbol, node, dontResolveAlias);
}
}
const root = isBindingElement(node) ? getRootDeclaration(node) as VariableDeclaration : node.parent.parent.parent;
const commonJSPropertyAccess = getCommonJSPropertyAccess(root);
const resolved = getExternalModuleMember(root, commonJSPropertyAccess || node, dontResolveAlias);
@@ -3070,6 +3103,13 @@ namespace ts {
}
function getTargetOfExportSpecifier(node: ExportSpecifier, meaning: SymbolFlags, dontResolveAlias?: boolean) {
if (idText(node.propertyName || node.name) === InternalSymbolName.Default) {
const specifier = getModuleSpecifierForImportOrExport(node);
const moduleSymbol = specifier && resolveExternalModuleName(node, specifier);
if (moduleSymbol) {
return getTargetofModuleDefault(moduleSymbol, node, !!dontResolveAlias);
}
}
const resolved = node.parent.parent.moduleSpecifier ?
getExternalModuleMember(node.parent.parent, node, dontResolveAlias) :
resolveEntityName(node.propertyName || node.name, meaning, /*ignoreErrors*/ false, dontResolveAlias);