@@ -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,42 @@ 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) {
13964- exportDefaultSymbol = moduleSymbol
13973+ // !!! exportDefaultSymbol = moduleSymbol
13974+ // Does nothing?
13975+ } else if file != nil && specifier != nil &&
13976+ core.ModuleKindNode20 <= c.moduleKind && c.moduleKind <= core.ModuleKindNodeNext &&
13977+ c.getEmitSyntaxForModuleSpecifierExpression(specifier) == core.ModuleKindCommonJS &&
13978+ c.program.GetImpliedNodeFormatForEmit(file.AsSourceFile()) == core.ModuleKindESNext {
13979+ exportModuleDotExportsSymbol = c.resolveExportByName(moduleSymbol, ast.InternalSymbolNameModuleExports, node, dontResolveAlias)
13980+ }
13981+ if exportModuleDotExportsSymbol != nil {
13982+ // We have a transpiled default import where the `require` resolves to an ES module with a `module.exports` named
13983+ // export. If `esModuleInterop` is enabled, this will work:
13984+ //
13985+ // const dep_1 = __importDefault(require("./dep.mjs")); // wraps like { default: require("./dep.mjs") }
13986+ // dep_1.default; // require("./dep.mjs") -> the `module.exports` export value
13987+ //
13988+ // But without `esModuleInterop`, it will be broken:
13989+ //
13990+ // const dep_1 = require("./dep.mjs"); // the `module.exports` export value (could be primitive)
13991+ // dep_1.default; // `default` property access on the `module.exports` export value
13992+ //
13993+ // We could try to resolve the 'default' property in the latter case, but it's a mistake to run in this
13994+ // environment without `esModuleInterop`, so just error.
13995+ if !c.compilerOptions.GetESModuleInterop() {
13996+ c.error(node.Name(), diagnostics.Module_0_can_only_be_default_imported_using_the_1_flag, c.symbolToString(moduleSymbol), "esModuleInterop")
13997+ return nil
13998+ }
13999+ c.markSymbolOfAliasDeclarationIfTypeOnly(node, exportModuleDotExportsSymbol /*finalTarget*/, nil /*overwriteEmpty*/, false, nil, "")
14000+ return exportModuleDotExportsSymbol
1396514001 } else {
1396614002 exportDefaultSymbol = c.resolveExportByName(moduleSymbol, ast.InternalSymbolNameDefault, node, dontResolveAlias)
1396714003 }
13968- file := core.Find(moduleSymbol.Declarations, ast.IsSourceFile)
13969- specifier := c.getModuleSpecifierForImportOrExport(node)
1397014004 if specifier == nil {
1397114005 return exportDefaultSymbol
1397214006 }
@@ -14948,7 +14982,11 @@ func (c *Checker) resolveESModuleSymbol(moduleSymbol *ast.Symbol, referencingLoc
1494814982 return symbol
1494914983 }
1495014984 referenceParent := referencingLocation.Parent
14951- if ast.IsImportDeclaration(referenceParent) && ast.GetNamespaceDeclarationNode(referenceParent) != nil || ast.IsImportCall(referenceParent) {
14985+ var namespaceImport *ast.Node
14986+ if ast.IsImportDeclaration(referenceParent) {
14987+ namespaceImport = ast.GetNamespaceDeclarationNode(referenceParent)
14988+ }
14989+ if namespaceImport != nil || ast.IsImportCall(referenceParent) {
1495214990 var reference *ast.Node
1495314991 if ast.IsImportCall(referenceParent) {
1495414992 reference = referenceParent.AsCallExpression().Arguments.Nodes[0]
@@ -14960,29 +14998,51 @@ func (c *Checker) resolveESModuleSymbol(moduleSymbol *ast.Symbol, referencingLoc
1496014998 if defaultOnlyType != nil {
1496114999 return c.cloneTypeAsModuleType(symbol, defaultOnlyType, referenceParent)
1496215000 }
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- // }
15001+
15002+ targetFile := core.Find(moduleSymbol.Declarations, ast.IsSourceFile)
15003+ usageMode := c.getEmitSyntaxForModuleSpecifierExpression(reference)
15004+ var exportModuleDotExportsSymbol *ast.Symbol
15005+ if namespaceImport != nil && targetFile != nil &&
15006+ core.ModuleKindNode20 <= c.moduleKind && c.moduleKind <= core.ModuleKindNodeNext &&
15007+ usageMode == core.ModuleKindCommonJS &&
15008+ c.program.GetImpliedNodeFormatForEmit(targetFile.AsSourceFile()) == core.ModuleKindESNext {
15009+ exportModuleDotExportsSymbol = c.getExportOfModule(symbol, ast.InternalSymbolNameModuleExports, namespaceImport, dontResolveAlias)
15010+ }
15011+ if exportModuleDotExportsSymbol != nil {
15012+ if !suppressInteropError && symbol.Flags&(ast.SymbolFlagsModule|ast.SymbolFlagsVariable) == 0 {
15013+ 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")
15014+ }
15015+ if c.compilerOptions.GetESModuleInterop() && c.hasSignatures(typ) {
15016+ return c.cloneTypeAsModuleType(exportModuleDotExportsSymbol, typ, referenceParent)
15017+ }
15018+ return exportModuleDotExportsSymbol
15019+ }
15020+
15021+ isEsmCjsRef := targetFile != nil && isESMFormatImportImportingCommonjsFormatFile(usageMode, c.program.GetImpliedNodeFormatForEmit(targetFile.AsSourceFile()))
15022+ if c.compilerOptions.GetESModuleInterop() || isEsmCjsRef {
15023+ if c.hasSignatures(typ) || c.getPropertyOfTypeEx(typ, ast.InternalSymbolNameDefault, true /*skipObjectFunctionPropertyAugment*/, false /*includeTypeOnlyMembers*/) != nil || isEsmCjsRef {
15024+ var moduleType *Type
15025+ if typ.Flags()&TypeFlagsStructuredType != 0 {
15026+ moduleType = c.getTypeWithSyntheticDefaultImportType(typ, symbol, moduleSymbol, reference)
15027+ } else {
15028+ moduleType = c.createDefaultPropertyWrapperForModule(symbol, symbol.Parent, nil)
15029+ }
15030+ return c.cloneTypeAsModuleType(symbol, moduleType, referenceParent)
15031+ }
15032+ }
1498115033 }
1498215034 }
1498315035 return symbol
1498415036}
1498515037
15038+ func (c *Checker) hasSignatures(t *Type) bool {
15039+ return len(c.getSignaturesOfStructuredType(t, SignatureKindCall)) > 0 || len(c.getSignaturesOfStructuredType(t, SignatureKindConstruct)) > 0
15040+ }
15041+
15042+ func isESMFormatImportImportingCommonjsFormatFile(usageMode core.ResolutionMode, targetMode core.ResolutionMode) bool {
15043+ return usageMode == core.ModuleKindESNext && targetMode == core.ModuleKindCommonJS
15044+ }
15045+
1498615046func (c *Checker) getTypeWithSyntheticDefaultOnly(t *Type, symbol *ast.Symbol, originalSymbol *ast.Symbol, moduleSpecifier *ast.Node) *Type {
1498715047 hasDefaultOnly := c.isOnlyImportableAsDefault(moduleSpecifier, nil)
1498815048 if hasDefaultOnly && t != nil && !c.isErrorType(t) {
0 commit comments