@@ -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,42 @@ 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
- 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
13965
14001
} else {
13966
14002
exportDefaultSymbol = c.resolveExportByName(moduleSymbol, ast.InternalSymbolNameDefault, node, dontResolveAlias)
13967
14003
}
13968
- file := core.Find(moduleSymbol.Declarations, ast.IsSourceFile)
13969
- specifier := c.getModuleSpecifierForImportOrExport(node)
13970
14004
if specifier == nil {
13971
14005
return exportDefaultSymbol
13972
14006
}
@@ -14948,7 +14982,11 @@ func (c *Checker) resolveESModuleSymbol(moduleSymbol *ast.Symbol, referencingLoc
14948
14982
return symbol
14949
14983
}
14950
14984
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) {
14952
14990
var reference *ast.Node
14953
14991
if ast.IsImportCall(referenceParent) {
14954
14992
reference = referenceParent.AsCallExpression().Arguments.Nodes[0]
@@ -14960,29 +14998,51 @@ func (c *Checker) resolveESModuleSymbol(moduleSymbol *ast.Symbol, referencingLoc
14960
14998
if defaultOnlyType != nil {
14961
14999
return c.cloneTypeAsModuleType(symbol, defaultOnlyType, referenceParent)
14962
15000
}
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
+ }
14981
15033
}
14982
15034
}
14983
15035
return symbol
14984
15036
}
14985
15037
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
+
14986
15046
func (c *Checker) getTypeWithSyntheticDefaultOnly(t *Type, symbol *ast.Symbol, originalSymbol *ast.Symbol, moduleSpecifier *ast.Node) *Type {
14987
15047
hasDefaultOnly := c.isOnlyImportableAsDefault(moduleSpecifier, nil)
14988
15048
if hasDefaultOnly && t != nil && !c.isErrorType(t) {
0 commit comments