Fix helpers emit for .cjs files in ESM module mode (#61814)

This commit is contained in:
Andrew Branch 2025-06-06 15:08:04 -07:00 committed by GitHub
parent fa2a0fc5a2
commit 7715955f89
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 45 additions and 3 deletions

View File

@ -700,9 +700,10 @@ export function createExternalHelpersImportDeclarationIfNeeded(nodeFactory: Node
const impliedModuleKind = getImpliedNodeFormatForEmitWorker(sourceFile, compilerOptions);
const helpers = getImportedHelpers(sourceFile);
if (
(moduleKind >= ModuleKind.ES2015 && moduleKind <= ModuleKind.ESNext) ||
impliedModuleKind === ModuleKind.ESNext ||
impliedModuleKind === undefined && moduleKind === ModuleKind.Preserve
impliedModuleKind !== ModuleKind.CommonJS &&
((moduleKind >= ModuleKind.ES2015 && moduleKind <= ModuleKind.ESNext) ||
impliedModuleKind === ModuleKind.ESNext ||
impliedModuleKind === undefined && moduleKind === ModuleKind.Preserve)
) {
// When we emit as an ES module, generate an `import` declaration that uses named imports for helpers.
// If we cannot determine the implied module kind under `module: preserve` we assume ESM.

View File

@ -0,0 +1,9 @@
notmodule.cts(1,23): error TS2354: This syntax requires an imported helper but module 'tslib' cannot be found.
==== notmodule.cts (1 errors) ====
export async function foo() {
~~~
!!! error TS2354: This syntax requires an imported helper but module 'tslib' cannot be found.
await 0;
}

View File

@ -0,0 +1,24 @@
//// [tests/cases/compiler/ctsFileInEsnextHelpers.ts] ////
//// [notmodule.cts]
export async function foo() {
await 0;
}
//// [notmodule.cjs]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.foo = foo;
var tslib_1 = require("tslib");
function foo() {
return tslib_1.__awaiter(this, void 0, void 0, function () {
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, 0];
case 1:
_a.sent();
return [2 /*return*/];
}
});
});
}

View File

@ -0,0 +1,8 @@
// @module: es2015
// @importHelpers: true
// @noTypesAndSymbols: true
// @Filename: notmodule.cts
export async function foo() {
await 0;
}