Skip to content

Commit a8f97b4

Browse files
committed
Merge branch 'main' into cacheModuleResolution
2 parents a4e5f88 + bdb8514 commit a8f97b4

File tree

89 files changed

+2068
-836
lines changed

Some content is hidden

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

89 files changed

+2068
-836
lines changed

package-lock.json

Lines changed: 49 additions & 267 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/compiler/checker.ts

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3576,7 +3576,51 @@ namespace ts {
35763576
// An override clause will take effect for type-only imports and import types, and allows importing the types across formats, regardless of
35773577
// normal mode restrictions
35783578
if (isSyncImport && sourceFile.impliedNodeFormat === ModuleKind.ESNext && !getResolutionModeOverrideForClause(overrideClause)) {
3579-
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);
3579+
if (findAncestor(location, isImportEqualsDeclaration)) {
3580+
// ImportEquals in a ESM file resolving to another ESM file
3581+
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);
3582+
}
3583+
else {
3584+
// CJS file resolving to an ESM file
3585+
let diagnosticDetails;
3586+
const ext = tryGetExtensionFromPath(currentSourceFile.fileName);
3587+
if (ext === Extension.Ts || ext === Extension.Js || ext === Extension.Tsx || ext === Extension.Jsx) {
3588+
const scope = currentSourceFile.packageJsonScope;
3589+
const targetExt = ext === Extension.Ts ? Extension.Mts : ext === Extension.Js ? Extension.Mjs : undefined;
3590+
if (scope && !scope.packageJsonContent.type) {
3591+
if (targetExt) {
3592+
diagnosticDetails = chainDiagnosticMessages(
3593+
/*details*/ undefined,
3594+
Diagnostics.To_convert_this_file_to_an_ECMAScript_module_change_its_file_extension_to_0_or_add_the_field_type_Colon_module_to_1,
3595+
targetExt,
3596+
combinePaths(scope.packageDirectory, "package.json"));
3597+
}
3598+
else {
3599+
diagnosticDetails = chainDiagnosticMessages(
3600+
/*details*/ undefined,
3601+
Diagnostics.To_convert_this_file_to_an_ECMAScript_module_add_the_field_type_Colon_module_to_0,
3602+
combinePaths(scope.packageDirectory, "package.json"));
3603+
}
3604+
}
3605+
else {
3606+
if (targetExt) {
3607+
diagnosticDetails = chainDiagnosticMessages(
3608+
/*details*/ undefined,
3609+
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,
3610+
targetExt);
3611+
}
3612+
else {
3613+
diagnosticDetails = chainDiagnosticMessages(
3614+
/*details*/ undefined,
3615+
Diagnostics.To_convert_this_file_to_an_ECMAScript_module_create_a_local_package_json_file_with_type_Colon_module);
3616+
}
3617+
}
3618+
}
3619+
diagnostics.add(createDiagnosticForNodeFromMessageChain(errorNode, chainDiagnosticMessages(
3620+
diagnosticDetails,
3621+
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,
3622+
moduleReference)));
3623+
}
35803624
}
35813625
}
35823626
// merged symbol is module declaration symbol combined with all augmentations
@@ -26777,12 +26821,15 @@ namespace ts {
2677726821
if (contextualReturnType) {
2677826822
const functionFlags = getFunctionFlags(func);
2677926823
if (functionFlags & FunctionFlags.Generator) { // Generator or AsyncGenerator function
26780-
const use = functionFlags & FunctionFlags.Async ? IterationUse.AsyncGeneratorReturnType : IterationUse.GeneratorReturnType;
26781-
const iterationTypes = getIterationTypesOfIterable(contextualReturnType, use, /*errorNode*/ undefined);
26782-
if (!iterationTypes) {
26824+
const isAsyncGenerator = (functionFlags & FunctionFlags.Async) !== 0;
26825+
if (contextualReturnType.flags & TypeFlags.Union) {
26826+
contextualReturnType = filterType(contextualReturnType, type => !!getIterationTypeOfGeneratorFunctionReturnType(IterationTypeKind.Return, type, isAsyncGenerator));
26827+
}
26828+
const iterationReturnType = getIterationTypeOfGeneratorFunctionReturnType(IterationTypeKind.Return, contextualReturnType, (functionFlags & FunctionFlags.Async) !== 0);
26829+
if (!iterationReturnType) {
2678326830
return undefined;
2678426831
}
26785-
contextualReturnType = iterationTypes.returnType;
26832+
contextualReturnType = iterationReturnType;
2678626833
// falls through to unwrap Promise for AsyncGenerators
2678726834
}
2678826835

@@ -26811,11 +26858,15 @@ namespace ts {
2681126858
const func = getContainingFunction(node);
2681226859
if (func) {
2681326860
const functionFlags = getFunctionFlags(func);
26814-
const contextualReturnType = getContextualReturnType(func, contextFlags);
26861+
let contextualReturnType = getContextualReturnType(func, contextFlags);
2681526862
if (contextualReturnType) {
26863+
const isAsyncGenerator = (functionFlags & FunctionFlags.Async) !== 0;
26864+
if (!node.asteriskToken && contextualReturnType.flags & TypeFlags.Union) {
26865+
contextualReturnType = filterType(contextualReturnType, type => !!getIterationTypeOfGeneratorFunctionReturnType(IterationTypeKind.Return, type, isAsyncGenerator));
26866+
}
2681626867
return node.asteriskToken
2681726868
? contextualReturnType
26818-
: getIterationTypeOfGeneratorFunctionReturnType(IterationTypeKind.Yield, contextualReturnType, (functionFlags & FunctionFlags.Async) !== 0);
26869+
: getIterationTypeOfGeneratorFunctionReturnType(IterationTypeKind.Yield, contextualReturnType, isAsyncGenerator);
2681926870
}
2682026871
}
2682126872

src/compiler/diagnosticMessages.json

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1485,7 +1485,7 @@
14851485
"category": "Error",
14861486
"code": 1470
14871487
},
1488-
"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.": {
1488+
"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.": {
14891489
"category": "Error",
14901490
"code": 1471
14911491
},
@@ -1517,6 +1517,26 @@
15171517
"category": "Error",
15181518
"code": 1478
15191519
},
1520+
"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.": {
1521+
"category": "Error",
1522+
"code": 1479
1523+
},
1524+
"To convert this file to an ECMAScript module, change its file extension to '{0}' or create a local package.json file with `{ \"type\": \"module\" }`.": {
1525+
"category": "Message",
1526+
"code": 1480
1527+
},
1528+
"To convert this file to an ECMAScript module, change its file extension to '{0}', or add the field `\"type\": \"module\"` to '{1}'.": {
1529+
"category": "Message",
1530+
"code": 1481
1531+
},
1532+
"To convert this file to an ECMAScript module, add the field `\"type\": \"module\"` to '{0}'.": {
1533+
"category": "Message",
1534+
"code": 1482
1535+
},
1536+
"To convert this file to an ECMAScript module, create a local package.json file with `{ \"type\": \"module\" }`.": {
1537+
"category": "Message",
1538+
"code": 1483
1539+
},
15201540

15211541
"The types of '{0}' are incompatible between these types.": {
15221542
"category": "Error",

src/loc/lcl/chs/diagnosticMessages/diagnosticMessages.generated.json.lcl

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6534,6 +6534,42 @@
65346534
</Str>
65356535
<Disp Icon="Str" />
65366536
</Item>
6537+
<Item ItemId=";File_is_CommonJS_module_because_0_does_not_have_field_type_1460" ItemType="0" PsrId="306" Leaf="true">
6538+
<Str Cat="Text">
6539+
<Val><![CDATA[File is CommonJS module because '{0}' does not have field "type"]]></Val>
6540+
<Tgt Cat="Text" Stat="Loc" Orig="New">
6541+
<Val><![CDATA[文件是 CommonJS 模块,因为“{0}”没有字段 “type”]]></Val>
6542+
</Tgt>
6543+
</Str>
6544+
<Disp Icon="Str" />
6545+
</Item>
6546+
<Item ItemId=";File_is_CommonJS_module_because_0_has_field_type_whose_value_is_not_module_1459" ItemType="0" PsrId="306" Leaf="true">
6547+
<Str Cat="Text">
6548+
<Val><![CDATA[File is CommonJS module because '{0}' has field "type" whose value is not "module"]]></Val>
6549+
<Tgt Cat="Text" Stat="Loc" Orig="New">
6550+
<Val><![CDATA[文件是 CommonJS 模块,因为“{0}”具有值不是 “module” 的字段 “type”]]></Val>
6551+
</Tgt>
6552+
</Str>
6553+
<Disp Icon="Str" />
6554+
</Item>
6555+
<Item ItemId=";File_is_CommonJS_module_because_package_json_was_not_found_1461" ItemType="0" PsrId="306" Leaf="true">
6556+
<Str Cat="Text">
6557+
<Val><![CDATA[File is CommonJS module because 'package.json' was not found]]></Val>
6558+
<Tgt Cat="Text" Stat="Loc" Orig="New">
6559+
<Val><![CDATA[文件是 CommonJS 模块,因为找不到 “package.json”]]></Val>
6560+
</Tgt>
6561+
</Str>
6562+
<Disp Icon="Str" />
6563+
</Item>
6564+
<Item ItemId=";File_is_ECMAScript_module_because_0_has_field_type_with_value_module_1458" ItemType="0" PsrId="306" Leaf="true">
6565+
<Str Cat="Text">
6566+
<Val><![CDATA[File is ECMAScript module because '{0}' has field "type" with value "module"]]></Val>
6567+
<Tgt Cat="Text" Stat="Loc" Orig="New">
6568+
<Val><![CDATA[文件是 ECMAScript 模块,因为“{0}”具有值为 “module” 的字段 “type”]]></Val>
6569+
</Tgt>
6570+
</Str>
6571+
<Disp Icon="Str" />
6572+
</Item>
65376573
<Item ItemId=";File_is_a_CommonJS_module_it_may_be_converted_to_an_ES_module_80001" ItemType="0" PsrId="306" Leaf="true">
65386574
<Str Cat="Text">
65396575
<Val><![CDATA[File is a CommonJS module; it may be converted to an ES module.]]></Val>

src/loc/lcl/cht/diagnosticMessages/diagnosticMessages.generated.json.lcl

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6534,6 +6534,42 @@
65346534
</Str>
65356535
<Disp Icon="Str" />
65366536
</Item>
6537+
<Item ItemId=";File_is_CommonJS_module_because_0_does_not_have_field_type_1460" ItemType="0" PsrId="306" Leaf="true">
6538+
<Str Cat="Text">
6539+
<Val><![CDATA[File is CommonJS module because '{0}' does not have field "type"]]></Val>
6540+
<Tgt Cat="Text" Stat="Loc" Orig="New">
6541+
<Val><![CDATA[檔案是 CommonJS 模組,因為 '{0}' 沒有 "type" 欄位]]></Val>
6542+
</Tgt>
6543+
</Str>
6544+
<Disp Icon="Str" />
6545+
</Item>
6546+
<Item ItemId=";File_is_CommonJS_module_because_0_has_field_type_whose_value_is_not_module_1459" ItemType="0" PsrId="306" Leaf="true">
6547+
<Str Cat="Text">
6548+
<Val><![CDATA[File is CommonJS module because '{0}' has field "type" whose value is not "module"]]></Val>
6549+
<Tgt Cat="Text" Stat="Loc" Orig="New">
6550+
<Val><![CDATA[檔案是 CommonJS 模組,因為 '{0}' 具有值不是 "module" 的 "type" 欄位]]></Val>
6551+
</Tgt>
6552+
</Str>
6553+
<Disp Icon="Str" />
6554+
</Item>
6555+
<Item ItemId=";File_is_CommonJS_module_because_package_json_was_not_found_1461" ItemType="0" PsrId="306" Leaf="true">
6556+
<Str Cat="Text">
6557+
<Val><![CDATA[File is CommonJS module because 'package.json' was not found]]></Val>
6558+
<Tgt Cat="Text" Stat="Loc" Orig="New">
6559+
<Val><![CDATA[檔案是 CommonJS 模組,因為找不到 'package.json']]></Val>
6560+
</Tgt>
6561+
</Str>
6562+
<Disp Icon="Str" />
6563+
</Item>
6564+
<Item ItemId=";File_is_ECMAScript_module_because_0_has_field_type_with_value_module_1458" ItemType="0" PsrId="306" Leaf="true">
6565+
<Str Cat="Text">
6566+
<Val><![CDATA[File is ECMAScript module because '{0}' has field "type" with value "module"]]></Val>
6567+
<Tgt Cat="Text" Stat="Loc" Orig="New">
6568+
<Val><![CDATA[檔案是 ECMAScript 模組,因為 '{0}' 具有值不是 "module" 的 "type" 欄位]]></Val>
6569+
</Tgt>
6570+
</Str>
6571+
<Disp Icon="Str" />
6572+
</Item>
65376573
<Item ItemId=";File_is_a_CommonJS_module_it_may_be_converted_to_an_ES_module_80001" ItemType="0" PsrId="306" Leaf="true">
65386574
<Str Cat="Text">
65396575
<Val><![CDATA[File is a CommonJS module; it may be converted to an ES module.]]></Val>

src/loc/lcl/csy/diagnosticMessages/diagnosticMessages.generated.json.lcl

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6543,6 +6543,42 @@
65436543
</Str>
65446544
<Disp Icon="Str" />
65456545
</Item>
6546+
<Item ItemId=";File_is_CommonJS_module_because_0_does_not_have_field_type_1460" ItemType="0" PsrId="306" Leaf="true">
6547+
<Str Cat="Text">
6548+
<Val><![CDATA[File is CommonJS module because '{0}' does not have field "type"]]></Val>
6549+
<Tgt Cat="Text" Stat="Loc" Orig="New">
6550+
<Val><![CDATA[Soubor je modul CommonJS, protože {0} nemá pole type]]></Val>
6551+
</Tgt>
6552+
</Str>
6553+
<Disp Icon="Str" />
6554+
</Item>
6555+
<Item ItemId=";File_is_CommonJS_module_because_0_has_field_type_whose_value_is_not_module_1459" ItemType="0" PsrId="306" Leaf="true">
6556+
<Str Cat="Text">
6557+
<Val><![CDATA[File is CommonJS module because '{0}' has field "type" whose value is not "module"]]></Val>
6558+
<Tgt Cat="Text" Stat="Loc" Orig="New">
6559+
<Val><![CDATA[Soubor je modul CommonJS, protože {0} má pole type, jehož hodnota není module]]></Val>
6560+
</Tgt>
6561+
</Str>
6562+
<Disp Icon="Str" />
6563+
</Item>
6564+
<Item ItemId=";File_is_CommonJS_module_because_package_json_was_not_found_1461" ItemType="0" PsrId="306" Leaf="true">
6565+
<Str Cat="Text">
6566+
<Val><![CDATA[File is CommonJS module because 'package.json' was not found]]></Val>
6567+
<Tgt Cat="Text" Stat="Loc" Orig="New">
6568+
<Val><![CDATA[Soubor je modul CommonJS, protože se nenašel package.json]]></Val>
6569+
</Tgt>
6570+
</Str>
6571+
<Disp Icon="Str" />
6572+
</Item>
6573+
<Item ItemId=";File_is_ECMAScript_module_because_0_has_field_type_with_value_module_1458" ItemType="0" PsrId="306" Leaf="true">
6574+
<Str Cat="Text">
6575+
<Val><![CDATA[File is ECMAScript module because '{0}' has field "type" with value "module"]]></Val>
6576+
<Tgt Cat="Text" Stat="Loc" Orig="New">
6577+
<Val><![CDATA[Soubor je modul ECMAScript, protože {0} má pole type s hodnotou module]]></Val>
6578+
</Tgt>
6579+
</Str>
6580+
<Disp Icon="Str" />
6581+
</Item>
65466582
<Item ItemId=";File_is_a_CommonJS_module_it_may_be_converted_to_an_ES_module_80001" ItemType="0" PsrId="306" Leaf="true">
65476583
<Str Cat="Text">
65486584
<Val><![CDATA[File is a CommonJS module; it may be converted to an ES module.]]></Val>

src/loc/lcl/deu/diagnosticMessages/diagnosticMessages.generated.json.lcl

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6531,6 +6531,42 @@
65316531
</Str>
65326532
<Disp Icon="Str" />
65336533
</Item>
6534+
<Item ItemId=";File_is_CommonJS_module_because_0_does_not_have_field_type_1460" ItemType="0" PsrId="306" Leaf="true">
6535+
<Str Cat="Text">
6536+
<Val><![CDATA[File is CommonJS module because '{0}' does not have field "type"]]></Val>
6537+
<Tgt Cat="Text" Stat="Loc" Orig="New">
6538+
<Val><![CDATA[Die Datei ist ein CommonJS-Modul, da '{0}' nicht das Feld „Typ“ aufweist]]></Val>
6539+
</Tgt>
6540+
</Str>
6541+
<Disp Icon="Str" />
6542+
</Item>
6543+
<Item ItemId=";File_is_CommonJS_module_because_0_has_field_type_whose_value_is_not_module_1459" ItemType="0" PsrId="306" Leaf="true">
6544+
<Str Cat="Text">
6545+
<Val><![CDATA[File is CommonJS module because '{0}' has field "type" whose value is not "module"]]></Val>
6546+
<Tgt Cat="Text" Stat="Loc" Orig="New">
6547+
<Val><![CDATA[Die Datei ist ein CommonJS-Modul, da '{0}' das Feld „Typ“ aufweist, dessen Wert nicht „Modul“ ist]]></Val>
6548+
</Tgt>
6549+
</Str>
6550+
<Disp Icon="Str" />
6551+
</Item>
6552+
<Item ItemId=";File_is_CommonJS_module_because_package_json_was_not_found_1461" ItemType="0" PsrId="306" Leaf="true">
6553+
<Str Cat="Text">
6554+
<Val><![CDATA[File is CommonJS module because 'package.json' was not found]]></Val>
6555+
<Tgt Cat="Text" Stat="Loc" Orig="New">
6556+
<Val><![CDATA[Die Datei ist ein CommonJS-Modul, da „package.json“ nicht gefunden wurde]]></Val>
6557+
</Tgt>
6558+
</Str>
6559+
<Disp Icon="Str" />
6560+
</Item>
6561+
<Item ItemId=";File_is_ECMAScript_module_because_0_has_field_type_with_value_module_1458" ItemType="0" PsrId="306" Leaf="true">
6562+
<Str Cat="Text">
6563+
<Val><![CDATA[File is ECMAScript module because '{0}' has field "type" with value "module"]]></Val>
6564+
<Tgt Cat="Text" Stat="Loc" Orig="New">
6565+
<Val><![CDATA[Die Datei ist ein ECMAScript-Modul, da '{0}' das Feld „Typ“ mit dem Wert „Modul“ aufweist.]]></Val>
6566+
</Tgt>
6567+
</Str>
6568+
<Disp Icon="Str" />
6569+
</Item>
65346570
<Item ItemId=";File_is_a_CommonJS_module_it_may_be_converted_to_an_ES_module_80001" ItemType="0" PsrId="306" Leaf="true">
65356571
<Str Cat="Text">
65366572
<Val><![CDATA[File is a CommonJS module; it may be converted to an ES module.]]></Val>

0 commit comments

Comments
 (0)