Skip to content

Commit 5f9c9a6

Browse files
Start Node ESM stable version at Node16 (#48879)
* Remove Node12, add Node16. * Accepted baselines. * Refactor checking for top-level await, give a better error message in CJS files. * Accepted baselines. * Stop erroring on JSON module imports in node ESM since they're no longer experimental. * Accepted baselines. * More refactoring, do the same checks for for-await loops. * Accepted baselines. * Adjust phrasing to permit for-await on CJS error. * Accepted baselines. * Accepted baselines. * Fix lints.
1 parent 38c1460 commit 5f9c9a6

File tree

403 files changed

+21233
-1371
lines changed

Some content is hidden

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

403 files changed

+21233
-1371
lines changed

src/compiler/checker.ts

Lines changed: 68 additions & 29 deletions
Large diffs are not rendered by default.

src/compiler/commandLineParser.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ namespace ts {
423423
es2020: ModuleKind.ES2020,
424424
es2022: ModuleKind.ES2022,
425425
esnext: ModuleKind.ESNext,
426-
node12: ModuleKind.Node12,
426+
node16: ModuleKind.Node16,
427427
nodenext: ModuleKind.NodeNext,
428428
})),
429429
affectsModuleResolution: true,
@@ -781,7 +781,7 @@ namespace ts {
781781
type: new Map(getEntries({
782782
node: ModuleResolutionKind.NodeJs,
783783
classic: ModuleResolutionKind.Classic,
784-
node12: ModuleResolutionKind.Node12,
784+
node16: ModuleResolutionKind.Node16,
785785
nodenext: ModuleResolutionKind.NodeNext,
786786
})),
787787
affectsModuleResolution: true,
@@ -1263,7 +1263,7 @@ namespace ts {
12631263
affectsModuleResolution: true,
12641264
description: Diagnostics.Control_what_method_is_used_to_detect_module_format_JS_files,
12651265
category: Diagnostics.Language_and_Environment,
1266-
defaultValueDescription: Diagnostics.auto_Colon_Treat_files_with_imports_exports_import_meta_jsx_with_jsx_Colon_react_jsx_or_esm_format_with_module_Colon_node12_as_modules,
1266+
defaultValueDescription: Diagnostics.auto_Colon_Treat_files_with_imports_exports_import_meta_jsx_with_jsx_Colon_react_jsx_or_esm_format_with_module_Colon_node16_as_modules,
12671267
}
12681268
];
12691269

src/compiler/diagnosticMessages.json

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -900,6 +900,10 @@
900900
"category": "Error",
901901
"code": 1308
902902
},
903+
"The current file is a CommonJS module and cannot use 'await' at the top level.": {
904+
"category": "Error",
905+
"code": 1309
906+
},
903907
"Did you mean to use a ':'? An '=' can only follow a property name when the containing object literal is part of a destructuring pattern.": {
904908
"category": "Error",
905909
"code": 1312
@@ -944,11 +948,11 @@
944948
"category": "Error",
945949
"code": 1322
946950
},
947-
"Dynamic imports are only supported when the '--module' flag is set to 'es2020', 'es2022', 'esnext', 'commonjs', 'amd', 'system', 'umd', 'node12', or 'nodenext'.": {
951+
"Dynamic imports are only supported when the '--module' flag is set to 'es2020', 'es2022', 'esnext', 'commonjs', 'amd', 'system', 'umd', 'node16', or 'nodenext'.": {
948952
"category": "Error",
949953
"code": 1323
950954
},
951-
"Dynamic imports only support a second argument when the '--module' option is set to 'esnext' or 'nodenext'.": {
955+
"Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', or 'nodenext'.": {
952956
"category": "Error",
953957
"code": 1324
954958
},
@@ -1016,7 +1020,7 @@
10161020
"category": "Error",
10171021
"code": 1342
10181022
},
1019-
"The 'import.meta' meta-property is only allowed when the '--module' option is 'es2020', 'es2022', 'esnext', 'system', 'node12', or 'nodenext'.": {
1023+
"The 'import.meta' meta-property is only allowed when the '--module' option is 'es2020', 'es2022', 'esnext', 'system', 'node16', or 'nodenext'.": {
10201024
"category": "Error",
10211025
"code": 1343
10221026
},
@@ -1140,7 +1144,7 @@
11401144
"category": "Message",
11411145
"code": 1377
11421146
},
1143-
"Top-level 'await' expressions are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', or 'nodenext', and the 'target' option is set to 'es2017' or higher.": {
1147+
"Top-level 'await' expressions are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', or 'nodenext', and the 'target' option is set to 'es2017' or higher.": {
11441148
"category": "Error",
11451149
"code": 1378
11461150
},
@@ -1348,7 +1352,7 @@
13481352
"category": "Error",
13491353
"code": 1431
13501354
},
1351-
"Top-level 'for await' loops are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', or 'nodenext', and the 'target' option is set to 'es2017' or higher.": {
1355+
"Top-level 'for await' loops are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', or 'nodenext', and the 'target' option is set to 'es2017' or higher.": {
13521356
"category": "Error",
13531357
"code": 1432
13541358
},
@@ -1420,7 +1424,7 @@
14201424
"category": "Error",
14211425
"code": 1451
14221426
},
1423-
"Resolution modes are only supported when `moduleResolution` is `node12` or `nodenext`.": {
1427+
"Resolution modes are only supported when `moduleResolution` is `node16` or `nodenext`.": {
14241428
"category": "Error",
14251429
"code": 1452
14261430
},
@@ -1465,7 +1469,7 @@
14651469
"category": "Message",
14661470
"code": 1475
14671471
},
1468-
"\"auto\": Treat files with imports, exports, import.meta, jsx (with jsx: react-jsx), or esm format (with module: node12+) as modules.": {
1472+
"\"auto\": Treat files with imports, exports, import.meta, jsx (with jsx: react-jsx), or esm format (with module: node16+) as modules.": {
14691473
"category": "Message",
14701474
"code": 1476
14711475
},
@@ -3418,11 +3422,11 @@
34183422
"category": "Error",
34193423
"code": 2833
34203424
},
3421-
"Relative import paths need explicit file extensions in EcmaScript imports when '--moduleResolution' is 'node12' or 'nodenext'. Consider adding an extension to the import path.": {
3425+
"Relative import paths need explicit file extensions in EcmaScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Consider adding an extension to the import path.": {
34223426
"category": "Error",
34233427
"code": 2834
34243428
},
3425-
"Relative import paths need explicit file extensions in EcmaScript imports when '--moduleResolution' is 'node12' or 'nodenext'. Did you mean '{0}'?": {
3429+
"Relative import paths need explicit file extensions in EcmaScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Did you mean '{0}'?": {
34263430
"category": "Error",
34273431
"code": 2835
34283432
},
@@ -6106,10 +6110,6 @@
61066110
"category": "Error",
61076111
"code": 7061
61086112
},
6109-
"JSON imports are experimental in ES module mode imports.": {
6110-
"category": "Error",
6111-
"code": 7062
6112-
},
61136113

61146114
"You cannot rename this element.": {
61156115
"category": "Error",

src/compiler/moduleNameResolver.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -348,9 +348,9 @@ namespace ts {
348348
// set in a non-modal module resolution setting here. Do note that our behavior is not particularly well defined when these mode-overriding imports
349349
// are present in a non-modal project; while in theory we'd like to either ignore the mode or provide faithful modern resolution, depending on what we feel is best,
350350
// in practice, not every cache has the options available to intelligently make the choice to ignore the mode request, and it's unclear how modern "faithful modern
351-
// resolution" should be (`node12`? `nodenext`?). As such, witnessing a mode-overriding triple-slash reference in a non-modal module resolution
351+
// resolution" should be (`node16`? `nodenext`?). As such, witnessing a mode-overriding triple-slash reference in a non-modal module resolution
352352
// context should _probably_ be an error - and that should likely be handled by the `Program` (which is what we do).
353-
if (resolutionMode === ModuleKind.ESNext && (getEmitModuleResolutionKind(options) === ModuleResolutionKind.Node12 || getEmitModuleResolutionKind(options) === ModuleResolutionKind.NodeNext)) {
353+
if (resolutionMode === ModuleKind.ESNext && (getEmitModuleResolutionKind(options) === ModuleResolutionKind.Node16 || getEmitModuleResolutionKind(options) === ModuleResolutionKind.NodeNext)) {
354354
features |= NodeResolutionFeatures.EsmMode;
355355
}
356356
const conditions = features & NodeResolutionFeatures.Exports ? features & NodeResolutionFeatures.EsmMode ? ["node", "import", "types"] : ["node", "require", "types"] : [];
@@ -444,7 +444,7 @@ namespace ts {
444444
}
445445

446446
function getDefaultNodeResolutionFeatures(options: CompilerOptions) {
447-
return getEmitModuleResolutionKind(options) === ModuleResolutionKind.Node12 ? NodeResolutionFeatures.Node12Default :
447+
return getEmitModuleResolutionKind(options) === ModuleResolutionKind.Node16 ? NodeResolutionFeatures.Node16Default :
448448
getEmitModuleResolutionKind(options) === ModuleResolutionKind.NodeNext ? NodeResolutionFeatures.NodeNextDefault :
449449
NodeResolutionFeatures.None;
450450
}
@@ -950,8 +950,8 @@ namespace ts {
950950
case ModuleKind.CommonJS:
951951
moduleResolution = ModuleResolutionKind.NodeJs;
952952
break;
953-
case ModuleKind.Node12:
954-
moduleResolution = ModuleResolutionKind.Node12;
953+
case ModuleKind.Node16:
954+
moduleResolution = ModuleResolutionKind.Node16;
955955
break;
956956
case ModuleKind.NodeNext:
957957
moduleResolution = ModuleResolutionKind.NodeNext;
@@ -972,8 +972,8 @@ namespace ts {
972972

973973
perfLogger.logStartResolveModule(moduleName /* , containingFile, ModuleResolutionKind[moduleResolution]*/);
974974
switch (moduleResolution) {
975-
case ModuleResolutionKind.Node12:
976-
result = node12ModuleNameResolver(moduleName, containingFile, compilerOptions, host, cache, redirectedReference, resolutionMode);
975+
case ModuleResolutionKind.Node16:
976+
result = node16ModuleNameResolver(moduleName, containingFile, compilerOptions, host, cache, redirectedReference, resolutionMode);
977977
break;
978978
case ModuleResolutionKind.NodeNext:
979979
result = nodeNextModuleNameResolver(moduleName, containingFile, compilerOptions, host, cache, redirectedReference, resolutionMode);
@@ -1231,22 +1231,22 @@ namespace ts {
12311231
// respecting the `.exports` member of packages' package.json files and its (conditional) mappings of export names
12321232
Exports = 1 << 3,
12331233
// allowing `*` in the LHS of an export to be followed by more content, eg `"./whatever/*.js"`
1234-
// not currently backported to node 12 - https://github.com/nodejs/Release/issues/690
1234+
// not supported in node 12 - https://github.com/nodejs/Release/issues/690
12351235
ExportsPatternTrailers = 1 << 4,
12361236
AllFeatures = Imports | SelfName | Exports | ExportsPatternTrailers,
12371237

1238-
Node12Default = Imports | SelfName | Exports,
1238+
Node16Default = Imports | SelfName | Exports | ExportsPatternTrailers,
12391239

12401240
NodeNextDefault = AllFeatures,
12411241

12421242
EsmMode = 1 << 5,
12431243
}
12441244

1245-
function node12ModuleNameResolver(moduleName: string, containingFile: string, compilerOptions: CompilerOptions,
1245+
function node16ModuleNameResolver(moduleName: string, containingFile: string, compilerOptions: CompilerOptions,
12461246
host: ModuleResolutionHost, cache?: ModuleResolutionCache, redirectedReference?: ResolvedProjectReference,
12471247
resolutionMode?: ModuleKind.CommonJS | ModuleKind.ESNext): ResolvedModuleWithFailedLookupLocations {
12481248
return nodeNextModuleNameResolverWorker(
1249-
NodeResolutionFeatures.Node12Default,
1249+
NodeResolutionFeatures.Node16Default,
12501250
moduleName,
12511251
containingFile,
12521252
compilerOptions,
@@ -1310,7 +1310,7 @@ namespace ts {
13101310
const traceEnabled = isTraceEnabled(compilerOptions, host);
13111311

13121312
const failedLookupLocations: string[] = [];
1313-
// conditions are only used by the node12/nodenext resolver - there's no priority order in the list,
1313+
// conditions are only used by the node16/nodenext resolver - there's no priority order in the list,
13141314
//it's essentially a set (priority is determined by object insertion order in the object we look at).
13151315
const conditions = features & NodeResolutionFeatures.EsmMode ? ["node", "import", "types"] : ["node", "require", "types"];
13161316
if (compilerOptions.noDtsResolution) {

src/compiler/moduleSpecifiers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ namespace ts.moduleSpecifiers {
4141
}
4242

4343
function isFormatRequiringExtensions(compilerOptions: CompilerOptions, importingSourceFileName: Path, host: ModuleSpecifierResolutionHost) {
44-
if (getEmitModuleResolutionKind(compilerOptions) !== ModuleResolutionKind.Node12
44+
if (getEmitModuleResolutionKind(compilerOptions) !== ModuleResolutionKind.Node16
4545
&& getEmitModuleResolutionKind(compilerOptions) !== ModuleResolutionKind.NodeNext) {
4646
return false;
4747
}
@@ -734,7 +734,7 @@ namespace ts.moduleSpecifiers {
734734
const cachedPackageJson = host.getPackageJsonInfoCache?.()?.getPackageJsonInfo(packageJsonPath);
735735
if (typeof cachedPackageJson === "object" || cachedPackageJson === undefined && host.fileExists(packageJsonPath)) {
736736
const packageJsonContent = cachedPackageJson?.packageJsonContent || JSON.parse(host.readFile!(packageJsonPath)!);
737-
if (getEmitModuleResolutionKind(options) === ModuleResolutionKind.Node12 || getEmitModuleResolutionKind(options) === ModuleResolutionKind.NodeNext) {
737+
if (getEmitModuleResolutionKind(options) === ModuleResolutionKind.Node16 || getEmitModuleResolutionKind(options) === ModuleResolutionKind.NodeNext) {
738738
// `conditions` *could* be made to go against `importingSourceFile.impliedNodeFormat` if something wanted to generate
739739
// an ImportEqualsDeclaration in an ESM-implied file or an ImportCall in a CJS-implied file. But since this function is
740740
// usually called to conjure an import out of thin air, we don't have an existing usage to call `getModeForUsageAtIndex`

src/compiler/parser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,7 @@ namespace ts {
683683
/**
684684
* Controls the format the file is detected as - this can be derived from only the path
685685
* and files on disk, but needs to be done with a module resolution cache in scope to be performant.
686-
* This is usually `undefined` for compilations that do not have `moduleResolution` values of `node12` or `nodenext`.
686+
* This is usually `undefined` for compilations that do not have `moduleResolution` values of `node16` or `nodenext`.
687687
*/
688688
impliedNodeFormat?: ModuleKind.ESNext | ModuleKind.CommonJS;
689689
/**

src/compiler/program.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -858,7 +858,7 @@ namespace ts {
858858
*/
859859
export function getImpliedNodeFormatForFile(fileName: Path, packageJsonInfoCache: PackageJsonInfoCache | undefined, host: ModuleResolutionHost, options: CompilerOptions): ModuleKind.ESNext | ModuleKind.CommonJS | undefined {
860860
switch (getEmitModuleResolutionKind(options)) {
861-
case ModuleResolutionKind.Node12:
861+
case ModuleResolutionKind.Node16:
862862
case ModuleResolutionKind.NodeNext:
863863
return fileExtensionIsOneOf(fileName, [Extension.Dmts, Extension.Mts, Extension.Mjs]) ? ModuleKind.ESNext :
864864
fileExtensionIsOneOf(fileName, [Extension.Dcts, Extension.Cts, Extension.Cjs]) ? ModuleKind.CommonJS :
@@ -1210,7 +1210,7 @@ namespace ts {
12101210
const containingFilename = combinePaths(containingDirectory, inferredTypesContainingFile);
12111211
const resolutions = resolveTypeReferenceDirectiveNamesWorker(typeReferences, containingFilename);
12121212
for (let i = 0; i < typeReferences.length; i++) {
1213-
// under node12/nodenext module resolution, load `types`/ata include names as cjs resolution results by passing an `undefined` mode
1213+
// under node16/nodenext module resolution, load `types`/ata include names as cjs resolution results by passing an `undefined` mode
12141214
processTypeReferenceDirective(typeReferences[i], /*mode*/ undefined, resolutions[i], { kind: FileIncludeKind.AutomaticTypeDirectiveFile, typeReference: typeReferences[i], packageId: resolutions[i]?.packageId });
12151215
}
12161216
tracing?.pop();
@@ -3085,8 +3085,8 @@ namespace ts {
30853085
const fileName = toFileNameLowerCase(ref.fileName);
30863086
setResolvedTypeReferenceDirective(file, fileName, resolvedTypeReferenceDirective);
30873087
const mode = ref.resolutionMode || file.impliedNodeFormat;
3088-
if (mode && getEmitModuleResolutionKind(options) !== ModuleResolutionKind.Node12 && getEmitModuleResolutionKind(options) !== ModuleResolutionKind.NodeNext) {
3089-
programDiagnostics.add(createDiagnosticForRange(file, ref, Diagnostics.Resolution_modes_are_only_supported_when_moduleResolution_is_node12_or_nodenext));
3088+
if (mode && getEmitModuleResolutionKind(options) !== ModuleResolutionKind.Node16 && getEmitModuleResolutionKind(options) !== ModuleResolutionKind.NodeNext) {
3089+
programDiagnostics.add(createDiagnosticForRange(file, ref, Diagnostics.Resolution_modes_are_only_supported_when_moduleResolution_is_node16_or_nodenext));
30903090
}
30913091
processTypeReferenceDirective(fileName, mode, resolvedTypeReferenceDirective, { kind: FileIncludeKind.TypeReferenceDirective, file: file.path, index, });
30923092
}
@@ -3515,7 +3515,7 @@ namespace ts {
35153515

35163516
if (options.resolveJsonModule) {
35173517
if (getEmitModuleResolutionKind(options) !== ModuleResolutionKind.NodeJs &&
3518-
getEmitModuleResolutionKind(options) !== ModuleResolutionKind.Node12 &&
3518+
getEmitModuleResolutionKind(options) !== ModuleResolutionKind.Node16 &&
35193519
getEmitModuleResolutionKind(options) !== ModuleResolutionKind.NodeNext) {
35203520
createDiagnosticForOptionName(Diagnostics.Option_resolveJsonModule_cannot_be_specified_without_node_module_resolution_strategy, "resolveJsonModule");
35213521
}

src/compiler/transformer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace ts {
99
return transformECMAScriptModule;
1010
case ModuleKind.System:
1111
return transformSystemModule;
12-
case ModuleKind.Node12:
12+
case ModuleKind.Node16:
1313
case ModuleKind.NodeNext:
1414
return transformNodeModule;
1515
default:

0 commit comments

Comments
 (0)