Skip to content

Commit b0278c5

Browse files
authored
Port node18 and nodenext changes (#1070)
1 parent 9bc5e3f commit b0278c5

File tree

380 files changed

+3768
-10554
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

380 files changed

+3768
-10554
lines changed

internal/checker/checker.go

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,7 @@ type Program interface {
528528
FileExists(fileName string) bool
529529
GetSourceFile(fileName string) *ast.SourceFile
530530
GetEmitModuleFormatOfFile(sourceFile ast.HasFileName) core.ModuleKind
531+
GetEmitSyntaxForUsageLocation(sourceFile ast.HasFileName, usageLocation *ast.StringLiteralLike) core.ResolutionMode
531532
GetImpliedNodeFormatForEmit(sourceFile ast.HasFileName) core.ModuleKind
532533
GetResolvedModule(currentSourceFile ast.HasFileName, moduleReference string, mode core.ResolutionMode) *module.ResolvedModule
533534
GetResolvedModules() map[tspath.Path]module.ModeAwareCache[*module.ResolvedModule]
@@ -5016,7 +5017,10 @@ func (c *Checker) checkImportDeclaration(node *ast.Node) {
50165017
}
50175018
}
50185019
}
5019-
if c.isOnlyImportableAsDefault(moduleSpecifier, resolvedModule) && !hasTypeJsonImportAttribute(node) {
5020+
if !importClause.IsTypeOnly() &&
5021+
core.ModuleKindNode18 <= c.moduleKind && c.moduleKind <= core.ModuleKindNodeNext &&
5022+
c.isOnlyImportableAsDefault(moduleSpecifier, resolvedModule) &&
5023+
!hasTypeJsonImportAttribute(node) {
50205024
c.error(moduleSpecifier, diagnostics.Importing_a_JSON_file_into_an_ECMAScript_module_requires_a_type_Colon_json_import_attribute_when_module_is_set_to_0, c.moduleKind.String())
50215025
}
50225026
} else if c.compilerOptions.NoUncheckedSideEffectImports.IsTrue() && importClause == nil {
@@ -5113,29 +5117,32 @@ func (c *Checker) checkImportAttributes(declaration *ast.Node) {
51135117
if isTypeOnly && override != core.ResolutionModeNone {
51145118
return // Other grammar checks do not apply to type-only imports with resolution mode assertions
51155119
}
5116-
var mode core.ResolutionMode
5117-
if c.moduleKind == core.ModuleKindNodeNext {
5118-
if moduleSpecifier := getModuleSpecifierFromNode(declaration); moduleSpecifier != nil {
5119-
mode = c.getEmitSyntaxForModuleSpecifierExpression(moduleSpecifier)
5120+
5121+
if !c.moduleKind.SupportsImportAttributes() {
5122+
if isImportAttributes {
5123+
c.grammarErrorOnNode(node, diagnostics.Import_attributes_are_only_supported_when_the_module_option_is_set_to_esnext_node18_nodenext_or_preserve)
5124+
} else {
5125+
c.grammarErrorOnNode(node, diagnostics.Import_assertions_are_only_supported_when_the_module_option_is_set_to_esnext_node18_nodenext_or_preserve)
51205126
}
5127+
return
51215128
}
5122-
if mode != core.ModuleKindESNext && c.moduleKind != core.ModuleKindESNext && c.moduleKind != core.ModuleKindPreserve {
5123-
var message *diagnostics.Message
5124-
switch {
5125-
case isImportAttributes:
5126-
if c.moduleKind == core.ModuleKindNodeNext {
5127-
message = diagnostics.Import_attributes_are_not_allowed_on_statements_that_compile_to_CommonJS_require_calls
5129+
5130+
if c.moduleKind == core.ModuleKindNodeNext && !isImportAttributes {
5131+
c.grammarErrorOnNode(node, diagnostics.Import_assertions_have_been_replaced_by_import_attributes_Use_with_instead_of_assert)
5132+
return
5133+
}
5134+
5135+
if moduleSpecifier := getModuleSpecifierFromNode(declaration); moduleSpecifier != nil {
5136+
if c.getEmitSyntaxForModuleSpecifierExpression(moduleSpecifier) == core.ModuleKindCommonJS {
5137+
if isImportAttributes {
5138+
c.grammarErrorOnNode(node, diagnostics.Import_attributes_are_not_allowed_on_statements_that_compile_to_CommonJS_require_calls)
51285139
} else {
5129-
message = diagnostics.Import_attributes_are_only_supported_when_the_module_option_is_set_to_esnext_node18_nodenext_or_preserve
5140+
c.grammarErrorOnNode(node, diagnostics.Import_assertions_are_not_allowed_on_statements_that_compile_to_CommonJS_require_calls)
51305141
}
5131-
case c.moduleKind == core.ModuleKindNodeNext:
5132-
message = diagnostics.Import_assertions_are_not_allowed_on_statements_that_compile_to_CommonJS_require_calls
5133-
default:
5134-
message = diagnostics.Import_assertions_are_only_supported_when_the_module_option_is_set_to_esnext_node18_nodenext_or_preserve
5142+
return
51355143
}
5136-
c.grammarErrorOnNode(node, message)
5137-
return
51385144
}
5145+
51395146
if isTypeOnly {
51405147
c.grammarErrorOnNode(node, core.IfElse(isImportAttributes,
51415148
diagnostics.Import_attributes_cannot_be_used_with_type_only_imports_or_exports,
@@ -10210,7 +10217,7 @@ func (c *Checker) checkNewTargetMetaProperty(node *ast.Node) *Type {
1021010217
}
1021110218

1021210219
func (c *Checker) checkImportMetaProperty(node *ast.Node) *Type {
10213-
if c.moduleKind == core.ModuleKindNode16 || c.moduleKind == core.ModuleKindNodeNext {
10220+
if core.ModuleKindNode16 <= c.moduleKind && c.moduleKind <= core.ModuleKindNodeNext {
1021410221
sourceFileMetaData := c.program.GetSourceFileMetaData(ast.GetSourceFileOfNode(node).Path())
1021510222
if sourceFileMetaData == nil || sourceFileMetaData.ImpliedNodeFormat != core.ModuleKindESNext {
1021610223
c.error(node, diagnostics.The_import_meta_meta_property_is_not_allowed_in_files_which_will_build_into_CommonJS_output)
@@ -14092,10 +14099,9 @@ func (c *Checker) canHaveSyntheticDefault(file *ast.Node, moduleSymbol *ast.Symb
1409214099
}
1409314100

1409414101
func (c *Checker) getEmitSyntaxForModuleSpecifierExpression(usage *ast.Node) core.ResolutionMode {
14095-
// !!!
14096-
// if isStringLiteralLike(usage) {
14097-
// return host.getEmitSyntaxForUsageLocation(ast.GetSourceFileOfNode(usage), usage)
14098-
// }
14102+
if ast.IsStringLiteralLike(usage) {
14103+
return c.program.GetEmitSyntaxForUsageLocation(ast.GetSourceFileOfNode(usage), usage)
14104+
}
1409914105
return core.ModuleKindNone
1410014106
}
1410114107

@@ -14446,7 +14452,7 @@ func (c *Checker) resolveExternalModule(location *ast.Node, moduleReference stri
1444614452
if resolvedModule.IsExternalLibraryImport && !(tspath.ExtensionIsTs(resolvedModule.Extension) || resolvedModule.Extension == tspath.ExtensionJson) {
1444714453
c.errorOnImplicitAnyModule(false /*isError*/, errorNode, mode, resolvedModule, moduleReference)
1444814454
}
14449-
if c.moduleResolutionKind == core.ModuleResolutionKindNode16 || c.moduleResolutionKind == core.ModuleResolutionKindNodeNext {
14455+
if c.moduleKind == core.ModuleKindNode16 || c.moduleKind == core.ModuleKindNode18 {
1445014456
isSyncImport := c.program.GetDefaultResolutionModeForFile(importingSourceFile) == core.ModuleKindCommonJS && ast.FindAncestor(location, ast.IsImportCall) == nil ||
1445114457
ast.FindAncestor(location, ast.IsImportEqualsDeclaration) != nil
1445214458
overrideHost := ast.FindAncestor(location, ast.IsResolutionModeOverrideHost)

internal/checker/grammarchecks.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1211,7 +1211,7 @@ func (c *Checker) checkGrammarForInOrForOfStatement(forInOrOfStatement *ast.ForI
12111211
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))
12121212
}
12131213
switch c.moduleKind {
1214-
case core.ModuleKindNode16, core.ModuleKindNodeNext:
1214+
case core.ModuleKindNode16, core.ModuleKindNode18, core.ModuleKindNodeNext:
12151215
sourceFileMetaData := c.program.GetSourceFileMetaData(sourceFile.Path())
12161216
if sourceFileMetaData != nil && sourceFileMetaData.ImpliedNodeFormat == core.ModuleKindCommonJS {
12171217
c.diagnostics.Add(createDiagnosticForNode(forInOrOfStatement.AwaitModifier, diagnostics.The_current_file_is_a_CommonJS_module_and_cannot_use_await_at_the_top_level))
@@ -1724,6 +1724,7 @@ func (c *Checker) checkGrammarAwaitOrAwaitUsing(node *ast.Node) bool {
17241724
}
17251725
switch c.moduleKind {
17261726
case core.ModuleKindNode16,
1727+
core.ModuleKindNode18,
17271728
core.ModuleKindNodeNext:
17281729
sourceFileMetaData := c.program.GetSourceFileMetaData(sourceFile.Path())
17291730
if sourceFileMetaData != nil && sourceFileMetaData.ImpliedNodeFormat == core.ModuleKindCommonJS {

internal/compiler/fileloader.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -512,17 +512,12 @@ func importSyntaxAffectsModuleResolution(options *core.CompilerOptions) bool {
512512
}
513513

514514
func getEmitSyntaxForUsageLocationWorker(fileName string, meta *ast.SourceFileMetaData, usage *ast.Node, options *core.CompilerOptions) core.ResolutionMode {
515-
if options == nil {
516-
// This should always be provided, but we try to fail somewhat
517-
// gracefully to allow projects like ts-node time to update.
518-
return core.ResolutionModeNone
519-
}
520-
521515
if ast.IsRequireCall(usage.Parent) || ast.IsExternalModuleReference(usage.Parent) && ast.IsImportEqualsDeclaration(usage.Parent.Parent) {
522516
return core.ModuleKindCommonJS
523517
}
518+
fileEmitMode := ast.GetEmitModuleFormatOfFileWorker(fileName, options, meta)
524519
if ast.IsImportCall(ast.WalkUpParenthesizedExpressions(usage.Parent)) {
525-
if ast.ShouldTransformImportCall(fileName, options, ast.GetImpliedNodeFormatForEmitWorker(fileName, options, meta)) {
520+
if ast.ShouldTransformImportCall(fileName, options, fileEmitMode) {
526521
return core.ModuleKindCommonJS
527522
} else {
528523
return core.ModuleKindESNext
@@ -535,7 +530,6 @@ func getEmitSyntaxForUsageLocationWorker(fileName string, meta *ast.SourceFileMe
535530
// file, until/unless declaration emit can indicate a true ESM import. On the
536531
// other hand, writing CJS syntax in a definitely-ESM file is fine, since declaration
537532
// emit preserves the CJS syntax.
538-
fileEmitMode := ast.GetEmitModuleFormatOfFileWorker(fileName, options, meta)
539533
if fileEmitMode == core.ModuleKindCommonJS {
540534
return core.ModuleKindCommonJS
541535
} else {

internal/compiler/program.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,10 @@ func (p *Program) GetEmitModuleFormatOfFile(sourceFile ast.HasFileName) core.Mod
689689
return ast.GetEmitModuleFormatOfFileWorker(sourceFile.FileName(), p.Options(), p.GetSourceFileMetaData(sourceFile.Path()))
690690
}
691691

692+
func (p *Program) GetEmitSyntaxForUsageLocation(sourceFile ast.HasFileName, location *ast.StringLiteralLike) core.ResolutionMode {
693+
return getEmitSyntaxForUsageLocationWorker(sourceFile.FileName(), p.sourceFileMetaDatas[sourceFile.Path()], location, p.Options())
694+
}
695+
692696
func (p *Program) GetImpliedNodeFormatForEmit(sourceFile ast.HasFileName) core.ResolutionMode {
693697
return ast.GetImpliedNodeFormatForEmitWorker(sourceFile.FileName(), p.Options(), p.GetSourceFileMetaData(sourceFile.Path()))
694698
}

internal/core/compileroptions.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ func (options *CompilerOptions) GetEmitScriptTarget() ScriptTarget {
149149
return options.Target
150150
}
151151
switch options.GetEmitModuleKind() {
152-
case ModuleKindNode16:
152+
case ModuleKindNode16, ModuleKindNode18:
153153
return ScriptTargetES2022
154154
case ModuleKindNodeNext:
155155
return ScriptTargetESNext
@@ -173,7 +173,7 @@ func (options *CompilerOptions) GetModuleResolutionKind() ModuleResolutionKind {
173173
return options.ModuleResolution
174174
}
175175
switch options.GetEmitModuleKind() {
176-
case ModuleKindNode16:
176+
case ModuleKindNode16, ModuleKindNode18:
177177
return ModuleResolutionKindNode16
178178
case ModuleKindNodeNext:
179179
return ModuleResolutionKindNodeNext
@@ -357,6 +357,12 @@ func (moduleKind ModuleKind) IsNonNodeESM() bool {
357357
return moduleKind >= ModuleKindES2015 && moduleKind <= ModuleKindESNext
358358
}
359359

360+
func (moduleKind ModuleKind) SupportsImportAttributes() bool {
361+
return ModuleKindNode18 <= moduleKind && moduleKind <= ModuleKindNodeNext ||
362+
moduleKind == ModuleKindPreserve ||
363+
moduleKind == ModuleKindESNext
364+
}
365+
360366
type ResolutionMode = ModuleKind // ModuleKindNone | ModuleKindCommonJS | ModuleKindESNext
361367

362368
const (

internal/transformers/importelision_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ type fakeProgram struct {
2525
getSourceFile func(FileName string) *ast.SourceFile
2626
}
2727

28+
// GetEmitSyntaxForUsageLocation implements checker.Program.
29+
func (p *fakeProgram) GetEmitSyntaxForUsageLocation(sourceFile ast.HasFileName, usageLocation *ast.StringLiteralLike) core.ResolutionMode {
30+
panic("unimplemented")
31+
}
32+
2833
// CommonSourceDirectory implements checker.Program.
2934
func (p *fakeProgram) CommonSourceDirectory() string {
3035
panic("unimplemented")

testdata/baselines/reference/submodule/compiler/dynamicImportsDeclaration.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,10 @@ export default _default;
6161
declare const _default: string;
6262
export default _default;
6363
//// [index.d.ts]
64-
export declare const mod: typeof import("./case0.js") | typeof import("./case1.js") | typeof import("./caseFallback.js");
64+
export declare const mod: {
65+
default: typeof import("./case0.js");
66+
} | {
67+
default: typeof import("./case1.js");
68+
} | {
69+
default: typeof import("./caseFallback.js");
70+
};

testdata/baselines/reference/submodule/compiler/dynamicImportsDeclaration.js.diff

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,4 @@
1616
+declare const _default: string;
1717
export default _default;
1818
//// [index.d.ts]
19-
-export declare const mod: {
20-
- default: typeof import("./case0.js");
21-
-} | {
22-
- default: typeof import("./case1.js");
23-
-} | {
24-
- default: typeof import("./caseFallback.js");
25-
-};
26-
+export declare const mod: typeof import("./case0.js") | typeof import("./case1.js") | typeof import("./caseFallback.js");
19+
export declare const mod: {

testdata/baselines/reference/submodule/compiler/dynamicImportsDeclaration.types

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ export default 'fallback';
1414

1515
=== /index.ts ===
1616
export const mod = await (async () => {
17-
>mod : typeof import("./case0.js") | typeof import("./case1.js") | typeof import("./caseFallback.js")
18-
>await (async () => { const x: number = 0; switch (x) { case 0: return await import("./case0.js"); case 1: return await import("./case1.js"); default: return await import("./caseFallback.js"); }})() : typeof import("./case0.js") | typeof import("./case1.js") | typeof import("./caseFallback.js")
19-
>(async () => { const x: number = 0; switch (x) { case 0: return await import("./case0.js"); case 1: return await import("./case1.js"); default: return await import("./caseFallback.js"); }})() : Promise<typeof import("./case0.js") | typeof import("./case1.js") | typeof import("./caseFallback.js")>
20-
>(async () => { const x: number = 0; switch (x) { case 0: return await import("./case0.js"); case 1: return await import("./case1.js"); default: return await import("./caseFallback.js"); }}) : () => Promise<typeof import("./case0.js") | typeof import("./case1.js") | typeof import("./caseFallback.js")>
21-
>async () => { const x: number = 0; switch (x) { case 0: return await import("./case0.js"); case 1: return await import("./case1.js"); default: return await import("./caseFallback.js"); }} : () => Promise<typeof import("./case0.js") | typeof import("./case1.js") | typeof import("./caseFallback.js")>
17+
>mod : { default: typeof import("./case0.js"); } | { default: typeof import("./case1.js"); } | { default: typeof import("./caseFallback.js"); }
18+
>await (async () => { const x: number = 0; switch (x) { case 0: return await import("./case0.js"); case 1: return await import("./case1.js"); default: return await import("./caseFallback.js"); }})() : { default: typeof import("./case0.js"); } | { default: typeof import("./case1.js"); } | { default: typeof import("./caseFallback.js"); }
19+
>(async () => { const x: number = 0; switch (x) { case 0: return await import("./case0.js"); case 1: return await import("./case1.js"); default: return await import("./caseFallback.js"); }})() : Promise<{ default: typeof import("./case0.js"); } | { default: typeof import("./case1.js"); } | { default: typeof import("./caseFallback.js"); }>
20+
>(async () => { const x: number = 0; switch (x) { case 0: return await import("./case0.js"); case 1: return await import("./case1.js"); default: return await import("./caseFallback.js"); }}) : () => Promise<{ default: typeof import("./case0.js"); } | { default: typeof import("./case1.js"); } | { default: typeof import("./caseFallback.js"); }>
21+
>async () => { const x: number = 0; switch (x) { case 0: return await import("./case0.js"); case 1: return await import("./case1.js"); default: return await import("./caseFallback.js"); }} : () => Promise<{ default: typeof import("./case0.js"); } | { default: typeof import("./case1.js"); } | { default: typeof import("./caseFallback.js"); }>
2222

2323
const x: number = 0;
2424
>x : number
@@ -31,22 +31,22 @@ export const mod = await (async () => {
3131
>0 : 0
3232

3333
return await import("./case0.js");
34-
>await import("./case0.js") : typeof import("./case0.js")
35-
>import("./case0.js") : Promise<typeof import("./case0.js")>
34+
>await import("./case0.js") : { default: typeof import("./case0.js"); }
35+
>import("./case0.js") : Promise<{ default: typeof import("./case0.js"); }>
3636
>"./case0.js" : "./case0.js"
3737

3838
case 1:
3939
>1 : 1
4040

4141
return await import("./case1.js");
42-
>await import("./case1.js") : typeof import("./case1.js")
43-
>import("./case1.js") : Promise<typeof import("./case1.js")>
42+
>await import("./case1.js") : { default: typeof import("./case1.js"); }
43+
>import("./case1.js") : Promise<{ default: typeof import("./case1.js"); }>
4444
>"./case1.js" : "./case1.js"
4545

4646
default:
4747
return await import("./caseFallback.js");
48-
>await import("./caseFallback.js") : typeof import("./caseFallback.js")
49-
>import("./caseFallback.js") : Promise<typeof import("./caseFallback.js")>
48+
>await import("./caseFallback.js") : { default: typeof import("./caseFallback.js"); }
49+
>import("./caseFallback.js") : Promise<{ default: typeof import("./caseFallback.js"); }>
5050
>"./caseFallback.js" : "./caseFallback.js"
5151
}
5252
})();

0 commit comments

Comments
 (0)