@@ -5153,7 +5153,7 @@ func (c *Checker) checkImportAttributes(declaration *ast.Node) {
5153
5153
return
5154
5154
}
5155
5155
5156
- if c.moduleKind = = core.ModuleKindNodeNext && !isImportAttributes {
5156
+ if core.ModuleKindNode20 <= c.moduleKind && c.moduleKind < = core.ModuleKindNodeNext && !isImportAttributes {
5157
5157
c.grammarErrorOnNode(node, diagnostics.Import_assertions_have_been_replaced_by_import_attributes_Use_with_instead_of_assert)
5158
5158
return
5159
5159
}
@@ -13850,6 +13850,12 @@ func (c *Checker) getTargetOfImportEqualsDeclaration(node *ast.Node, dontResolve
13850
13850
}
13851
13851
immediate := c.resolveExternalModuleName(node, moduleReference, false /*ignoreErrors*/)
13852
13852
resolved := c.resolveExternalModuleSymbol(immediate, false /*dontResolveAlias*/)
13853
+ if resolved != nil && core.ModuleKindNode20 <= c.moduleKind && c.moduleKind <= core.ModuleKindNodeNext {
13854
+ moduleExports := c.getExportOfModule(resolved, ast.InternalSymbolNameModuleExports, node, dontResolveAlias)
13855
+ if moduleExports != nil {
13856
+ return moduleExports
13857
+ }
13858
+ }
13853
13859
c.markSymbolOfAliasDeclarationIfTypeOnly(node, immediate, resolved, false /*overwriteEmpty*/, nil, "")
13854
13860
return resolved
13855
13861
}
@@ -13919,14 +13925,42 @@ func (c *Checker) getTargetOfImportClause(node *ast.Node, dontResolveAlias bool)
13919
13925
}
13920
13926
13921
13927
func (c *Checker) getTargetOfModuleDefault(moduleSymbol *ast.Symbol, node *ast.Node, dontResolveAlias bool) *ast.Symbol {
13928
+ file := core.Find(moduleSymbol.Declarations, ast.IsSourceFile)
13929
+ specifier := c.getModuleSpecifierForImportOrExport(node)
13922
13930
var exportDefaultSymbol *ast.Symbol
13931
+ var exportModuleDotExportsSymbol *ast.Symbol
13923
13932
if isShorthandAmbientModuleSymbol(moduleSymbol) {
13924
13933
exportDefaultSymbol = moduleSymbol
13925
- } else {
13934
+ } else if file != nil && specifier != nil &&
13935
+ core.ModuleKindNode20 <= c.moduleKind && c.moduleKind <= core.ModuleKindNodeNext &&
13936
+ c.getEmitSyntaxForModuleSpecifierExpression(specifier) == core.ModuleKindCommonJS &&
13937
+ c.program.GetImpliedNodeFormatForEmit(file.AsSourceFile()) == core.ModuleKindESNext {
13938
+ exportModuleDotExportsSymbol = c.resolveExportByName(moduleSymbol, "module.exports", node, dontResolveAlias)
13939
+ }
13940
+ if exportModuleDotExportsSymbol != nil {
13941
+ // We have a transpiled default import where the `require` resolves to an ES module with a `module.exports` named
13942
+ // export. If `esModuleInterop` is enabled, this will work:
13943
+ //
13944
+ // const dep_1 = __importDefault(require("./dep.mjs")); // wraps like { default: require("./dep.mjs") }
13945
+ // dep_1.default; // require("./dep.mjs") -> the `module.exports` export value
13946
+ //
13947
+ // But without `esModuleInterop`, it will be broken:
13948
+ //
13949
+ // const dep_1 = require("./dep.mjs"); // the `module.exports` export value (could be primitive)
13950
+ // dep_1.default; // `default` property access on the `module.exports` export value
13951
+ //
13952
+ // We could try to resolve the 'default' property in the latter case, but it's a mistake to run in this
13953
+ // environment without `esModuleInterop`, so just error.
13954
+ if !c.compilerOptions.GetESModuleInterop() {
13955
+ c.error(node.Name(), diagnostics.Module_0_can_only_be_default_imported_using_the_1_flag, c.symbolToString(moduleSymbol), "esModuleInterop")
13956
+ return nil
13957
+ }
13958
+ c.markSymbolOfAliasDeclarationIfTypeOnly(node, exportModuleDotExportsSymbol /*finalTarget*/, nil /*overwriteEmpty*/, false, nil, "")
13959
+ return exportModuleDotExportsSymbol
13960
+ }
13961
+ if !isShorthandAmbientModuleSymbol(moduleSymbol) {
13926
13962
exportDefaultSymbol = c.resolveExportByName(moduleSymbol, ast.InternalSymbolNameDefault, node, dontResolveAlias)
13927
13963
}
13928
- file := core.Find(moduleSymbol.Declarations, ast.IsSourceFile)
13929
- specifier := c.getModuleSpecifierForImportOrExport(node)
13930
13964
if specifier == nil {
13931
13965
return exportDefaultSymbol
13932
13966
}
@@ -14908,7 +14942,11 @@ func (c *Checker) resolveESModuleSymbol(moduleSymbol *ast.Symbol, referencingLoc
14908
14942
return symbol
14909
14943
}
14910
14944
referenceParent := referencingLocation.Parent
14911
- if ast.IsImportDeclaration(referenceParent) && ast.GetNamespaceDeclarationNode(referenceParent) != nil || ast.IsImportCall(referenceParent) {
14945
+ var namespaceImport *ast.Node
14946
+ if ast.IsImportDeclaration(referenceParent) {
14947
+ namespaceImport = ast.GetNamespaceDeclarationNode(referenceParent)
14948
+ }
14949
+ if namespaceImport != nil || ast.IsImportCall(referenceParent) {
14912
14950
var reference *ast.Node
14913
14951
if ast.IsImportCall(referenceParent) {
14914
14952
reference = referenceParent.AsCallExpression().Arguments.Nodes[0]
@@ -14920,29 +14958,47 @@ func (c *Checker) resolveESModuleSymbol(moduleSymbol *ast.Symbol, referencingLoc
14920
14958
if defaultOnlyType != nil {
14921
14959
return c.cloneTypeAsModuleType(symbol, defaultOnlyType, referenceParent)
14922
14960
}
14923
- // !!!
14924
- // targetFile := moduleSymbol. /* ? */ declarations. /* ? */ find(isSourceFile)
14925
- // isEsmCjsRef := targetFile && c.isESMFormatImportImportingCommonjsFormatFile(c.getEmitSyntaxForModuleSpecifierExpression(reference), host.getImpliedNodeFormatForEmit(targetFile))
14926
- // if c.compilerOptions.GetESModuleInterop() || isEsmCjsRef {
14927
- // sigs := c.getSignaturesOfStructuredType(type_, SignatureKindCall)
14928
- // if !sigs || !sigs.length {
14929
- // sigs = c.getSignaturesOfStructuredType(type_, SignatureKindConstruct)
14930
- // }
14931
- // if (sigs && sigs.length) || c.getPropertyOfType(type_, ast.InternalSymbolNameDefault /*skipObjectFunctionPropertyAugment*/, true) || isEsmCjsRef {
14932
- // var moduleType *Type
14933
- // if type_.flags & TypeFlagsStructuredType {
14934
- // moduleType = c.getTypeWithSyntheticDefaultImportType(type_, symbol, moduleSymbol, reference)
14935
- // } else {
14936
- // moduleType = c.createDefaultPropertyWrapperForModule(symbol, symbol.parent)
14937
- // }
14938
- // return c.cloneTypeAsModuleType(symbol, moduleType, referenceParent)
14939
- // }
14940
- // }
14961
+
14962
+ targetFile := core.Find(moduleSymbol.Declarations, ast.IsSourceFile)
14963
+ usageMode := c.getEmitSyntaxForModuleSpecifierExpression(reference)
14964
+ var exportModuleDotExportsSymbol *ast.Symbol
14965
+ if namespaceImport != nil && targetFile != nil &&
14966
+ core.ModuleKindNode20 <= c.moduleKind && c.moduleKind <= core.ModuleKindNodeNext &&
14967
+ usageMode == core.ModuleKindCommonJS &&
14968
+ c.program.GetImpliedNodeFormatForEmit(targetFile.AsSourceFile()) == core.ModuleKindESNext {
14969
+ exportModuleDotExportsSymbol = c.getExportOfModule(symbol, "module.exports", namespaceImport, dontResolveAlias)
14970
+ }
14971
+ if exportModuleDotExportsSymbol != nil {
14972
+ if !suppressInteropError && symbol.Flags&(ast.SymbolFlagsModule|ast.SymbolFlagsVariable) == 0 {
14973
+ 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")
14974
+ }
14975
+ if c.compilerOptions.GetESModuleInterop() && c.hasSignatures(typ) {
14976
+ return c.cloneTypeAsModuleType(exportModuleDotExportsSymbol, typ, referenceParent)
14977
+ }
14978
+ return exportModuleDotExportsSymbol
14979
+ }
14980
+
14981
+ isEsmCjsRef := targetFile != nil && usageMode == core.ModuleKindESNext && c.program.GetImpliedNodeFormatForEmit(targetFile.AsSourceFile()) == core.ModuleKindCommonJS
14982
+ if c.compilerOptions.GetESModuleInterop() || isEsmCjsRef {
14983
+ if c.hasSignatures(typ) || c.getPropertyOfTypeEx(typ, ast.InternalSymbolNameDefault, true /*skipObjectFunctionPropertyAugment*/, false /*includeTypeOnlyMembers*/) != nil || isEsmCjsRef {
14984
+ var moduleType *Type
14985
+ if typ.Flags()&TypeFlagsStructuredType != 0 {
14986
+ moduleType = c.getTypeWithSyntheticDefaultImportType(typ, symbol, moduleSymbol, reference)
14987
+ } else {
14988
+ moduleType = c.createDefaultPropertyWrapperForModule(symbol, moduleSymbol, nil)
14989
+ }
14990
+ return c.cloneTypeAsModuleType(symbol, moduleType, referenceParent)
14991
+ }
14992
+ }
14941
14993
}
14942
14994
}
14943
14995
return symbol
14944
14996
}
14945
14997
14998
+ func (c *Checker) hasSignatures(t *Type) bool {
14999
+ return len(c.getSignaturesOfStructuredType(t, SignatureKindCall)) > 0 || len(c.getSignaturesOfStructuredType(t, SignatureKindConstruct)) > 0
15000
+ }
15001
+
14946
15002
func (c *Checker) getTypeWithSyntheticDefaultOnly(t *Type, symbol *ast.Symbol, originalSymbol *ast.Symbol, moduleSpecifier *ast.Node) *Type {
14947
15003
hasDefaultOnly := c.isOnlyImportableAsDefault(moduleSpecifier, nil)
14948
15004
if hasDefaultOnly && t != nil && !c.isErrorType(t) {
0 commit comments