Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/compiler/factory/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was able to simplify this condition in Corsa since we’re removing System, AMD, and UMD. Opting to make the minimal change here rather than try to clean up.

) {
// 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.
Expand Down
9 changes: 9 additions & 0 deletions tests/baselines/reference/ctsFileInEsnextHelpers.errors.txt
Original file line number Diff line number Diff line change
@@ -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;
}
24 changes: 24 additions & 0 deletions tests/baselines/reference/ctsFileInEsnextHelpers.js
Original file line number Diff line number Diff line change
@@ -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) {
Comment on lines +14 to +15
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before the change, emit was missing the tslib_1. on these

switch (_a.label) {
case 0: return [4 /*yield*/, 0];
case 1:
_a.sent();
return [2 /*return*/];
}
});
});
}
8 changes: 8 additions & 0 deletions tests/cases/compiler/ctsFileInEsnextHelpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// @module: es2015
// @importHelpers: true
// @noTypesAndSymbols: true

// @Filename: notmodule.cts
export async function foo() {
await 0;
}