From 100225fbe104d4e609363a78979e42d474ad44f5 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Mon, 12 Aug 2024 09:51:40 +0200 Subject: [PATCH] module: do not attempt to strip type when there's no source PR-URL: https://github.com/nodejs/node/pull/54287 Reviewed-By: Marco Ippolito Reviewed-By: Mohammed Keyvanzadeh Reviewed-By: Benjamin Gruenbaum Reviewed-By: Moshe Atlow Reviewed-By: Yagiz Nizipli Reviewed-By: James M Snell --- lib/internal/modules/esm/get_format.js | 8 ++++++-- lib/internal/modules/helpers.js | 3 +-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/internal/modules/esm/get_format.js b/lib/internal/modules/esm/get_format.js index 207cd91edd61af..739124e5241f09 100644 --- a/lib/internal/modules/esm/get_format.js +++ b/lib/internal/modules/esm/get_format.js @@ -160,8 +160,12 @@ function getFileProtocolModuleFormat(url, context = { __proto__: null }, ignoreE default: { // The user did not pass `--experimental-default-type`. // `source` is undefined when this is called from `defaultResolve`; // but this gets called again from `defaultLoad`/`defaultLoadSync`. - const { tsParse } = require('internal/modules/helpers'); - const parsedSource = tsParse(source); + let parsedSource; + if (source) { + // We do the type stripping only if `source` is not falsy. + const { tsParse } = require('internal/modules/helpers'); + parsedSource = tsParse(source); + } const detectedFormat = detectModuleFormat(parsedSource, url); // When source is undefined, default to module-typescript. const format = detectedFormat ? `${detectedFormat}-typescript` : 'module-typescript'; diff --git a/lib/internal/modules/helpers.js b/lib/internal/modules/helpers.js index b84312f2def6fb..4bcfa379ae9929 100644 --- a/lib/internal/modules/helpers.js +++ b/lib/internal/modules/helpers.js @@ -331,8 +331,7 @@ function loadTypeScriptParser(parser) { * @returns {string} JavaScript code. */ function tsParse(source) { - // TODO(@marco-ippolito) Checking empty string or non string input should be handled in Amaro. - if (!source || typeof source !== 'string') { return ''; } + assert(typeof source === 'string'); const parse = loadTypeScriptParser(); const { code } = parse(source); return code;