mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-03-15 14:05:47 -05:00
Update error messages for CJS imports resolving to ES modules (#50088)
* Update error messages for CJS imports resolving to ES modules * Update error message * Use package scope from source file * Update baselines * Issue error for JSX/TSX files * Switch from related info to message chain
This commit is contained in:
@@ -3576,7 +3576,51 @@ namespace ts {
|
||||
// An override clause will take effect for type-only imports and import types, and allows importing the types across formats, regardless of
|
||||
// normal mode restrictions
|
||||
if (isSyncImport && sourceFile.impliedNodeFormat === ModuleKind.ESNext && !getResolutionModeOverrideForClause(overrideClause)) {
|
||||
error(errorNode, Diagnostics.Module_0_cannot_be_imported_using_this_construct_The_specifier_only_resolves_to_an_ES_module_which_cannot_be_imported_synchronously_Use_dynamic_import_instead, moduleReference);
|
||||
if (findAncestor(location, isImportEqualsDeclaration)) {
|
||||
// ImportEquals in a ESM file resolving to another ESM file
|
||||
error(errorNode, Diagnostics.Module_0_cannot_be_imported_using_this_construct_The_specifier_only_resolves_to_an_ES_module_which_cannot_be_imported_with_require_Use_an_ECMAScript_import_instead, moduleReference);
|
||||
}
|
||||
else {
|
||||
// CJS file resolving to an ESM file
|
||||
let diagnosticDetails;
|
||||
const ext = tryGetExtensionFromPath(currentSourceFile.fileName);
|
||||
if (ext === Extension.Ts || ext === Extension.Js || ext === Extension.Tsx || ext === Extension.Jsx) {
|
||||
const scope = currentSourceFile.packageJsonScope;
|
||||
const targetExt = ext === Extension.Ts ? Extension.Mts : ext === Extension.Js ? Extension.Mjs : undefined;
|
||||
if (scope && !scope.packageJsonContent.type) {
|
||||
if (targetExt) {
|
||||
diagnosticDetails = chainDiagnosticMessages(
|
||||
/*details*/ undefined,
|
||||
Diagnostics.To_convert_this_file_to_an_ECMAScript_module_change_its_file_extension_to_0_or_add_the_field_type_Colon_module_to_1,
|
||||
targetExt,
|
||||
combinePaths(scope.packageDirectory, "package.json"));
|
||||
}
|
||||
else {
|
||||
diagnosticDetails = chainDiagnosticMessages(
|
||||
/*details*/ undefined,
|
||||
Diagnostics.To_convert_this_file_to_an_ECMAScript_module_add_the_field_type_Colon_module_to_0,
|
||||
combinePaths(scope.packageDirectory, "package.json"));
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (targetExt) {
|
||||
diagnosticDetails = chainDiagnosticMessages(
|
||||
/*details*/ undefined,
|
||||
Diagnostics.To_convert_this_file_to_an_ECMAScript_module_change_its_file_extension_to_0_or_create_a_local_package_json_file_with_type_Colon_module,
|
||||
targetExt);
|
||||
}
|
||||
else {
|
||||
diagnosticDetails = chainDiagnosticMessages(
|
||||
/*details*/ undefined,
|
||||
Diagnostics.To_convert_this_file_to_an_ECMAScript_module_create_a_local_package_json_file_with_type_Colon_module);
|
||||
}
|
||||
}
|
||||
}
|
||||
diagnostics.add(createDiagnosticForNodeFromMessageChain(errorNode, chainDiagnosticMessages(
|
||||
diagnosticDetails,
|
||||
Diagnostics.The_current_file_is_a_CommonJS_module_whose_imports_will_produce_require_calls_however_the_referenced_file_is_an_ECMAScript_module_and_cannot_be_imported_with_require_Consider_writing_a_dynamic_import_0_call_instead,
|
||||
moduleReference)));
|
||||
}
|
||||
}
|
||||
}
|
||||
// merged symbol is module declaration symbol combined with all augmentations
|
||||
|
||||
@@ -1485,7 +1485,7 @@
|
||||
"category": "Error",
|
||||
"code": 1470
|
||||
},
|
||||
"Module '{0}' cannot be imported using this construct. The specifier only resolves to an ES module, which cannot be imported synchronously. Use dynamic import instead.": {
|
||||
"Module '{0}' cannot be imported using this construct. The specifier only resolves to an ES module, which cannot be imported with 'require'. Use an ECMAScript import instead.": {
|
||||
"category": "Error",
|
||||
"code": 1471
|
||||
},
|
||||
@@ -1517,6 +1517,26 @@
|
||||
"category": "Error",
|
||||
"code": 1478
|
||||
},
|
||||
"The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import(\"{0}\")' call instead.": {
|
||||
"category": "Error",
|
||||
"code": 1479
|
||||
},
|
||||
"To convert this file to an ECMAScript module, change its file extension to '{0}' or create a local package.json file with `{ \"type\": \"module\" }`.": {
|
||||
"category": "Message",
|
||||
"code": 1480
|
||||
},
|
||||
"To convert this file to an ECMAScript module, change its file extension to '{0}', or add the field `\"type\": \"module\"` to '{1}'.": {
|
||||
"category": "Message",
|
||||
"code": 1481
|
||||
},
|
||||
"To convert this file to an ECMAScript module, add the field `\"type\": \"module\"` to '{0}'.": {
|
||||
"category": "Message",
|
||||
"code": 1482
|
||||
},
|
||||
"To convert this file to an ECMAScript module, create a local package.json file with `{ \"type\": \"module\" }`.": {
|
||||
"category": "Message",
|
||||
"code": 1483
|
||||
},
|
||||
|
||||
"The types of '{0}' are incompatible between these types.": {
|
||||
"category": "Error",
|
||||
|
||||
Reference in New Issue
Block a user