Skip to content

Commit 086e664

Browse files
Merge remote-tracking branch 'origin/main' into release-5.9
2 parents ae9e5da + be86783 commit 086e664

File tree

49 files changed

+21193
-5688
lines changed

Some content is hidden

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

49 files changed

+21193
-5688
lines changed

src/compiler/checker.ts

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2528,6 +2528,20 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
25282528
return diagnostic;
25292529
}
25302530

2531+
function getVerbatimModuleSyntaxErrorMessage(node: Node): DiagnosticMessage {
2532+
const sourceFile = getSourceFileOfNode(node);
2533+
const fileName = sourceFile.fileName;
2534+
2535+
// Check if the file is .cts or .cjs (CommonJS-specific extensions)
2536+
if (fileExtensionIsOneOf(fileName, [Extension.Cts, Extension.Cjs])) {
2537+
return Diagnostics.ECMAScript_imports_and_exports_cannot_be_written_in_a_CommonJS_file_under_verbatimModuleSyntax;
2538+
}
2539+
else {
2540+
// For .ts, .tsx, .js, etc.
2541+
return Diagnostics.ECMAScript_imports_and_exports_cannot_be_written_in_a_CommonJS_file_under_verbatimModuleSyntax_Adjust_the_type_field_in_the_nearest_package_json_to_make_this_file_an_ECMAScript_module_or_adjust_your_verbatimModuleSyntax_module_and_moduleResolution_settings_in_TypeScript;
2542+
}
2543+
}
2544+
25312545
function addErrorOrSuggestion(isError: boolean, diagnostic: Diagnostic) {
25322546
if (isError) {
25332547
diagnostics.add(diagnostic);
@@ -6156,7 +6170,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
61566170
verbosityLevel?: number,
61576171
out?: WriterContextOut,
61586172
): string {
6159-
const noTruncation = compilerOptions.noErrorTruncation ||
6173+
const noTruncation = !maximumLength && compilerOptions.noErrorTruncation ||
61606174
flags & TypeFormatFlags.NoTruncation;
61616175
const typeNode = nodeBuilder.typeToTypeNode(
61626176
type,
@@ -32741,6 +32755,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3274132755

3274232756
function popContextualType() {
3274332757
contextualTypeCount--;
32758+
// Clear out the popped element's referenced objects.
32759+
contextualTypeNodes[contextualTypeCount] = undefined!;
32760+
contextualTypes[contextualTypeCount] = undefined;
32761+
contextualIsCache[contextualTypeCount] = undefined!;
3274432762
}
3274532763

3274632764
function findContextualNode(node: Node, includeCaches: boolean) {
@@ -32760,6 +32778,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3276032778

3276132779
function popInferenceContext() {
3276232780
inferenceContextCount--;
32781+
inferenceContextNodes[inferenceContextCount] = undefined!;
32782+
inferenceContexts[inferenceContextCount] = undefined;
3276332783
}
3276432784

3276532785
function getInferenceContext(node: Node) {
@@ -32772,12 +32792,15 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3277232792

3277332793
function pushActiveMapper(mapper: TypeMapper) {
3277432794
activeTypeMappers[activeTypeMappersCount] = mapper;
32775-
activeTypeMappersCaches[activeTypeMappersCount] = new Map();
32795+
activeTypeMappersCaches[activeTypeMappersCount] ??= new Map();
3277632796
activeTypeMappersCount++;
3277732797
}
3277832798

3277932799
function popActiveMapper() {
3278032800
activeTypeMappersCount--;
32801+
// Clear out the popped element's referenced objects.
32802+
activeTypeMappers[activeTypeMappersCount] = undefined!;
32803+
activeTypeMappersCaches[activeTypeMappersCount].clear();
3278132804
}
3278232805

3278332806
function findActiveMapper(mapper: TypeMapper) {
@@ -48316,7 +48339,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4831648339
!isInJSFile(node) &&
4831748340
host.getEmitModuleFormatOfFile(getSourceFileOfNode(node)) === ModuleKind.CommonJS
4831848341
) {
48319-
error(node, Diagnostics.ESM_syntax_is_not_allowed_in_a_CommonJS_module_when_verbatimModuleSyntax_is_enabled);
48342+
error(node, getVerbatimModuleSyntaxErrorMessage(node));
4832048343
}
4832148344
else if (
4832248345
moduleKind === ModuleKind.Preserve &&
@@ -48328,7 +48351,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4832848351
// when we look at the `impliedNodeFormat` of this file and decide it's CommonJS (i.e., currently,
4832948352
// only if the file extension is .cjs/.cts). To avoid that inconsistency, we disallow ESM syntax
4833048353
// in files that are unambiguously CommonJS in this mode.
48331-
error(node, Diagnostics.ESM_syntax_is_not_allowed_in_a_CommonJS_module_when_module_is_set_to_preserve);
48354+
error(node, Diagnostics.ECMAScript_module_syntax_is_not_allowed_in_a_CommonJS_module_when_module_is_set_to_preserve);
4833248355
}
4833348356

4833448357
if (
@@ -48760,7 +48783,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4876048783
}
4876148784

4876248785
if (isIllegalExportDefaultInCJS) {
48763-
error(node, Diagnostics.ESM_syntax_is_not_allowed_in_a_CommonJS_module_when_verbatimModuleSyntax_is_enabled);
48786+
error(node, getVerbatimModuleSyntaxErrorMessage(node));
4876448787
}
4876548788

4876648789
checkExternalModuleExports(container);
@@ -53325,7 +53348,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
5332553348

5332653349
function checkGrammarImportCallExpression(node: ImportCall): boolean {
5332753350
if (compilerOptions.verbatimModuleSyntax && moduleKind === ModuleKind.CommonJS) {
53328-
return grammarErrorOnNode(node, Diagnostics.ESM_syntax_is_not_allowed_in_a_CommonJS_module_when_verbatimModuleSyntax_is_enabled);
53351+
return grammarErrorOnNode(node, getVerbatimModuleSyntaxErrorMessage(node));
5332953352
}
5333053353

5333153354
if (node.expression.kind === SyntaxKind.MetaProperty) {

src/compiler/diagnosticMessages.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,7 @@
939939
"category": "Error",
940940
"code": 1285
941941
},
942-
"ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.": {
942+
"ECMAScript imports and exports cannot be written in a CommonJS file under 'verbatimModuleSyntax'.": {
943943
"category": "Error",
944944
"code": 1286
945945
},
@@ -967,14 +967,18 @@
967967
"category": "Error",
968968
"code": 1292
969969
},
970-
"ESM syntax is not allowed in a CommonJS module when 'module' is set to 'preserve'.": {
970+
"ECMAScript module syntax is not allowed in a CommonJS module when 'module' is set to 'preserve'.": {
971971
"category": "Error",
972972
"code": 1293
973973
},
974974
"This syntax is not allowed when 'erasableSyntaxOnly' is enabled.": {
975975
"category": "Error",
976976
"code": 1294
977977
},
978+
"ECMAScript imports and exports cannot be written in a CommonJS file under 'verbatimModuleSyntax'. Adjust the 'type' field in the nearest 'package.json' to make this file an ECMAScript module, or adjust your 'verbatimModuleSyntax', 'module', and 'moduleResolution' settings in TypeScript.": {
979+
"category": "Error",
980+
"code": 1295
981+
},
978982
"'with' statements are not allowed in an async function block.": {
979983
"category": "Error",
980984
"code": 1300

0 commit comments

Comments
 (0)