diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index aabb6dd9301fc..77f35376da785 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -2528,6 +2528,20 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { return diagnostic; } + function getVerbatimModuleSyntaxErrorMessage(node: Node): DiagnosticMessage { + const sourceFile = getSourceFileOfNode(node); + const fileName = sourceFile.fileName; + + // Check if the file is .cts or .cjs (CommonJS-specific extensions) + if (fileExtensionIsOneOf(fileName, [Extension.Cts, Extension.Cjs])) { + return Diagnostics.ECMAScript_imports_and_exports_cannot_be_written_in_a_CommonJS_file_under_verbatimModuleSyntax; + } + else { + // For .ts, .tsx, .js, etc. + 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; + } + } + function addErrorOrSuggestion(isError: boolean, diagnostic: Diagnostic) { if (isError) { diagnostics.add(diagnostic); @@ -48325,7 +48339,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { !isInJSFile(node) && host.getEmitModuleFormatOfFile(getSourceFileOfNode(node)) === ModuleKind.CommonJS ) { - error(node, Diagnostics.ESM_syntax_is_not_allowed_in_a_CommonJS_module_when_verbatimModuleSyntax_is_enabled); + error(node, getVerbatimModuleSyntaxErrorMessage(node)); } else if ( moduleKind === ModuleKind.Preserve && @@ -48337,7 +48351,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { // when we look at the `impliedNodeFormat` of this file and decide it's CommonJS (i.e., currently, // only if the file extension is .cjs/.cts). To avoid that inconsistency, we disallow ESM syntax // in files that are unambiguously CommonJS in this mode. - error(node, Diagnostics.ESM_syntax_is_not_allowed_in_a_CommonJS_module_when_module_is_set_to_preserve); + error(node, Diagnostics.ECMAScript_module_syntax_is_not_allowed_in_a_CommonJS_module_when_module_is_set_to_preserve); } if ( @@ -48769,7 +48783,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { } if (isIllegalExportDefaultInCJS) { - error(node, Diagnostics.ESM_syntax_is_not_allowed_in_a_CommonJS_module_when_verbatimModuleSyntax_is_enabled); + error(node, getVerbatimModuleSyntaxErrorMessage(node)); } checkExternalModuleExports(container); @@ -53334,7 +53348,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { function checkGrammarImportCallExpression(node: ImportCall): boolean { if (compilerOptions.verbatimModuleSyntax && moduleKind === ModuleKind.CommonJS) { - return grammarErrorOnNode(node, Diagnostics.ESM_syntax_is_not_allowed_in_a_CommonJS_module_when_verbatimModuleSyntax_is_enabled); + return grammarErrorOnNode(node, getVerbatimModuleSyntaxErrorMessage(node)); } if (node.expression.kind === SyntaxKind.MetaProperty) { diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 163de688f16fe..0e60926c93d50 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -939,7 +939,7 @@ "category": "Error", "code": 1285 }, - "ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.": { + "ECMAScript imports and exports cannot be written in a CommonJS file under 'verbatimModuleSyntax'.": { "category": "Error", "code": 1286 }, @@ -967,7 +967,7 @@ "category": "Error", "code": 1292 }, - "ESM syntax is not allowed in a CommonJS module when 'module' is set to 'preserve'.": { + "ECMAScript module syntax is not allowed in a CommonJS module when 'module' is set to 'preserve'.": { "category": "Error", "code": 1293 }, @@ -975,6 +975,10 @@ "category": "Error", "code": 1294 }, + "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.": { + "category": "Error", + "code": 1295 + }, "'with' statements are not allowed in an async function block.": { "category": "Error", "code": 1300 diff --git a/tests/baselines/reference/isolatedModulesShadowGlobalTypeNotValue(isolatedmodules=false,verbatimmodulesyntax=true).errors.txt b/tests/baselines/reference/isolatedModulesShadowGlobalTypeNotValue(isolatedmodules=false,verbatimmodulesyntax=true).errors.txt index 09ca32e0905e6..9e79fd15e8698 100644 --- a/tests/baselines/reference/isolatedModulesShadowGlobalTypeNotValue(isolatedmodules=false,verbatimmodulesyntax=true).errors.txt +++ b/tests/baselines/reference/isolatedModulesShadowGlobalTypeNotValue(isolatedmodules=false,verbatimmodulesyntax=true).errors.txt @@ -1,8 +1,8 @@ -bad.ts(1,10): error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled. +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. bad.ts(1,10): error TS1484: 'Date' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled. -bad.ts(1,16): error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled. +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. bad.ts(1,16): error TS1484: 'Event' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled. -good.ts(2,10): error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled. +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. ==== ./types.ts (0 errors) ==== @@ -36,11 +36,11 @@ good.ts(2,10): error TS1286: ESM syntax is not allowed in a CommonJS module when ==== ./bad.ts (4 errors) ==== import { Date, Event } from './types'; ~~~~ -!!! error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled. +!!! 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. ~~~~ !!! error TS1484: 'Date' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled. ~~~~~ -!!! error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled. +!!! 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. ~~~~~ !!! error TS1484: 'Event' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled. function foo(a: Date) { @@ -55,7 +55,7 @@ good.ts(2,10): error TS1286: ESM syntax is not allowed in a CommonJS module when import type { Date, Event } from './types'; import { Console } from 'node:console'; ~~~~~~~ -!!! error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled. +!!! 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. function foo(a: Date) { const b = new Date(a.year, a.month, a.day); return b.getTime(); diff --git a/tests/baselines/reference/isolatedModulesShadowGlobalTypeNotValue(isolatedmodules=true,verbatimmodulesyntax=true).errors.txt b/tests/baselines/reference/isolatedModulesShadowGlobalTypeNotValue(isolatedmodules=true,verbatimmodulesyntax=true).errors.txt index deb401b76857d..5a21b8780e6f7 100644 --- a/tests/baselines/reference/isolatedModulesShadowGlobalTypeNotValue(isolatedmodules=true,verbatimmodulesyntax=true).errors.txt +++ b/tests/baselines/reference/isolatedModulesShadowGlobalTypeNotValue(isolatedmodules=true,verbatimmodulesyntax=true).errors.txt @@ -1,10 +1,10 @@ -bad.ts(1,10): error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled. +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. bad.ts(1,10): error TS1484: 'Date' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled. 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. -bad.ts(1,16): error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled. +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. bad.ts(1,16): error TS1484: 'Event' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled. 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. -good.ts(2,10): error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled. +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. ==== ./types.ts (0 errors) ==== @@ -38,13 +38,13 @@ good.ts(2,10): error TS1286: ESM syntax is not allowed in a CommonJS module when ==== ./bad.ts (6 errors) ==== import { Date, Event } from './types'; ~~~~ -!!! error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled. +!!! 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. ~~~~ !!! error TS1484: 'Date' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled. ~~~~ !!! 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. ~~~~~ -!!! error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled. +!!! 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. ~~~~~ !!! error TS1484: 'Event' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled. ~~~~~ @@ -61,7 +61,7 @@ good.ts(2,10): error TS1286: ESM syntax is not allowed in a CommonJS module when import type { Date, Event } from './types'; import { Console } from 'node:console'; ~~~~~~~ -!!! error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled. +!!! 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. function foo(a: Date) { const b = new Date(a.year, a.month, a.day); return b.getTime(); diff --git a/tests/baselines/reference/isolatedModulesSketchyAliasLocalMerge(isolatedmodules=false,verbatimmodulesyntax=true).errors.txt b/tests/baselines/reference/isolatedModulesSketchyAliasLocalMerge(isolatedmodules=false,verbatimmodulesyntax=true).errors.txt index 6d699b7aaea0b..fda0014e040b0 100644 --- a/tests/baselines/reference/isolatedModulesSketchyAliasLocalMerge(isolatedmodules=false,verbatimmodulesyntax=true).errors.txt +++ b/tests/baselines/reference/isolatedModulesSketchyAliasLocalMerge(isolatedmodules=false,verbatimmodulesyntax=true).errors.txt @@ -1,4 +1,4 @@ -bad.ts(1,10): error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled. +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. bad.ts(1,10): error TS1484: 'FC' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled. @@ -8,7 +8,7 @@ bad.ts(1,10): error TS1484: 'FC' is a type and must be imported using a type-onl ==== bad.ts (2 errors) ==== import { FC } from "./types"; ~~ -!!! error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled. +!!! 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. ~~ !!! error TS1484: 'FC' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled. let FC: FC | null = null; diff --git a/tests/baselines/reference/isolatedModulesSketchyAliasLocalMerge(isolatedmodules=true,verbatimmodulesyntax=true).errors.txt b/tests/baselines/reference/isolatedModulesSketchyAliasLocalMerge(isolatedmodules=true,verbatimmodulesyntax=true).errors.txt index f87ec7ea8fc17..ac7fea081ab9f 100644 --- a/tests/baselines/reference/isolatedModulesSketchyAliasLocalMerge(isolatedmodules=true,verbatimmodulesyntax=true).errors.txt +++ b/tests/baselines/reference/isolatedModulesSketchyAliasLocalMerge(isolatedmodules=true,verbatimmodulesyntax=true).errors.txt @@ -1,4 +1,4 @@ -bad.ts(1,10): error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled. +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. bad.ts(1,10): error TS1484: 'FC' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled. 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. @@ -9,7 +9,7 @@ bad.ts(1,10): error TS2865: Import 'FC' conflicts with local value, so must be d ==== bad.ts (3 errors) ==== import { FC } from "./types"; ~~ -!!! error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled. +!!! 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. ~~ !!! error TS1484: 'FC' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled. ~~ diff --git a/tests/baselines/reference/modulePreserve4.errors.txt b/tests/baselines/reference/modulePreserve4.errors.txt index 65e21aa749b3c..33856c3e7d1fa 100644 --- a/tests/baselines/reference/modulePreserve4.errors.txt +++ b/tests/baselines/reference/modulePreserve4.errors.txt @@ -1,5 +1,5 @@ /a.js(2,1): error TS2580: Cannot find name 'module'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node`. -/f.cts(1,1): error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled. +/f.cts(1,1): error TS1286: ECMAScript imports and exports cannot be written in a CommonJS file under 'verbatimModuleSyntax'. /main1.ts(1,13): error TS2305: Module '"./a"' has no exported member 'y'. /main1.ts(3,12): error TS2580: Cannot find name 'require'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node`. /main1.ts(19,4): error TS2339: Property 'default' does not exist on type '() => void'. @@ -8,15 +8,15 @@ /main2.mts(4,4): error TS2339: Property 'default' does not exist on type 'typeof import("/a")'. /main2.mts(5,12): error TS2580: Cannot find name 'require'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node`. /main2.mts(14,8): error TS1192: Module '"/e"' has no default export. -/main3.cjs(1,10): error TS1293: ESM syntax is not allowed in a CommonJS module when 'module' is set to 'preserve'. +/main3.cjs(1,10): error TS1293: ECMAScript module syntax is not allowed in a CommonJS module when 'module' is set to 'preserve'. /main3.cjs(1,13): error TS2305: Module '"./a"' has no exported member 'y'. /main3.cjs(2,1): error TS8002: 'import ... =' can only be used in TypeScript files. -/main3.cjs(5,8): error TS1293: ESM syntax is not allowed in a CommonJS module when 'module' is set to 'preserve'. -/main3.cjs(8,8): error TS1293: ESM syntax is not allowed in a CommonJS module when 'module' is set to 'preserve'. -/main3.cjs(10,8): error TS1293: ESM syntax is not allowed in a CommonJS module when 'module' is set to 'preserve'. -/main3.cjs(12,8): error TS1293: ESM syntax is not allowed in a CommonJS module when 'module' is set to 'preserve'. -/main3.cjs(14,8): error TS1293: ESM syntax is not allowed in a CommonJS module when 'module' is set to 'preserve'. -/main3.cjs(17,8): error TS1293: ESM syntax is not allowed in a CommonJS module when 'module' is set to 'preserve'. +/main3.cjs(5,8): error TS1293: ECMAScript module syntax is not allowed in a CommonJS module when 'module' is set to 'preserve'. +/main3.cjs(8,8): error TS1293: ECMAScript module syntax is not allowed in a CommonJS module when 'module' is set to 'preserve'. +/main3.cjs(10,8): error TS1293: ECMAScript module syntax is not allowed in a CommonJS module when 'module' is set to 'preserve'. +/main3.cjs(12,8): error TS1293: ECMAScript module syntax is not allowed in a CommonJS module when 'module' is set to 'preserve'. +/main3.cjs(14,8): error TS1293: ECMAScript module syntax is not allowed in a CommonJS module when 'module' is set to 'preserve'. +/main3.cjs(17,8): error TS1293: ECMAScript module syntax is not allowed in a CommonJS module when 'module' is set to 'preserve'. ==== /a.js (1 errors) ==== @@ -42,7 +42,7 @@ ==== /f.cts (1 errors) ==== export default 0; ~~~~~~~~~~~~~~~~~ -!!! error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled. +!!! error TS1286: ECMAScript imports and exports cannot be written in a CommonJS file under 'verbatimModuleSyntax'. ==== /g.js (0 errors) ==== exports.default = 0; @@ -122,7 +122,7 @@ ==== /main3.cjs (9 errors) ==== import { x, y } from "./a"; // No y ~ -!!! error TS1293: ESM syntax is not allowed in a CommonJS module when 'module' is set to 'preserve'. +!!! error TS1293: ECMAScript module syntax is not allowed in a CommonJS module when 'module' is set to 'preserve'. ~ !!! error TS2305: Module '"./a"' has no exported member 'y'. import a1 = require("./a"); // Error in JS @@ -132,29 +132,29 @@ import b1 from "./b"; // 0 ~~ -!!! error TS1293: ESM syntax is not allowed in a CommonJS module when 'module' is set to 'preserve'. +!!! error TS1293: ECMAScript module syntax is not allowed in a CommonJS module when 'module' is set to 'preserve'. const b2 = require("./b"); // { default: 0 } import c1 from "./c"; // { default: [Function: default] } ~~ -!!! error TS1293: ESM syntax is not allowed in a CommonJS module when 'module' is set to 'preserve'. +!!! error TS1293: ECMAScript module syntax is not allowed in a CommonJS module when 'module' is set to 'preserve'. const c2 = require("./c"); // { default: [Function: default] } import d1 from "./d"; // [Function: default] ~~ -!!! error TS1293: ESM syntax is not allowed in a CommonJS module when 'module' is set to 'preserve'. +!!! error TS1293: ECMAScript module syntax is not allowed in a CommonJS module when 'module' is set to 'preserve'. const d2 = require("./d"); // [Function: default] import e1 from "./e.mjs"; // 0 ~~ -!!! error TS1293: ESM syntax is not allowed in a CommonJS module when 'module' is set to 'preserve'. +!!! error TS1293: ECMAScript module syntax is not allowed in a CommonJS module when 'module' is set to 'preserve'. const e2 = require("./e.mjs"); // 0 import f1 from "./f.cjs"; // 0 ~~ -!!! error TS1293: ESM syntax is not allowed in a CommonJS module when 'module' is set to 'preserve'. +!!! error TS1293: ECMAScript module syntax is not allowed in a CommonJS module when 'module' is set to 'preserve'. const f2 = require("./f.cjs"); // { default: 0 } import g1 from "./g"; // { default: 0 } ~~ -!!! error TS1293: ESM syntax is not allowed in a CommonJS module when 'module' is set to 'preserve'. +!!! error TS1293: ECMAScript module syntax is not allowed in a CommonJS module when 'module' is set to 'preserve'. const g2 = require("./g"); // { default: 0 } ==== /main4.cjs (0 errors) ==== diff --git a/tests/baselines/reference/noCrashWithVerbatimModuleSyntaxAndImportsNotUsedAsValues.errors.txt b/tests/baselines/reference/noCrashWithVerbatimModuleSyntaxAndImportsNotUsedAsValues.errors.txt index 0455a747a91a9..494c5e4616ddd 100644 --- a/tests/baselines/reference/noCrashWithVerbatimModuleSyntaxAndImportsNotUsedAsValues.errors.txt +++ b/tests/baselines/reference/noCrashWithVerbatimModuleSyntaxAndImportsNotUsedAsValues.errors.txt @@ -1,7 +1,7 @@ error TS5102: Option 'importsNotUsedAsValues' has been removed. Please remove it from your configuration. Use 'verbatimModuleSyntax' instead. file.ts(1,1): error TS1287: A top-level 'export' modifier cannot be used on value declarations in a CommonJS module when 'verbatimModuleSyntax' is enabled. -index.ts(1,9): error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled. +index.ts(1,9): 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. !!! error TS5102: Option 'importsNotUsedAsValues' has been removed. Please remove it from your configuration. @@ -13,6 +13,6 @@ index.ts(1,9): error TS1286: ESM syntax is not allowed in a CommonJS module when ==== index.ts (1 errors) ==== import {A} from "./file"; ~ -!!! error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled. +!!! 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. const a: A = null as any; \ No newline at end of file diff --git a/tests/baselines/reference/tscWatch/programUpdates/updates-errors-and-emit-when-verbatimModuleSyntax-changes.js b/tests/baselines/reference/tscWatch/programUpdates/updates-errors-and-emit-when-verbatimModuleSyntax-changes.js index 65a1365ab633f..ca61deb9e9bcf 100644 --- a/tests/baselines/reference/tscWatch/programUpdates/updates-errors-and-emit-when-verbatimModuleSyntax-changes.js +++ b/tests/baselines/reference/tscWatch/programUpdates/updates-errors-and-emit-when-verbatimModuleSyntax-changes.js @@ -131,7 +131,7 @@ Output:: 1 export class C {}   ~~~~~~ -b.ts:1:9 - error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled. +b.ts:1:9 - 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. 1 import {C} from './a';    ~ diff --git a/tests/baselines/reference/verbatimModuleSyntaxRestrictionsCJS.errors.txt b/tests/baselines/reference/verbatimModuleSyntaxRestrictionsCJS.errors.txt index 9bcb8b0214e39..59913037165d0 100644 --- a/tests/baselines/reference/verbatimModuleSyntaxRestrictionsCJS.errors.txt +++ b/tests/baselines/reference/verbatimModuleSyntaxRestrictionsCJS.errors.txt @@ -1,14 +1,14 @@ -/main.ts(1,8): error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled. -/main.ts(2,13): error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled. -/main.ts(3,10): error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled. -/main.ts(5,1): error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled. +/main.ts(1,8): 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. +/main.ts(2,13): 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. +/main.ts(3,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. +/main.ts(5,1): 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. /main.ts(8,1): error TS1287: A top-level 'export' modifier cannot be used on value declarations in a CommonJS module when 'verbatimModuleSyntax' is enabled. /main.ts(14,1): error TS1287: A top-level 'export' modifier cannot be used on value declarations in a CommonJS module when 'verbatimModuleSyntax' is enabled. /main2.ts(2,1): error TS2309: An export assignment cannot be used in a module with other exported elements. -/main4.ts(1,1): error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled. +/main4.ts(1,1): 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. /main5.ts(1,1): error TS1287: A top-level 'export' modifier cannot be used on value declarations in a CommonJS module when 'verbatimModuleSyntax' is enabled. -/main6.ts(2,1): error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled. -/main7.ts(2,1): error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled. +/main6.ts(2,1): 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. +/main7.ts(2,1): 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. ==== /decl.d.ts (0 errors) ==== @@ -25,17 +25,17 @@ ==== /main.ts (6 errors) ==== import esmy from "./decl"; // error ~~~~ -!!! error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled. +!!! 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. import * as esmy2 from "./decl"; // error ~~~~~ -!!! error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled. +!!! 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. import { funciton } from "./decl"; // error ~~~~~~~~ -!!! error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled. +!!! 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. import type { funciton as funciton2 } from "./decl"; // ok I guess? import("./decl"); // error ~~~~~~~~~~~~~~~~ -!!! error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled. +!!! 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. type T = typeof import("./decl"); // ok export {}; // error export const x = 1; // error @@ -69,7 +69,7 @@ ==== /main4.ts (1 errors) ==== export default 1; // error ~~~~~~~~~~~~~~~~~ -!!! error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled. +!!! 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. ==== /main5.ts (1 errors) ==== export default class C {} // error @@ -80,11 +80,11 @@ interface I {} export default I; // error ~~~~~~~~~~~~~~~~~ -!!! error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled. +!!! 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. ==== /main7.ts (1 errors) ==== import type esmy from "./decl"; export default esmy; // error ~~~~~~~~~~~~~~~~~~~~ -!!! error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled. +!!! 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. \ No newline at end of file