@@ -5162,7 +5162,7 @@ func (c *Checker) checkImportAttributes(declaration *ast.Node) {
5162
5162
return
5163
5163
}
5164
5164
5165
- if c.moduleKind = = core.ModuleKindNodeNext && !isImportAttributes {
5165
+ if core.ModuleKindNode20 <= c.moduleKind && c.moduleKind < = core.ModuleKindNodeNext && !isImportAttributes {
5166
5166
c.grammarErrorOnNode(node, diagnostics.Import_assertions_have_been_replaced_by_import_attributes_Use_with_instead_of_assert)
5167
5167
return
5168
5168
}
@@ -13890,6 +13890,12 @@ func (c *Checker) getTargetOfImportEqualsDeclaration(node *ast.Node, dontResolve
13890
13890
}
13891
13891
immediate := c.resolveExternalModuleName(node, moduleReference, false /*ignoreErrors*/)
13892
13892
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
+ }
13893
13899
c.markSymbolOfAliasDeclarationIfTypeOnly(node, immediate, resolved, false /*overwriteEmpty*/, nil, "")
13894
13900
return resolved
13895
13901
}
@@ -13959,14 +13965,41 @@ func (c *Checker) getTargetOfImportClause(node *ast.Node, dontResolveAlias bool)
13959
13965
}
13960
13966
13961
13967
func (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)
13962
13970
var exportDefaultSymbol *ast.Symbol
13971
+ var exportModuleDotExportsSymbol *ast.Symbol
13963
13972
if isShorthandAmbientModuleSymbol(moduleSymbol) {
13964
13973
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
13965
14000
} else {
13966
14001
exportDefaultSymbol = c.resolveExportByName(moduleSymbol, ast.InternalSymbolNameDefault, node, dontResolveAlias)
13967
14002
}
13968
- file := core.Find(moduleSymbol.Declarations, ast.IsSourceFile)
13969
- specifier := c.getModuleSpecifierForImportOrExport(node)
13970
14003
if specifier == nil {
13971
14004
return exportDefaultSymbol
13972
14005
}
@@ -14948,7 +14981,11 @@ func (c *Checker) resolveESModuleSymbol(moduleSymbol *ast.Symbol, referencingLoc
14948
14981
return symbol
14949
14982
}
14950
14983
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) {
14952
14989
var reference *ast.Node
14953
14990
if ast.IsImportCall(referenceParent) {
14954
14991
reference = referenceParent.AsCallExpression().Arguments.Nodes[0]
@@ -14960,29 +14997,51 @@ func (c *Checker) resolveESModuleSymbol(moduleSymbol *ast.Symbol, referencingLoc
14960
14997
if defaultOnlyType != nil {
14961
14998
return c.cloneTypeAsModuleType(symbol, defaultOnlyType, referenceParent)
14962
14999
}
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
+ }
14981
15032
}
14982
15033
}
14983
15034
return symbol
14984
15035
}
14985
15036
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
+
14986
15045
func (c *Checker) getTypeWithSyntheticDefaultOnly(t *Type, symbol *ast.Symbol, originalSymbol *ast.Symbol, moduleSpecifier *ast.Node) *Type {
14987
15046
hasDefaultOnly := c.isOnlyImportableAsDefault(moduleSpecifier, nil)
14988
15047
if hasDefaultOnly && t != nil && !c.isErrorType(t) {
0 commit comments