- Notifications
You must be signed in to change notification settings - Fork13.2k
Add--module node20#61805
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Add--module node20#61805
Changes from3 commits
Commits
Show all changes
4 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
The table of contents is too big for display.
Diff view
Diff view
Uh oh!
There was an error while loading.Please reload this page.
There are no files selected for viewing
93 changes: 72 additions & 21 deletionssrc/compiler/checker.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -3683,6 +3683,12 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | ||
| getExternalModuleRequireArgument(node) || getExternalModuleImportEqualsDeclarationExpression(node), | ||
| ); | ||
| const resolved = resolveExternalModuleSymbol(immediate); | ||
| if (resolved && ModuleKind.Node20 <= moduleKind && moduleKind <= ModuleKind.NodeNext) { | ||
| const moduleExports = getExportOfModule(resolved, "module.exports" as __String, node, dontResolveAlias); | ||
| if (moduleExports) { | ||
| return moduleExports; | ||
| } | ||
| } | ||
andrewbranch marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| markSymbolOfAliasDeclarationIfTypeOnly(node, immediate, resolved, /*overwriteEmpty*/ false); | ||
| return resolved; | ||
| } | ||
| @@ -3798,16 +3804,44 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | ||
| } | ||
| function getTargetofModuleDefault(moduleSymbol: Symbol, node: ImportClause | ImportOrExportSpecifier, dontResolveAlias: boolean) { | ||
| const file = moduleSymbol.declarations?.find(isSourceFile); | ||
| const specifier = getModuleSpecifierForImportOrExport(node); | ||
| let exportDefaultSymbol: Symbol | undefined; | ||
| let exportModuleDotExportsSymbol: Symbol | undefined; | ||
| if (isShorthandAmbientModuleSymbol(moduleSymbol)) { | ||
| exportDefaultSymbol = moduleSymbol; | ||
| } | ||
| else if ( | ||
| file && specifier && | ||
| ModuleKind.Node20 <= moduleKind && moduleKind <= ModuleKind.NodeNext && | ||
| getEmitSyntaxForModuleSpecifierExpression(specifier) === ModuleKind.CommonJS && | ||
| host.getImpliedNodeFormatForEmit(file) === ModuleKind.ESNext && | ||
| (exportModuleDotExportsSymbol = resolveExportByName(moduleSymbol, "module.exports" as __String, node, dontResolveAlias)) | ||
| ) { | ||
| // We have a transpiled default import where the `require` resolves to an ES module with a `module.exports` named | ||
| // export. If `esModuleInterop` is enabled, this will work: | ||
| // | ||
| // const dep_1 = __importDefault(require("./dep.mjs")); // wraps like { default: require("./dep.mjs") } | ||
| // dep_1.default; // require("./dep.mjs") -> the `module.exports` export value | ||
| // | ||
| // But without `esModuleInterop`, it will be broken: | ||
| // | ||
| // const dep_1 = require("./dep.mjs"); // the `module.exports` export value (could be primitive) | ||
| // dep_1.default; // `default` property access on the `module.exports` export value | ||
| // | ||
| // We could try to resolve the 'default' property in the latter case, but it's a mistake to run in this | ||
| // environment without `esModuleInterop`, so just error. | ||
| if (!getESModuleInterop(compilerOptions)) { | ||
| error(node.name, Diagnostics.Module_0_can_only_be_default_imported_using_the_1_flag, symbolToString(moduleSymbol), "esModuleInterop"); | ||
| return undefined; | ||
| } | ||
| markSymbolOfAliasDeclarationIfTypeOnly(node, exportModuleDotExportsSymbol, /*finalTarget*/ undefined, /*overwriteEmpty*/ false); | ||
| return exportModuleDotExportsSymbol; | ||
| } | ||
| else { | ||
| exportDefaultSymbol = resolveExportByName(moduleSymbol, InternalSymbolName.Default, node, dontResolveAlias); | ||
| } | ||
| if (!specifier) { | ||
| return exportDefaultSymbol; | ||
| } | ||
| @@ -4953,10 +4987,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | ||
| } | ||
| const referenceParent = referencingLocation.parent; | ||
| const namespaceImport = isImportDeclaration(referenceParent) && getNamespaceDeclarationNode(referenceParent); | ||
| if (namespaceImport || isImportCall(referenceParent)) { | ||
| const reference = isImportCall(referenceParent) ? referenceParent.arguments[0] : referenceParent.moduleSpecifier; | ||
| const type = getTypeOfSymbol(symbol); | ||
| const defaultOnlyType = getTypeWithSyntheticDefaultOnly(type, symbol, moduleSymbol!, reference); | ||
| @@ -4965,14 +4997,27 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | ||
| } | ||
| const targetFile = moduleSymbol?.declarations?.find(isSourceFile); | ||
| const usageMode = getEmitSyntaxForModuleSpecifierExpression(reference); | ||
| let exportModuleDotExportsSymbol: Symbol | undefined; | ||
| if ( | ||
| namespaceImport && targetFile && | ||
| ModuleKind.Node20 <= moduleKind && moduleKind <= ModuleKind.NodeNext && | ||
| usageMode === ModuleKind.CommonJS && host.getImpliedNodeFormatForEmit(targetFile) === ModuleKind.ESNext && | ||
| (exportModuleDotExportsSymbol = resolveExportByName(symbol, "module.exports" as __String, namespaceImport, dontResolveAlias)) | ||
| ) { | ||
| if (!suppressInteropError && !(symbol.flags & (SymbolFlags.Module | SymbolFlags.Variable))) { | ||
| 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"); | ||
| } | ||
| if (getESModuleInterop(compilerOptions) && hasSignatures(type)) { | ||
| return cloneTypeAsModuleType(exportModuleDotExportsSymbol, type, referenceParent); | ||
| } | ||
| return exportModuleDotExportsSymbol; | ||
| } | ||
| const isEsmCjsRef = targetFile && isESMFormatImportImportingCommonjsFormatFile(usageMode, host.getImpliedNodeFormatForEmit(targetFile)); | ||
| if (getESModuleInterop(compilerOptions) || isEsmCjsRef) { | ||
| if ( | ||
| hasSignatures(type) || | ||
| getPropertyOfType(type, InternalSymbolName.Default, /*skipObjectFunctionPropertyAugment*/ true) || | ||
| isEsmCjsRef | ||
| ) { | ||
| @@ -4987,6 +5032,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | ||
| return symbol; | ||
| } | ||
| function hasSignatures(type: Type): boolean { | ||
| return some(getSignaturesOfStructuredType(type, SignatureKind.Call)) || some(getSignaturesOfStructuredType(type, SignatureKind.Construct)); | ||
| } | ||
| /** | ||
| * Create a new symbol which has the module's type less the call and construct signatures | ||
| */ | ||
| @@ -38054,7 +38103,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | ||
| } | ||
| } | ||
| 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_node18_node20_or_nodenext); | ||
| } | ||
| const file = getSourceFileOfNode(node); | ||
| Debug.assert(!!(file.flags & NodeFlags.PossiblyContainsImportMeta), "Containing file is missing import meta node flag."); | ||
| @@ -39636,6 +39685,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | ||
| switch (moduleKind) { | ||
| case ModuleKind.Node16: | ||
| case ModuleKind.Node18: | ||
| case ModuleKind.Node20: | ||
| case ModuleKind.NodeNext: | ||
| if (sourceFile.impliedNodeFormat === ModuleKind.CommonJS) { | ||
| span ??= getSpanOfTokenAtPosition(sourceFile, node.pos); | ||
| @@ -39656,8 +39706,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_node18_node20_nodenext_or_preserve_and_the_target_option_is_set_to_es2017_or_higher : | ||
andrewbranch marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| Diagnostics.Top_level_await_using_statements_are_only_allowed_when_the_module_option_is_set_to_es2022_esnext_system_node16_node18_node20_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; | ||
| @@ -48264,12 +48314,12 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | ||
| return grammarErrorOnNode( | ||
| node, | ||
| isImportAttributes | ||
| ? Diagnostics.Import_attributes_are_only_supported_when_the_module_option_is_set_to_esnext_node18_node20_nodenext_or_preserve | ||
| : Diagnostics.Import_assertions_are_only_supported_when_the_module_option_is_set_to_esnext_node18_node20_nodenext_or_preserve, | ||
| ); | ||
| } | ||
| if (ModuleKind.Node20 <= moduleKind && moduleKind <= ModuleKind.NodeNext && !isImportAttributes) { | ||
| return grammarErrorOnFirstToken(node, Diagnostics.Import_assertions_have_been_replaced_by_import_attributes_Use_with_instead_of_assert); | ||
| } | ||
| @@ -52313,6 +52363,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | ||
| switch (moduleKind) { | ||
| case ModuleKind.Node16: | ||
| case ModuleKind.Node18: | ||
| case ModuleKind.Node20: | ||
| case ModuleKind.NodeNext: | ||
| if (sourceFile.impliedNodeFormat === ModuleKind.CommonJS) { | ||
| diagnostics.add( | ||
| @@ -52331,7 +52382,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_node18_node20_nodenext_or_preserve_and_the_target_option_is_set_to_es2017_or_higher), | ||
| ); | ||
| break; | ||
| } | ||
| @@ -53134,7 +53185,7 @@ 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_node18_node20_or_nodenext); | ||
| } | ||
| if (node.typeArguments) { | ||
| @@ -53148,7 +53199,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | ||
| 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_node18_node20_nodenext_or_preserve); | ||
| } | ||
| } | ||
1 change: 1 addition & 0 deletionssrc/compiler/commandLineParser.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
16 changes: 8 additions & 8 deletionssrc/compiler/diagnosticMessages.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletionssrc/compiler/transformer.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletionssrc/compiler/types.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.