mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-12-11 09:24:19 -06:00
Fix helpers emit for .cjs files in ESM module mode (#61814)
This commit is contained in:
parent
fa2a0fc5a2
commit
7715955f89
@ -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.
|
||||
|
||||
@ -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
tests/baselines/reference/ctsFileInEsnextHelpers.js
Normal file
24
tests/baselines/reference/ctsFileInEsnextHelpers.js
Normal 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*/];
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
8
tests/cases/compiler/ctsFileInEsnextHelpers.ts
Normal file
8
tests/cases/compiler/ctsFileInEsnextHelpers.ts
Normal file
@ -0,0 +1,8 @@
|
||||
// @module: es2015
|
||||
// @importHelpers: true
|
||||
// @noTypesAndSymbols: true
|
||||
|
||||
// @Filename: notmodule.cts
|
||||
export async function foo() {
|
||||
await 0;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user