Skip to content

Commit

Permalink
Add --module node18 (#60722)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewbranch authored Dec 13, 2024
1 parent 4a18b5c commit f69580f
Show file tree
Hide file tree
Showing 463 changed files with 31,423 additions and 529 deletions.
45 changes: 27 additions & 18 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,7 @@ import {
ModuleKind,
ModuleResolutionKind,
ModuleSpecifierResolutionHost,
moduleSupportsImportAttributes,
Mutable,
MutableNodeArray,
NamedDeclaration,
Expand Down Expand Up @@ -37631,13 +37632,13 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}

function checkImportMetaProperty(node: MetaProperty) {
if (moduleKind === ModuleKind.Node16 || moduleKind === ModuleKind.NodeNext) {
if (ModuleKind.Node16 <= moduleKind && moduleKind <= ModuleKind.NodeNext) {
if (getSourceFileOfNode(node).impliedNodeFormat !== ModuleKind.ESNext) {
error(node, Diagnostics.The_import_meta_meta_property_is_not_allowed_in_files_which_will_build_into_CommonJS_output);
}
}
else if (moduleKind < ModuleKind.ES2020 && moduleKind !== ModuleKind.System) {
error(node, Diagnostics.The_import_meta_meta_property_is_only_allowed_when_the_module_option_is_es2020_es2022_esnext_system_node16_or_nodenext);
error(node, Diagnostics.The_import_meta_meta_property_is_only_allowed_when_the_module_option_is_es2020_es2022_esnext_system_node16_node18_or_nodenext);
}
const file = getSourceFileOfNode(node);
Debug.assert(!!(file.flags & NodeFlags.PossiblyContainsImportMeta), "Containing file is missing import meta node flag.");
Expand Down Expand Up @@ -39212,6 +39213,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
switch (moduleKind) {
case ModuleKind.Node16:
case ModuleKind.Node18:
case ModuleKind.NodeNext:
if (sourceFile.impliedNodeFormat === ModuleKind.CommonJS) {
span ??= getSpanOfTokenAtPosition(sourceFile, node.pos);
Expand All @@ -39232,8 +39234,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
// fallthrough
default:
span ??= getSpanOfTokenAtPosition(sourceFile, node.pos);
const message = isAwaitExpression(node) ? Diagnostics.Top_level_await_expressions_are_only_allowed_when_the_module_option_is_set_to_es2022_esnext_system_node16_nodenext_or_preserve_and_the_target_option_is_set_to_es2017_or_higher :
Diagnostics.Top_level_await_using_statements_are_only_allowed_when_the_module_option_is_set_to_es2022_esnext_system_node16_nodenext_or_preserve_and_the_target_option_is_set_to_es2017_or_higher;
const message = isAwaitExpression(node) ? Diagnostics.Top_level_await_expressions_are_only_allowed_when_the_module_option_is_set_to_es2022_esnext_system_node16_node18_nodenext_or_preserve_and_the_target_option_is_set_to_es2017_or_higher :
Diagnostics.Top_level_await_using_statements_are_only_allowed_when_the_module_option_is_set_to_es2022_esnext_system_node16_node18_nodenext_or_preserve_and_the_target_option_is_set_to_es2017_or_higher;
diagnostics.add(createFileDiagnostic(sourceFile, span.start, span.length, message));
hasError = true;
break;
Expand Down Expand Up @@ -48064,16 +48066,22 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return; // Other grammar checks do not apply to type-only imports with resolution mode assertions
}

const mode = (moduleKind === ModuleKind.NodeNext) && declaration.moduleSpecifier && getEmitSyntaxForModuleSpecifierExpression(declaration.moduleSpecifier);
if (mode !== ModuleKind.ESNext && moduleKind !== ModuleKind.ESNext && moduleKind !== ModuleKind.Preserve) {
const message = isImportAttributes
? moduleKind === ModuleKind.NodeNext
if (!moduleSupportsImportAttributes(moduleKind)) {
return grammarErrorOnNode(
node,
isImportAttributes
? Diagnostics.Import_attributes_are_only_supported_when_the_module_option_is_set_to_esnext_node18_nodenext_or_preserve
: Diagnostics.Import_assertions_are_only_supported_when_the_module_option_is_set_to_esnext_node18_nodenext_or_preserve,
);
}

if (declaration.moduleSpecifier && getEmitSyntaxForModuleSpecifierExpression(declaration.moduleSpecifier) === ModuleKind.CommonJS) {
return grammarErrorOnNode(
node,
isImportAttributes
? Diagnostics.Import_attributes_are_not_allowed_on_statements_that_compile_to_CommonJS_require_calls
: Diagnostics.Import_attributes_are_only_supported_when_the_module_option_is_set_to_esnext_nodenext_or_preserve
: moduleKind === ModuleKind.NodeNext
? Diagnostics.Import_assertions_are_not_allowed_on_statements_that_compile_to_CommonJS_require_calls
: Diagnostics.Import_assertions_are_only_supported_when_the_module_option_is_set_to_esnext_nodenext_or_preserve;
return grammarErrorOnNode(node, message);
: Diagnostics.Import_assertions_are_not_allowed_on_statements_that_compile_to_CommonJS_require_calls,
);
}

const isTypeOnly = isJSDocImportTag(declaration) || (isImportDeclaration(declaration) ? declaration.importClause?.isTypeOnly : declaration.isTypeOnly);
Expand Down Expand Up @@ -48122,7 +48130,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
}

if (!importClause.isTypeOnly && moduleKind === ModuleKind.NodeNext && isOnlyImportableAsDefault(node.moduleSpecifier, resolvedModule) && !hasTypeJsonImportAttribute(node)) {
if (!importClause.isTypeOnly && ModuleKind.Node18 <= moduleKind && moduleKind <= ModuleKind.NodeNext && isOnlyImportableAsDefault(node.moduleSpecifier, resolvedModule) && !hasTypeJsonImportAttribute(node)) {
// Import attributes/assertions are not allowed in --module node16, so don't suggest adding one
error(node.moduleSpecifier, Diagnostics.Importing_a_JSON_file_into_an_ECMAScript_module_requires_a_type_Colon_json_import_attribute_when_module_is_set_to_0, ModuleKind[moduleKind]);
}
Expand Down Expand Up @@ -52062,6 +52070,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
switch (moduleKind) {
case ModuleKind.Node16:
case ModuleKind.Node18:
case ModuleKind.NodeNext:
if (sourceFile.impliedNodeFormat === ModuleKind.CommonJS) {
diagnostics.add(
Expand All @@ -52080,7 +52089,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
// fallthrough
default:
diagnostics.add(
createDiagnosticForNode(forInOrOfStatement.awaitModifier, Diagnostics.Top_level_for_await_loops_are_only_allowed_when_the_module_option_is_set_to_es2022_esnext_system_node16_nodenext_or_preserve_and_the_target_option_is_set_to_es2017_or_higher),
createDiagnosticForNode(forInOrOfStatement.awaitModifier, Diagnostics.Top_level_for_await_loops_are_only_allowed_when_the_module_option_is_set_to_es2022_esnext_system_node16_node18_nodenext_or_preserve_and_the_target_option_is_set_to_es2017_or_higher),
);
break;
}
Expand Down Expand Up @@ -52882,21 +52891,21 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}

if (moduleKind === ModuleKind.ES2015) {
return grammarErrorOnNode(node, Diagnostics.Dynamic_imports_are_only_supported_when_the_module_flag_is_set_to_es2020_es2022_esnext_commonjs_amd_system_umd_node16_or_nodenext);
return grammarErrorOnNode(node, Diagnostics.Dynamic_imports_are_only_supported_when_the_module_flag_is_set_to_es2020_es2022_esnext_commonjs_amd_system_umd_node16_node18_or_nodenext);
}

if (node.typeArguments) {
return grammarErrorOnNode(node, Diagnostics.This_use_of_import_is_invalid_import_calls_can_be_written_but_they_must_have_parentheses_and_cannot_have_type_arguments);
}

const nodeArguments = node.arguments;
if (moduleKind !== ModuleKind.ESNext && moduleKind !== ModuleKind.NodeNext && moduleKind !== ModuleKind.Node16 && moduleKind !== ModuleKind.Preserve) {
if (!(ModuleKind.Node16 <= moduleKind && moduleKind <= ModuleKind.NodeNext) && moduleKind !== ModuleKind.ESNext && moduleKind !== ModuleKind.Preserve) {
// We are allowed trailing comma after proposal-import-assertions.
checkGrammarForDisallowedTrailingComma(nodeArguments);

if (nodeArguments.length > 1) {
const importAttributesArgument = nodeArguments[1];
return grammarErrorOnNode(importAttributesArgument, Diagnostics.Dynamic_imports_only_support_a_second_argument_when_the_module_option_is_set_to_esnext_node16_nodenext_or_preserve);
return grammarErrorOnNode(importAttributesArgument, Diagnostics.Dynamic_imports_only_support_a_second_argument_when_the_module_option_is_set_to_esnext_node16_node18_nodenext_or_preserve);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/compiler/commandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,7 @@ export const moduleOptionDeclaration: CommandLineOptionOfCustomType = {
es2022: ModuleKind.ES2022,
esnext: ModuleKind.ESNext,
node16: ModuleKind.Node16,
node18: ModuleKind.Node18,
nodenext: ModuleKind.NodeNext,
preserve: ModuleKind.Preserve,
})),
Expand Down
16 changes: 8 additions & 8 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1028,11 +1028,11 @@
"category": "Error",
"code": 1322
},
"Dynamic imports are only supported when the '--module' flag is set to 'es2020', 'es2022', 'esnext', 'commonjs', 'amd', 'system', 'umd', 'node16', or 'nodenext'.": {
"Dynamic imports are only supported when the '--module' flag is set to 'es2020', 'es2022', 'esnext', 'commonjs', 'amd', 'system', 'umd', 'node16', 'node18', or 'nodenext'.": {
"category": "Error",
"code": 1323
},
"Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', 'nodenext', or 'preserve'.": {
"Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', 'node18', 'nodenext', or 'preserve'.": {
"category": "Error",
"code": 1324
},
Expand Down Expand Up @@ -1100,7 +1100,7 @@
"category": "Error",
"code": 1341
},
"The 'import.meta' meta-property is only allowed when the '--module' option is 'es2020', 'es2022', 'esnext', 'system', 'node16', or 'nodenext'.": {
"The 'import.meta' meta-property is only allowed when the '--module' option is 'es2020', 'es2022', 'esnext', 'system', 'node16', 'node18', or 'nodenext'.": {
"category": "Error",
"code": 1343
},
Expand Down Expand Up @@ -1220,7 +1220,7 @@
"category": "Message",
"code": 1377
},
"Top-level 'await' expressions are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher.": {
"Top-level 'await' expressions are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'node18', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher.": {
"category": "Error",
"code": 1378
},
Expand Down Expand Up @@ -1424,7 +1424,7 @@
"category": "Error",
"code": 1431
},
"Top-level 'for await' loops are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher.": {
"Top-level 'for await' loops are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'node18', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher.": {
"category": "Error",
"code": 1432
},
Expand Down Expand Up @@ -3788,15 +3788,15 @@
"category": "Error",
"code": 2820
},
"Import assertions are only supported when the '--module' option is set to 'esnext', 'nodenext', or 'preserve'.": {
"Import assertions are only supported when the '--module' option is set to 'esnext', 'node18', 'nodenext', or 'preserve'.": {
"category": "Error",
"code": 2821
},
"Import assertions cannot be used with type-only imports or exports.": {
"category": "Error",
"code": 2822
},
"Import attributes are only supported when the '--module' option is set to 'esnext', 'nodenext', or 'preserve'.": {
"Import attributes are only supported when the '--module' option is set to 'esnext', 'node18', 'nodenext', or 'preserve'.": {
"category": "Error",
"code": 2823
},
Expand Down Expand Up @@ -3876,7 +3876,7 @@
"category": "Error",
"code": 2853
},
"Top-level 'await using' statements are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher.": {
"Top-level 'await using' statements are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'node18', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher.": {
"category": "Error",
"code": 2854
},
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4539,7 +4539,8 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
!(ModuleResolutionKind.Node16 <= moduleResolution && moduleResolution <= ModuleResolutionKind.NodeNext)
) {
const moduleKindName = ModuleKind[moduleKind];
createOptionValueDiagnostic("moduleResolution", Diagnostics.Option_moduleResolution_must_be_set_to_0_or_left_unspecified_when_option_module_is_set_to_1, moduleKindName, moduleKindName);
const moduleResolutionName = ModuleResolutionKind[moduleKindName as any] ? moduleKindName : "Node16";
createOptionValueDiagnostic("moduleResolution", Diagnostics.Option_moduleResolution_must_be_set_to_0_or_left_unspecified_when_option_module_is_set_to_1, moduleResolutionName, moduleKindName);
}
else if (
ModuleResolutionKind[moduleResolution] &&
Expand Down
1 change: 1 addition & 0 deletions src/compiler/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ function getModuleTransformer(moduleKind: ModuleKind): TransformerFactory<Source
case ModuleKind.ES2020:
case ModuleKind.ES2015:
case ModuleKind.Node16:
case ModuleKind.Node18:
case ModuleKind.NodeNext:
case ModuleKind.CommonJS:
// Wraps `transformModule` and `transformECMAScriptModule` and
Expand Down
1 change: 1 addition & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7545,6 +7545,7 @@ export enum ModuleKind {

// Node16+ is an amalgam of commonjs (albeit updated) and es2022+, and represents a distinct module system from es2020/esnext
Node16 = 100,
Node18 = 101,
NodeNext = 199,

// Emit as written
Expand Down
25 changes: 21 additions & 4 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2074,7 +2074,7 @@ export function getNonAugmentationDeclaration(symbol: Symbol): Declaration | und
}

function isCommonJSContainingModuleKind(kind: ModuleKind) {
return kind === ModuleKind.CommonJS || kind === ModuleKind.Node16 || kind === ModuleKind.NodeNext;
return kind === ModuleKind.CommonJS || ModuleKind.Node16 <= kind && kind <= ModuleKind.NodeNext;
}

/** @internal */
Expand Down Expand Up @@ -8940,6 +8940,7 @@ const _computedOptions = createComputedCompilerOptions({
const target = compilerOptions.target === ScriptTarget.ES3 ? undefined : compilerOptions.target;
return target ??
((compilerOptions.module === ModuleKind.Node16 && ScriptTarget.ES2022) ||
(compilerOptions.module === ModuleKind.Node18 && ScriptTarget.ES2022) ||
(compilerOptions.module === ModuleKind.NodeNext && ScriptTarget.ESNext) ||
ScriptTarget.ES5);
},
Expand All @@ -8962,6 +8963,7 @@ const _computedOptions = createComputedCompilerOptions({
moduleResolution = ModuleResolutionKind.Node10;
break;
case ModuleKind.Node16:
case ModuleKind.Node18:
moduleResolution = ModuleResolutionKind.Node16;
break;
case ModuleKind.NodeNext:
Expand All @@ -8981,9 +8983,13 @@ const _computedOptions = createComputedCompilerOptions({
moduleDetection: {
dependencies: ["module", "target"],
computeValue: (compilerOptions): ModuleDetectionKind => {
return compilerOptions.moduleDetection ||
(_computedOptions.module.computeValue(compilerOptions) === ModuleKind.Node16 ||
_computedOptions.module.computeValue(compilerOptions) === ModuleKind.NodeNext ? ModuleDetectionKind.Force : ModuleDetectionKind.Auto);
if (compilerOptions.moduleDetection !== undefined) {
return compilerOptions.moduleDetection;
}
const moduleKind = _computedOptions.module.computeValue(compilerOptions);
return ModuleKind.Node16 <= moduleKind && moduleKind <= ModuleKind.NodeNext
? ModuleDetectionKind.Force
: ModuleDetectionKind.Auto;
},
},
isolatedModules: {
Expand All @@ -9000,6 +9006,7 @@ const _computedOptions = createComputedCompilerOptions({
}
switch (_computedOptions.module.computeValue(compilerOptions)) {
case ModuleKind.Node16:
case ModuleKind.Node18:
case ModuleKind.NodeNext:
case ModuleKind.Preserve:
return true;
Expand Down Expand Up @@ -9229,6 +9236,16 @@ export function moduleResolutionSupportsPackageJsonExportsAndImports(moduleResol
|| moduleResolution === ModuleResolutionKind.Bundler;
}

/**
* @internal
* The same set of options also support import assertions.
*/
export function moduleSupportsImportAttributes(moduleKind: ModuleKind): boolean {
return ModuleKind.Node18 <= moduleKind && moduleKind <= ModuleKind.NodeNext
|| moduleKind === ModuleKind.Preserve
|| moduleKind === ModuleKind.ESNext;
}

/** @internal */
export type StrictOptionName =
| "noImplicitAny"
Expand Down
1 change: 1 addition & 0 deletions src/server/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3254,6 +3254,7 @@ export const enum ModuleKind {
ES2022 = "es2022",
ESNext = "esnext",
Node16 = "node16",
Node18 = "node18",
NodeNext = "nodenext",
Preserve = "preserve",
}
Expand Down
Loading

0 comments on commit f69580f

Please sign in to comment.