Skip to content

Commit 816f21c

Browse files
committed
Fix helpers emit for .cjs files in ESM module mode
1 parent b504a1e commit 816f21c

7 files changed

Lines changed: 67 additions & 4 deletions

File tree

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.

src/compiler/moduleSpecifiers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ function computeModuleSpecifiers(
513513
info,
514514
compilerOptions,
515515
host,
516-
options.overrideImportMode || importingSourceFile.impliedNodeFormat,
516+
options.overrideImportMode,
517517
preferences,
518518
/*pathsOnly*/ modulePath.isRedirect || !!specifier,
519519
);
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+
//// [tests/cases/compiler/ctsFileInEsnextHelpers.ts] ////
2+
3+
=== notmodule.cts ===
4+
export async function foo() {
5+
>foo : Symbol(foo, Decl(notmodule.cts, 0, 0))
6+
7+
await 0;
8+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//// [tests/cases/compiler/ctsFileInEsnextHelpers.ts] ////
2+
3+
=== notmodule.cts ===
4+
export async function foo() {
5+
>foo : () => Promise<void>
6+
> : ^^^^^^^^^^^^^^^^^^^
7+
8+
await 0;
9+
>await 0 : 0
10+
> : ^
11+
>0 : 0
12+
> : ^
13+
}
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)