@@ -5162,7 +5162,7 @@ func (c *Checker) checkImportAttributes(declaration *ast.Node) {
51625162 return
51635163 }
51645164
5165- if c.moduleKind = = core.ModuleKindNodeNext && !isImportAttributes {
5165+ if core.ModuleKindNode20 <= c.moduleKind && c.moduleKind < = core.ModuleKindNodeNext && !isImportAttributes {
51665166 c.grammarErrorOnNode(node, diagnostics.Import_assertions_have_been_replaced_by_import_attributes_Use_with_instead_of_assert)
51675167 return
51685168 }
@@ -13890,6 +13890,12 @@ func (c *Checker) getTargetOfImportEqualsDeclaration(node *ast.Node, dontResolve
1389013890 }
1389113891 immediate := c.resolveExternalModuleName(node, moduleReference, false /*ignoreErrors*/)
1389213892 resolved := c.resolveExternalModuleSymbol(immediate, false /*dontResolveAlias*/)
13893+ if resolved != nil && core.ModuleKindNode20 <= c.moduleKind && c.moduleKind <= core.ModuleKindNodeNext {
13894+ moduleExports := c.getExportOfModule(resolved, ast.InternalSymbolNameModuleExports, node, dontResolveAlias)
13895+ if moduleExports != nil {
13896+ return moduleExports
13897+ }
13898+ }
1389313899 c.markSymbolOfAliasDeclarationIfTypeOnly(node, immediate, resolved, false /*overwriteEmpty*/, nil, "")
1389413900 return resolved
1389513901 }
@@ -13959,14 +13965,41 @@ func (c *Checker) getTargetOfImportClause(node *ast.Node, dontResolveAlias bool)
1395913965}
1396013966
1396113967func (c *Checker) getTargetOfModuleDefault(moduleSymbol *ast.Symbol, node *ast.Node, dontResolveAlias bool) *ast.Symbol {
13968+ file := core.Find(moduleSymbol.Declarations, ast.IsSourceFile)
13969+ specifier := c.getModuleSpecifierForImportOrExport(node)
1396213970 var exportDefaultSymbol *ast.Symbol
13971+ var exportModuleDotExportsSymbol *ast.Symbol
1396313972 if isShorthandAmbientModuleSymbol(moduleSymbol) {
1396413973 exportDefaultSymbol = moduleSymbol
13974+ } else if file != nil && specifier != nil &&
13975+ core.ModuleKindNode20 <= c.moduleKind && c.moduleKind <= core.ModuleKindNodeNext &&
13976+ c.getEmitSyntaxForModuleSpecifierExpression(specifier) == core.ModuleKindCommonJS &&
13977+ c.program.GetImpliedNodeFormatForEmit(file.AsSourceFile()) == core.ModuleKindESNext {
13978+ exportModuleDotExportsSymbol = c.resolveExportByName(moduleSymbol, ast.InternalSymbolNameModuleExports, node, dontResolveAlias)
13979+ }
13980+ if exportModuleDotExportsSymbol != nil {
13981+ // We have a transpiled default import where the `require` resolves to an ES module with a `module.exports` named
13982+ // export. If `esModuleInterop` is enabled, this will work:
13983+ //
13984+ // const dep_1 = __importDefault(require("./dep.mjs")); // wraps like { default: require("./dep.mjs") }
13985+ // dep_1.default; // require("./dep.mjs") -> the `module.exports` export value
13986+ //
13987+ // But without `esModuleInterop`, it will be broken:
13988+ //
13989+ // const dep_1 = require("./dep.mjs"); // the `module.exports` export value (could be primitive)
13990+ // dep_1.default; // `default` property access on the `module.exports` export value
13991+ //
13992+ // We could try to resolve the 'default' property in the latter case, but it's a mistake to run in this
13993+ // environment without `esModuleInterop`, so just error.
13994+ if !c.compilerOptions.GetESModuleInterop() {
13995+ c.error(node.Name(), diagnostics.Module_0_can_only_be_default_imported_using_the_1_flag, c.symbolToString(moduleSymbol), "esModuleInterop")
13996+ return nil
13997+ }
13998+ c.markSymbolOfAliasDeclarationIfTypeOnly(node, exportModuleDotExportsSymbol /*finalTarget*/, nil /*overwriteEmpty*/, false, nil, "")
13999+ return exportModuleDotExportsSymbol
1396514000 } else {
1396614001 exportDefaultSymbol = c.resolveExportByName(moduleSymbol, ast.InternalSymbolNameDefault, node, dontResolveAlias)
1396714002 }
13968- file := core.Find(moduleSymbol.Declarations, ast.IsSourceFile)
13969- specifier := c.getModuleSpecifierForImportOrExport(node)
1397014003 if specifier == nil {
1397114004 return exportDefaultSymbol
1397214005 }
@@ -14948,7 +14981,11 @@ func (c *Checker) resolveESModuleSymbol(moduleSymbol *ast.Symbol, referencingLoc
1494814981 return symbol
1494914982 }
1495014983 referenceParent := referencingLocation.Parent
14951- if ast.IsImportDeclaration(referenceParent) && ast.GetNamespaceDeclarationNode(referenceParent) != nil || ast.IsImportCall(referenceParent) {
14984+ var namespaceImport *ast.Node
14985+ if ast.IsImportDeclaration(referenceParent) {
14986+ namespaceImport = ast.GetNamespaceDeclarationNode(referenceParent)
14987+ }
14988+ if namespaceImport != nil || ast.IsImportCall(referenceParent) {
1495214989 var reference *ast.Node
1495314990 if ast.IsImportCall(referenceParent) {
1495414991 reference = referenceParent.AsCallExpression().Arguments.Nodes[0]
@@ -14960,29 +14997,51 @@ func (c *Checker) resolveESModuleSymbol(moduleSymbol *ast.Symbol, referencingLoc
1496014997 if defaultOnlyType != nil {
1496114998 return c.cloneTypeAsModuleType(symbol, defaultOnlyType, referenceParent)
1496214999 }
14963- // !!!
14964- // targetFile := moduleSymbol. /* ? */ declarations. /* ? */ find(isSourceFile)
14965- // isEsmCjsRef := targetFile && c.isESMFormatImportImportingCommonjsFormatFile(c.getEmitSyntaxForModuleSpecifierExpression(reference), host.getImpliedNodeFormatForEmit(targetFile))
14966- // if c.compilerOptions.GetESModuleInterop() || isEsmCjsRef {
14967- // sigs := c.getSignaturesOfStructuredType(type_, SignatureKindCall)
14968- // if !sigs || !sigs.length {
14969- // sigs = c.getSignaturesOfStructuredType(type_, SignatureKindConstruct)
14970- // }
14971- // if (sigs && sigs.length) || c.getPropertyOfType(type_, ast.InternalSymbolNameDefault /*skipObjectFunctionPropertyAugment*/, true) || isEsmCjsRef {
14972- // var moduleType *Type
14973- // if type_.flags & TypeFlagsStructuredType {
14974- // moduleType = c.getTypeWithSyntheticDefaultImportType(type_, symbol, moduleSymbol, reference)
14975- // } else {
14976- // moduleType = c.createDefaultPropertyWrapperForModule(symbol, symbol.parent)
14977- // }
14978- // return c.cloneTypeAsModuleType(symbol, moduleType, referenceParent)
14979- // }
14980- // }
15000+
15001+ targetFile := core.Find(moduleSymbol.Declarations, ast.IsSourceFile)
15002+ usageMode := c.getEmitSyntaxForModuleSpecifierExpression(reference)
15003+ var exportModuleDotExportsSymbol *ast.Symbol
15004+ if namespaceImport != nil && targetFile != nil &&
15005+ core.ModuleKindNode20 <= c.moduleKind && c.moduleKind <= core.ModuleKindNodeNext &&
15006+ usageMode == core.ModuleKindCommonJS &&
15007+ c.program.GetImpliedNodeFormatForEmit(targetFile.AsSourceFile()) == core.ModuleKindESNext {
15008+ exportModuleDotExportsSymbol = c.getExportOfModule(symbol, ast.InternalSymbolNameModuleExports, namespaceImport, dontResolveAlias)
15009+ }
15010+ if exportModuleDotExportsSymbol != nil {
15011+ if !suppressInteropError && symbol.Flags&(ast.SymbolFlagsModule|ast.SymbolFlagsVariable) == 0 {
15012+ c.error(referencingLocation, diagnostics.This_module_can_only_be_referenced_with_ECMAScript_imports_Slashexports_by_turning_on_the_0_flag_and_referencing_its_default_export, "esModuleInterop")
15013+ }
15014+ if c.compilerOptions.GetESModuleInterop() && c.hasSignatures(typ) {
15015+ return c.cloneTypeAsModuleType(exportModuleDotExportsSymbol, typ, referenceParent)
15016+ }
15017+ return exportModuleDotExportsSymbol
15018+ }
15019+
15020+ isEsmCjsRef := targetFile != nil && isESMFormatImportImportingCommonjsFormatFile(usageMode, c.program.GetImpliedNodeFormatForEmit(targetFile.AsSourceFile()))
15021+ if c.compilerOptions.GetESModuleInterop() || isEsmCjsRef {
15022+ if c.hasSignatures(typ) || c.getPropertyOfTypeEx(typ, ast.InternalSymbolNameDefault, true /*skipObjectFunctionPropertyAugment*/, false /*includeTypeOnlyMembers*/) != nil || isEsmCjsRef {
15023+ var moduleType *Type
15024+ if typ.Flags()&TypeFlagsStructuredType != 0 {
15025+ moduleType = c.getTypeWithSyntheticDefaultImportType(typ, symbol, moduleSymbol, reference)
15026+ } else {
15027+ moduleType = c.createDefaultPropertyWrapperForModule(symbol, symbol.Parent, nil)
15028+ }
15029+ return c.cloneTypeAsModuleType(symbol, moduleType, referenceParent)
15030+ }
15031+ }
1498115032 }
1498215033 }
1498315034 return symbol
1498415035}
1498515036
15037+ func (c *Checker) hasSignatures(t *Type) bool {
15038+ return len(c.getSignaturesOfStructuredType(t, SignatureKindCall)) > 0 || len(c.getSignaturesOfStructuredType(t, SignatureKindConstruct)) > 0
15039+ }
15040+
15041+ func isESMFormatImportImportingCommonjsFormatFile(usageMode core.ResolutionMode, targetMode core.ResolutionMode) bool {
15042+ return usageMode == core.ModuleKindESNext && targetMode == core.ModuleKindCommonJS
15043+ }
15044+
1498615045func (c *Checker) getTypeWithSyntheticDefaultOnly(t *Type, symbol *ast.Symbol, originalSymbol *ast.Symbol, moduleSpecifier *ast.Node) *Type {
1498715046 hasDefaultOnly := c.isOnlyImportableAsDefault(moduleSpecifier, nil)
1498815047 if hasDefaultOnly && t != nil && !c.isErrorType(t) {
0 commit comments