Skip to content

Commit 539d849

Browse files
committed
Port node20
1 parent f2d955b commit 539d849

File tree

5 files changed

+92
-28
lines changed

5 files changed

+92
-28
lines changed

internal/checker/checker.go

Lines changed: 79 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5153,7 +5153,7 @@ func (c *Checker) checkImportAttributes(declaration *ast.Node) {
51535153
return
51545154
}
51555155

5156-
if c.moduleKind == core.ModuleKindNodeNext && !isImportAttributes {
5156+
if core.ModuleKindNode20 <= c.moduleKind && c.moduleKind <= core.ModuleKindNodeNext && !isImportAttributes {
51575157
c.grammarErrorOnNode(node, diagnostics.Import_assertions_have_been_replaced_by_import_attributes_Use_with_instead_of_assert)
51585158
return
51595159
}
@@ -13850,6 +13850,12 @@ func (c *Checker) getTargetOfImportEqualsDeclaration(node *ast.Node, dontResolve
1385013850
}
1385113851
immediate := c.resolveExternalModuleName(node, moduleReference, false /*ignoreErrors*/)
1385213852
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+
}
1385313859
c.markSymbolOfAliasDeclarationIfTypeOnly(node, immediate, resolved, false /*overwriteEmpty*/, nil, "")
1385413860
return resolved
1385513861
}
@@ -13919,14 +13925,42 @@ func (c *Checker) getTargetOfImportClause(node *ast.Node, dontResolveAlias bool)
1391913925
}
1392013926

1392113927
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)
1392213930
var exportDefaultSymbol *ast.Symbol
13931+
var exportModuleDotExportsSymbol *ast.Symbol
1392313932
if isShorthandAmbientModuleSymbol(moduleSymbol) {
1392413933
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) {
1392613962
exportDefaultSymbol = c.resolveExportByName(moduleSymbol, ast.InternalSymbolNameDefault, node, dontResolveAlias)
1392713963
}
13928-
file := core.Find(moduleSymbol.Declarations, ast.IsSourceFile)
13929-
specifier := c.getModuleSpecifierForImportOrExport(node)
1393013964
if specifier == nil {
1393113965
return exportDefaultSymbol
1393213966
}
@@ -14908,7 +14942,11 @@ func (c *Checker) resolveESModuleSymbol(moduleSymbol *ast.Symbol, referencingLoc
1490814942
return symbol
1490914943
}
1491014944
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) {
1491214950
var reference *ast.Node
1491314951
if ast.IsImportCall(referenceParent) {
1491414952
reference = referenceParent.AsCallExpression().Arguments.Nodes[0]
@@ -14920,29 +14958,47 @@ func (c *Checker) resolveESModuleSymbol(moduleSymbol *ast.Symbol, referencingLoc
1492014958
if defaultOnlyType != nil {
1492114959
return c.cloneTypeAsModuleType(symbol, defaultOnlyType, referenceParent)
1492214960
}
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+
}
1494114993
}
1494214994
}
1494314995
return symbol
1494414996
}
1494514997

14998+
func (c *Checker) hasSignatures(t *Type) bool {
14999+
return len(c.getSignaturesOfStructuredType(t, SignatureKindCall)) > 0 || len(c.getSignaturesOfStructuredType(t, SignatureKindConstruct)) > 0
15000+
}
15001+
1494615002
func (c *Checker) getTypeWithSyntheticDefaultOnly(t *Type, symbol *ast.Symbol, originalSymbol *ast.Symbol, moduleSpecifier *ast.Node) *Type {
1494715003
hasDefaultOnly := c.isOnlyImportableAsDefault(moduleSpecifier, nil)
1494815004
if hasDefaultOnly && t != nil && !c.isErrorType(t) {

internal/checker/grammarchecks.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1240,7 +1240,7 @@ func (c *Checker) checkGrammarForInOrForOfStatement(forInOrOfStatement *ast.ForI
12401240
c.diagnostics.Add(createDiagnosticForNode(forInOrOfStatement.AwaitModifier, diagnostics.X_for_await_loops_are_only_allowed_at_the_top_level_of_a_file_when_that_file_is_a_module_but_this_file_has_no_imports_or_exports_Consider_adding_an_empty_export_to_make_this_file_a_module))
12411241
}
12421242
switch c.moduleKind {
1243-
case core.ModuleKindNode16, core.ModuleKindNode18, core.ModuleKindNodeNext:
1243+
case core.ModuleKindNode16, core.ModuleKindNode18, core.ModuleKindNode20, core.ModuleKindNodeNext:
12441244
sourceFileMetaData := c.program.GetSourceFileMetaData(sourceFile.Path())
12451245
if sourceFileMetaData.ImpliedNodeFormat == core.ModuleKindCommonJS {
12461246
c.diagnostics.Add(createDiagnosticForNode(forInOrOfStatement.AwaitModifier, diagnostics.The_current_file_is_a_CommonJS_module_and_cannot_use_await_at_the_top_level))
@@ -1747,6 +1747,7 @@ func (c *Checker) checkGrammarAwaitOrAwaitUsing(node *ast.Node) bool {
17471747
switch c.moduleKind {
17481748
case core.ModuleKindNode16,
17491749
core.ModuleKindNode18,
1750+
core.ModuleKindNode20,
17501751
core.ModuleKindNodeNext:
17511752
sourceFileMetaData := c.program.GetSourceFileMetaData(sourceFile.Path())
17521753
if sourceFileMetaData.ImpliedNodeFormat == core.ModuleKindCommonJS {

internal/compiler/emitter.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ func getModuleTransformer(opts *transformers.TransformOptions) *transformers.Tra
6464
core.ModuleKindES2022,
6565
core.ModuleKindES2020,
6666
core.ModuleKindES2015,
67+
core.ModuleKindNode20,
6768
core.ModuleKindNode18,
6869
core.ModuleKindNode16,
6970
core.ModuleKindNodeNext,

internal/core/compileroptions.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ func (options *CompilerOptions) GetEmitScriptTarget() ScriptTarget {
190190
switch options.GetEmitModuleKind() {
191191
case ModuleKindNode16, ModuleKindNode18:
192192
return ScriptTargetES2022
193+
case ModuleKindNode20:
194+
return ScriptTargetES2023
193195
case ModuleKindNodeNext:
194196
return ScriptTargetESNext
195197
default:
@@ -212,7 +214,7 @@ func (options *CompilerOptions) GetModuleResolutionKind() ModuleResolutionKind {
212214
return options.ModuleResolution
213215
}
214216
switch options.GetEmitModuleKind() {
215-
case ModuleKindNode16, ModuleKindNode18:
217+
case ModuleKindNode16, ModuleKindNode18, ModuleKindNode20:
216218
return ModuleResolutionKindNode16
217219
case ModuleKindNodeNext:
218220
return ModuleResolutionKindNodeNext
@@ -226,7 +228,7 @@ func (options *CompilerOptions) GetEmitModuleDetectionKind() ModuleDetectionKind
226228
return options.ModuleDetection
227229
}
228230
switch options.GetEmitModuleKind() {
229-
case ModuleKindNode16, ModuleKindNodeNext:
231+
case ModuleKindNode16, ModuleKindNode20, ModuleKindNodeNext:
230232
return ModuleDetectionKindForce
231233
default:
232234
return ModuleDetectionKindAuto
@@ -254,7 +256,7 @@ func (options *CompilerOptions) GetESModuleInterop() bool {
254256
return options.ESModuleInterop == TSTrue
255257
}
256258
switch options.GetEmitModuleKind() {
257-
case ModuleKindNode16, ModuleKindNode18, ModuleKindNodeNext, ModuleKindPreserve:
259+
case ModuleKindNode16, ModuleKindNode18, ModuleKindNode20, ModuleKindNodeNext, ModuleKindPreserve:
258260
return true
259261
}
260262
return false
@@ -273,6 +275,10 @@ func (options *CompilerOptions) GetResolveJsonModule() bool {
273275
if options.ResolveJsonModule != TSUnknown {
274276
return options.ResolveJsonModule == TSTrue
275277
}
278+
switch options.GetEmitModuleKind() {
279+
case ModuleKindNode20, ModuleKindESNext:
280+
return true
281+
}
276282
return options.GetModuleResolutionKind() == ModuleResolutionKindBundler
277283
}
278284

internal/ls/autoimports.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1237,7 +1237,7 @@ func getUmdImportKind(importingFile *ast.SourceFile /* | FutureSourceFile */, pr
12371237
case core.ModuleKindES2015, core.ModuleKindES2020, core.ModuleKindES2022, core.ModuleKindESNext, core.ModuleKindNone, core.ModuleKindPreserve:
12381238
// Fall back to the `import * as ns` style import.
12391239
return ImportKindNamespace
1240-
case core.ModuleKindNode16, core.ModuleKindNode18, core.ModuleKindNodeNext:
1240+
case core.ModuleKindNode16, core.ModuleKindNode18, core.ModuleKindNode20, core.ModuleKindNodeNext:
12411241
if program.GetImpliedNodeFormatForEmit(importingFile) == core.ModuleKindESNext {
12421242
return ImportKindNamespace
12431243
}

0 commit comments

Comments
 (0)