From be4babb3c2cb46030057577bc93153ebd245f2a3 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Thu, 19 Sep 2024 12:34:13 +0200 Subject: [PATCH] module: report unfinished TLA in ambiguous modules MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/54980 Fixes: https://github.com/nodejs/node/issues/54931 Reviewed-By: Michaƫl Zasso Reviewed-By: Marco Ippolito Reviewed-By: Joyee Cheung --- lib/internal/modules/cjs/loader.js | 2 +- test/es-module/test-esm-detect-ambiguous.mjs | 12 ++++++++++++ test/fixtures/es-modules/tla/unresolved.js | 1 + 3 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/es-modules/tla/unresolved.js diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index 8d3f59abcf5..a8d245b1081 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -1381,7 +1381,7 @@ function loadESMFromCJS(mod, filename) { if (isMain) { require('internal/modules/run_main').runEntryPointWithESMLoader((cascadedLoader) => { const mainURL = pathToFileURL(filename).href; - cascadedLoader.import(mainURL, undefined, { __proto__: null }, true); + return cascadedLoader.import(mainURL, undefined, { __proto__: null }, true); }); // ESM won't be accessible via process.mainModule. setOwnProperty(process, 'mainModule', undefined); diff --git a/test/es-module/test-esm-detect-ambiguous.mjs b/test/es-module/test-esm-detect-ambiguous.mjs index fce455cf18b..5d5a2744afc 100644 --- a/test/es-module/test-esm-detect-ambiguous.mjs +++ b/test/es-module/test-esm-detect-ambiguous.mjs @@ -252,6 +252,18 @@ describe('Module syntax detection', { concurrency: !process.env.TEST_PARALLEL }, strictEqual(signal, null); }); + it('reports unfinished top-level `await`', async () => { + const { stdout, stderr, code, signal } = await spawnPromisified(process.execPath, [ + '--no-warnings', + fixtures.path('es-modules/tla/unresolved.js'), + ]); + + strictEqual(stderr, ''); + strictEqual(stdout, ''); + strictEqual(code, 13); + strictEqual(signal, null); + }); + it('permits top-level `await` above import/export syntax', async () => { const { stdout, stderr, code, signal } = await spawnPromisified(process.execPath, [ '--eval', diff --git a/test/fixtures/es-modules/tla/unresolved.js b/test/fixtures/es-modules/tla/unresolved.js new file mode 100644 index 00000000000..231a8cd6348 --- /dev/null +++ b/test/fixtures/es-modules/tla/unresolved.js @@ -0,0 +1 @@ +await new Promise(() => {});