Skip to content

Commit

Permalink
module: do not attempt to strip type when there's no source
Browse files Browse the repository at this point in the history
PR-URL: #54287
Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
aduh95 authored Aug 12, 2024
1 parent d7f3730 commit 9a4eb21
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
8 changes: 6 additions & 2 deletions lib/internal/modules/esm/get_format.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
3 changes: 1 addition & 2 deletions lib/internal/modules/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 9a4eb21

Please sign in to comment.