diff --git a/src/compiler/factory/utilities.ts b/src/compiler/factory/utilities.ts index 80df86fd3ceae..bdc90e40e9eb1 100644 --- a/src/compiler/factory/utilities.ts +++ b/src/compiler/factory/utilities.ts @@ -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. diff --git a/tests/baselines/reference/ctsFileInEsnextHelpers.errors.txt b/tests/baselines/reference/ctsFileInEsnextHelpers.errors.txt new file mode 100644 index 0000000000000..b43c66af41996 --- /dev/null +++ b/tests/baselines/reference/ctsFileInEsnextHelpers.errors.txt @@ -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; + } \ No newline at end of file diff --git a/tests/baselines/reference/ctsFileInEsnextHelpers.js b/tests/baselines/reference/ctsFileInEsnextHelpers.js new file mode 100644 index 0000000000000..e08d213dfff57 --- /dev/null +++ b/tests/baselines/reference/ctsFileInEsnextHelpers.js @@ -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*/]; + } + }); + }); +} diff --git a/tests/cases/compiler/ctsFileInEsnextHelpers.ts b/tests/cases/compiler/ctsFileInEsnextHelpers.ts new file mode 100644 index 0000000000000..6708f6266b5b3 --- /dev/null +++ b/tests/cases/compiler/ctsFileInEsnextHelpers.ts @@ -0,0 +1,8 @@ +// @module: es2015 +// @importHelpers: true +// @noTypesAndSymbols: true + +// @Filename: notmodule.cts +export async function foo() { + await 0; +} \ No newline at end of file