Skip to content

Commit 6d043de

Browse files
committed
Port node20
1 parent 1f3304f commit 6d043de

File tree

5 files changed

+97
-28
lines changed

5 files changed

+97
-28
lines changed

internal/checker/checker.go

Lines changed: 83 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -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

1396113967
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)
1396213970
var exportDefaultSymbol *ast.Symbol
13971+
var exportModuleDotExportsSymbol *ast.Symbol
1396313972
if isShorthandAmbientModuleSymbol(moduleSymbol) {
1396413973
exportDefaultSymbol = moduleSymbol
13965-
} else {
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
14000+
}
14001+
if !isShorthandAmbientModuleSymbol(moduleSymbol) {
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+
1498615046
func (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) {

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))
@@ -1752,6 +1752,7 @@ func (c *Checker) checkGrammarAwaitOrAwaitUsing(node *ast.Node) bool {
17521752
switch c.moduleKind {
17531753
case core.ModuleKindNode16,
17541754
core.ModuleKindNode18,
1755+
core.ModuleKindNode20,
17551756
core.ModuleKindNodeNext:
17561757
sourceFileMetaData := c.program.GetSourceFileMetaData(sourceFile.Path())
17571758
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: 10 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,11 @@ func (options *CompilerOptions) GetResolveJsonModule() bool {
273275
if options.ResolveJsonModule != TSUnknown {
274276
return options.ResolveJsonModule == TSTrue
275277
}
278+
switch options.GetEmitModuleKind() {
279+
// TODO in 6.0: add Node16/Node18
280+
case ModuleKindNode20, ModuleKindESNext:
281+
return true
282+
}
276283
return options.GetModuleResolutionKind() == ModuleResolutionKindBundler
277284
}
278285

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)