Skip to content

Commit 7715955

Browse files
authored
Fix helpers emit for .cjs files in ESM module mode (#61814)
1 parent fa2a0fc commit 7715955

File tree

4 files changed

+45
-3
lines changed

4 files changed

+45
-3
lines changed

src/compiler/factory/utilities.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -700,9 +700,10 @@ export function createExternalHelpersImportDeclarationIfNeeded(nodeFactory: Node
700700
const impliedModuleKind = getImpliedNodeFormatForEmitWorker(sourceFile, compilerOptions);
701701
const helpers = getImportedHelpers(sourceFile);
702702
if (
703-
(moduleKind >= ModuleKind.ES2015 && moduleKind <= ModuleKind.ESNext) ||
704-
impliedModuleKind === ModuleKind.ESNext ||
705-
impliedModuleKind === undefined && moduleKind === ModuleKind.Preserve
703+
impliedModuleKind !== ModuleKind.CommonJS &&
704+
((moduleKind >= ModuleKind.ES2015 && moduleKind <= ModuleKind.ESNext) ||
705+
impliedModuleKind === ModuleKind.ESNext ||
706+
impliedModuleKind === undefined && moduleKind === ModuleKind.Preserve)
706707
) {
707708
// When we emit as an ES module, generate an `import` declaration that uses named imports for helpers.
708709
// If we cannot determine the implied module kind under `module: preserve` we assume ESM.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
notmodule.cts(1,23): error TS2354: This syntax requires an imported helper but module 'tslib' cannot be found.
2+
3+
4+
==== notmodule.cts (1 errors) ====
5+
export async function foo() {
6+
~~~
7+
!!! error TS2354: This syntax requires an imported helper but module 'tslib' cannot be found.
8+
await 0;
9+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//// [tests/cases/compiler/ctsFileInEsnextHelpers.ts] ////
2+
3+
//// [notmodule.cts]
4+
export async function foo() {
5+
await 0;
6+
}
7+
8+
//// [notmodule.cjs]
9+
"use strict";
10+
Object.defineProperty(exports, "__esModule", { value: true });
11+
exports.foo = foo;
12+
var tslib_1 = require("tslib");
13+
function foo() {
14+
return tslib_1.__awaiter(this, void 0, void 0, function () {
15+
return tslib_1.__generator(this, function (_a) {
16+
switch (_a.label) {
17+
case 0: return [4 /*yield*/, 0];
18+
case 1:
19+
_a.sent();
20+
return [2 /*return*/];
21+
}
22+
});
23+
});
24+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// @module: es2015
2+
// @importHelpers: true
3+
// @noTypesAndSymbols: true
4+
5+
// @Filename: notmodule.cts
6+
export async function foo() {
7+
await 0;
8+
}

0 commit comments

Comments
 (0)