Skip to content

Commit

Permalink
Update lib/internal/modules/helpers.js
Browse files Browse the repository at this point in the history
Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com>
  • Loading branch information
marco-ippolito and aduh95 committed Oct 8, 2024
1 parent 062986d commit b0a0808
Showing 1 changed file with 22 additions and 38 deletions.
60 changes: 22 additions & 38 deletions lib/internal/modules/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,44 +314,37 @@ function getBuiltinModule(id) {
return normalizedId ? require(normalizedId) : undefined;
}

/**
* TypeScript parsing function, by default Amaro.transformSync.
* @type {Function}
*/
let typeScriptParser;
/**
* The TypeScript parsing mode, either 'strip-only' or 'transform'.
* @type {string}
*/
let typeScriptParsingMode;
/**
* Whether source maps are enabled for TypeScript parsing.
* @type {boolean}
*/
let sourceMapEnabled;
const getTypeScriptParsingMode = getLazy(() =>
(getOptionValue('--experimental-transform-types') ? 'transform' : 'strip-only'),
);

/**
* Load the TypeScript parser.
* @param {Function} parser - A function that takes a string of TypeScript code
* and returns an object with a `code` property.
* @returns {Function} The TypeScript parser function.
*/
function loadTypeScriptParser(parser) {
if (typeScriptParser) {
return typeScriptParser;
}
const loadTypeScriptParser = getLazy(() => {
const amaro = require('internal/deps/amaro/dist/index');
return amaro.transformSync;
});

if (parser) {
typeScriptParser = parser;
} else {
const amaro = require('internal/deps/amaro/dist/index');
// Default option for Amaro is to perform Type Stripping only.
typeScriptParsingMode = getOptionValue('--experimental-transform-types') ? 'transform' : 'strip-only';
sourceMapEnabled = getOptionValue('--enable-source-maps');
// Curry the transformSync function with the default options.
typeScriptParser = amaro.transformSync;
/**
*
* @param {string} source the source code
* @param {object} options the options to pass to the parser
* @returns {TransformOutput} an object with a `code` property.
*/
function parseTypeScript(source, options) {
const parse = loadTypeScriptParser();
try {
return parse(source, options);
} catch (error) {
throw new ERR_INVALID_TYPESCRIPT_SYNTAX(error);
}
return typeScriptParser;
}

/**
Expand All @@ -366,14 +359,13 @@ function loadTypeScriptParser(parser) {
*/
function stripTypeScriptTypes(source, filename) {
assert(typeof source === 'string');
const parse = loadTypeScriptParser();
const options = {
__proto__: null,
mode: typeScriptParsingMode,
sourceMap: sourceMapEnabled,
mode: getTypeScriptParsingMode(),
sourceMap: getOptionValue('--enable-source-maps'),
filename,
};
const { code, map } = tryParse(() => parse(source, options));
const { code, map } = parseTypeScript(source, options);
if (map) {
// TODO(@marco-ippolito) When Buffer.transcode supports utf8 to
// base64 transformation, we should change this line.
Expand All @@ -386,14 +378,6 @@ function stripTypeScriptTypes(source, filename) {
return `${code}\n\n//# sourceURL=${filename}`;
}

function tryParse(parse) {
try {
return parse();
} catch (error) {
throw new ERR_INVALID_TYPESCRIPT_SYNTAX(error);
}
}

/** @type {import('internal/util/types')} */
let _TYPES = null;
/**
Expand Down

0 comments on commit b0a0808

Please sign in to comment.