From 35bddfc07d6582661287eb1147dd9c2405e47dbf Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Fri, 9 Aug 2024 18:26:51 +0200 Subject: [PATCH] module: do not attempt to strip type when there's no source --- 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 0a15d5b87d4f95..6822facc9e5343 100644 --- a/lib/internal/modules/esm/get_format.js +++ b/lib/internal/modules/esm/get_format.js @@ -161,8 +161,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;