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;