Skip to content

Commit be86783

Browse files
Give more specific errors for verbatimModuleSyntax (#62113)
1 parent 22ef577 commit be86783

10 files changed

+73
-55
lines changed

src/compiler/checker.ts

Lines changed: 18 additions & 4 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);
@@ -48325,7 +48339,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4832548339
!isInJSFile(node) &&
4832648340
host.getEmitModuleFormatOfFile(getSourceFileOfNode(node)) === ModuleKind.CommonJS
4832748341
) {
48328-
error(node, Diagnostics.ESM_syntax_is_not_allowed_in_a_CommonJS_module_when_verbatimModuleSyntax_is_enabled);
48342+
error(node, getVerbatimModuleSyntaxErrorMessage(node));
4832948343
}
4833048344
else if (
4833148345
moduleKind === ModuleKind.Preserve &&
@@ -48337,7 +48351,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4833748351
// when we look at the `impliedNodeFormat` of this file and decide it's CommonJS (i.e., currently,
4833848352
// only if the file extension is .cjs/.cts). To avoid that inconsistency, we disallow ESM syntax
4833948353
// in files that are unambiguously CommonJS in this mode.
48340-
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);
4834148355
}
4834248356

4834348357
if (
@@ -48769,7 +48783,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4876948783
}
4877048784

4877148785
if (isIllegalExportDefaultInCJS) {
48772-
error(node, Diagnostics.ESM_syntax_is_not_allowed_in_a_CommonJS_module_when_verbatimModuleSyntax_is_enabled);
48786+
error(node, getVerbatimModuleSyntaxErrorMessage(node));
4877348787
}
4877448788

4877548789
checkExternalModuleExports(container);
@@ -53334,7 +53348,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
5333453348

5333553349
function checkGrammarImportCallExpression(node: ImportCall): boolean {
5333653350
if (compilerOptions.verbatimModuleSyntax && moduleKind === ModuleKind.CommonJS) {
53337-
return grammarErrorOnNode(node, Diagnostics.ESM_syntax_is_not_allowed_in_a_CommonJS_module_when_verbatimModuleSyntax_is_enabled);
53351+
return grammarErrorOnNode(node, getVerbatimModuleSyntaxErrorMessage(node));
5333853352
}
5333953353

5334053354
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

tests/baselines/reference/isolatedModulesShadowGlobalTypeNotValue(isolatedmodules=false,verbatimmodulesyntax=true).errors.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
bad.ts(1,10): error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
1+
bad.ts(1,10): error TS1295: 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.
22
bad.ts(1,10): error TS1484: 'Date' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.
3-
bad.ts(1,16): error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
3+
bad.ts(1,16): error TS1295: 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.
44
bad.ts(1,16): error TS1484: 'Event' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.
5-
good.ts(2,10): error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
5+
good.ts(2,10): error TS1295: 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.
66

77

88
==== ./types.ts (0 errors) ====
@@ -36,11 +36,11 @@ good.ts(2,10): error TS1286: ESM syntax is not allowed in a CommonJS module when
3636
==== ./bad.ts (4 errors) ====
3737
import { Date, Event } from './types';
3838
~~~~
39-
!!! error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
39+
!!! error TS1295: 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.
4040
~~~~
4141
!!! error TS1484: 'Date' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.
4242
~~~~~
43-
!!! error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
43+
!!! error TS1295: 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.
4444
~~~~~
4545
!!! error TS1484: 'Event' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.
4646
function foo(a: Date) {
@@ -55,7 +55,7 @@ good.ts(2,10): error TS1286: ESM syntax is not allowed in a CommonJS module when
5555
import type { Date, Event } from './types';
5656
import { Console } from 'node:console';
5757
~~~~~~~
58-
!!! error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
58+
!!! error TS1295: 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.
5959
function foo(a: Date) {
6060
const b = new Date(a.year, a.month, a.day);
6161
return b.getTime();

tests/baselines/reference/isolatedModulesShadowGlobalTypeNotValue(isolatedmodules=true,verbatimmodulesyntax=true).errors.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
bad.ts(1,10): error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
1+
bad.ts(1,10): error TS1295: 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.
22
bad.ts(1,10): error TS1484: 'Date' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.
33
bad.ts(1,10): error TS2866: Import 'Date' conflicts with global value used in this file, so must be declared with a type-only import when 'isolatedModules' is enabled.
4-
bad.ts(1,16): error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
4+
bad.ts(1,16): error TS1295: 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.
55
bad.ts(1,16): error TS1484: 'Event' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.
66
bad.ts(1,16): error TS2866: Import 'Event' conflicts with global value used in this file, so must be declared with a type-only import when 'isolatedModules' is enabled.
7-
good.ts(2,10): error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
7+
good.ts(2,10): error TS1295: 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.
88

99

1010
==== ./types.ts (0 errors) ====
@@ -38,13 +38,13 @@ good.ts(2,10): error TS1286: ESM syntax is not allowed in a CommonJS module when
3838
==== ./bad.ts (6 errors) ====
3939
import { Date, Event } from './types';
4040
~~~~
41-
!!! error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
41+
!!! error TS1295: 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.
4242
~~~~
4343
!!! error TS1484: 'Date' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.
4444
~~~~
4545
!!! error TS2866: Import 'Date' conflicts with global value used in this file, so must be declared with a type-only import when 'isolatedModules' is enabled.
4646
~~~~~
47-
!!! error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
47+
!!! error TS1295: 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.
4848
~~~~~
4949
!!! error TS1484: 'Event' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.
5050
~~~~~
@@ -61,7 +61,7 @@ good.ts(2,10): error TS1286: ESM syntax is not allowed in a CommonJS module when
6161
import type { Date, Event } from './types';
6262
import { Console } from 'node:console';
6363
~~~~~~~
64-
!!! error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
64+
!!! error TS1295: 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.
6565
function foo(a: Date) {
6666
const b = new Date(a.year, a.month, a.day);
6767
return b.getTime();

tests/baselines/reference/isolatedModulesSketchyAliasLocalMerge(isolatedmodules=false,verbatimmodulesyntax=true).errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
bad.ts(1,10): error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
1+
bad.ts(1,10): error TS1295: 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.
22
bad.ts(1,10): error TS1484: 'FC' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.
33

44

@@ -8,7 +8,7 @@ bad.ts(1,10): error TS1484: 'FC' is a type and must be imported using a type-onl
88
==== bad.ts (2 errors) ====
99
import { FC } from "./types";
1010
~~
11-
!!! error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
11+
!!! error TS1295: 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.
1212
~~
1313
!!! error TS1484: 'FC' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.
1414
let FC: FC | null = null;

tests/baselines/reference/isolatedModulesSketchyAliasLocalMerge(isolatedmodules=true,verbatimmodulesyntax=true).errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
bad.ts(1,10): error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
1+
bad.ts(1,10): error TS1295: 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.
22
bad.ts(1,10): error TS1484: 'FC' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.
33
bad.ts(1,10): error TS2865: Import 'FC' conflicts with local value, so must be declared with a type-only import when 'isolatedModules' is enabled.
44

@@ -9,7 +9,7 @@ bad.ts(1,10): error TS2865: Import 'FC' conflicts with local value, so must be d
99
==== bad.ts (3 errors) ====
1010
import { FC } from "./types";
1111
~~
12-
!!! error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
12+
!!! error TS1295: 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.
1313
~~
1414
!!! error TS1484: 'FC' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.
1515
~~

0 commit comments

Comments
 (0)