Skip to content

Commit 9a4eb21

Browse files
authored
module: do not attempt to strip type when there's no source
PR-URL: nodejs#54287 Reviewed-By: Marco Ippolito <[email protected]> Reviewed-By: Mohammed Keyvanzadeh <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Moshe Atlow <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent d7f3730 commit 9a4eb21

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

lib/internal/modules/esm/get_format.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,12 @@ function getFileProtocolModuleFormat(url, context = { __proto__: null }, ignoreE
161161
default: { // The user did not pass `--experimental-default-type`.
162162
// `source` is undefined when this is called from `defaultResolve`;
163163
// but this gets called again from `defaultLoad`/`defaultLoadSync`.
164-
const { tsParse } = require('internal/modules/helpers');
165-
const parsedSource = tsParse(source);
164+
let parsedSource;
165+
if (source) {
166+
// We do the type stripping only if `source` is not falsy.
167+
const { tsParse } = require('internal/modules/helpers');
168+
parsedSource = tsParse(source);
169+
}
166170
const detectedFormat = detectModuleFormat(parsedSource, url);
167171
// When source is undefined, default to module-typescript.
168172
const format = detectedFormat ? `${detectedFormat}-typescript` : 'module-typescript';

lib/internal/modules/helpers.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,8 +331,7 @@ function loadTypeScriptParser(parser) {
331331
* @returns {string} JavaScript code.
332332
*/
333333
function tsParse(source) {
334-
// TODO(@marco-ippolito) Checking empty string or non string input should be handled in Amaro.
335-
if (!source || typeof source !== 'string') { return ''; }
334+
assert(typeof source === 'string');
336335
const parse = loadTypeScriptParser();
337336
const { code } = parse(source);
338337
return code;

0 commit comments

Comments
 (0)