Skip to content

Commit b0a0808

Browse files
Update lib/internal/modules/helpers.js
Co-authored-by: Antoine du Hamel <[email protected]>
1 parent 062986d commit b0a0808

File tree

1 file changed

+22
-38
lines changed

1 file changed

+22
-38
lines changed

lib/internal/modules/helpers.js

Lines changed: 22 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -314,44 +314,37 @@ function getBuiltinModule(id) {
314314
return normalizedId ? require(normalizedId) : undefined;
315315
}
316316

317-
/**
318-
* TypeScript parsing function, by default Amaro.transformSync.
319-
* @type {Function}
320-
*/
321-
let typeScriptParser;
322317
/**
323318
* The TypeScript parsing mode, either 'strip-only' or 'transform'.
324319
* @type {string}
325320
*/
326-
let typeScriptParsingMode;
327-
/**
328-
* Whether source maps are enabled for TypeScript parsing.
329-
* @type {boolean}
330-
*/
331-
let sourceMapEnabled;
321+
const getTypeScriptParsingMode = getLazy(() =>
322+
(getOptionValue('--experimental-transform-types') ? 'transform' : 'strip-only'),
323+
);
332324

333325
/**
334326
* Load the TypeScript parser.
335-
* @param {Function} parser - A function that takes a string of TypeScript code
336327
* and returns an object with a `code` property.
337328
* @returns {Function} The TypeScript parser function.
338329
*/
339-
function loadTypeScriptParser(parser) {
340-
if (typeScriptParser) {
341-
return typeScriptParser;
342-
}
330+
const loadTypeScriptParser = getLazy(() => {
331+
const amaro = require('internal/deps/amaro/dist/index');
332+
return amaro.transformSync;
333+
});
343334

344-
if (parser) {
345-
typeScriptParser = parser;
346-
} else {
347-
const amaro = require('internal/deps/amaro/dist/index');
348-
// Default option for Amaro is to perform Type Stripping only.
349-
typeScriptParsingMode = getOptionValue('--experimental-transform-types') ? 'transform' : 'strip-only';
350-
sourceMapEnabled = getOptionValue('--enable-source-maps');
351-
// Curry the transformSync function with the default options.
352-
typeScriptParser = amaro.transformSync;
335+
/**
336+
*
337+
* @param {string} source the source code
338+
* @param {object} options the options to pass to the parser
339+
* @returns {TransformOutput} an object with a `code` property.
340+
*/
341+
function parseTypeScript(source, options) {
342+
const parse = loadTypeScriptParser();
343+
try {
344+
return parse(source, options);
345+
} catch (error) {
346+
throw new ERR_INVALID_TYPESCRIPT_SYNTAX(error);
353347
}
354-
return typeScriptParser;
355348
}
356349

357350
/**
@@ -366,14 +359,13 @@ function loadTypeScriptParser(parser) {
366359
*/
367360
function stripTypeScriptTypes(source, filename) {
368361
assert(typeof source === 'string');
369-
const parse = loadTypeScriptParser();
370362
const options = {
371363
__proto__: null,
372-
mode: typeScriptParsingMode,
373-
sourceMap: sourceMapEnabled,
364+
mode: getTypeScriptParsingMode(),
365+
sourceMap: getOptionValue('--enable-source-maps'),
374366
filename,
375367
};
376-
const { code, map } = tryParse(() => parse(source, options));
368+
const { code, map } = parseTypeScript(source, options);
377369
if (map) {
378370
// TODO(@marco-ippolito) When Buffer.transcode supports utf8 to
379371
// base64 transformation, we should change this line.
@@ -386,14 +378,6 @@ function stripTypeScriptTypes(source, filename) {
386378
return `${code}\n\n//# sourceURL=${filename}`;
387379
}
388380

389-
function tryParse(parse) {
390-
try {
391-
return parse();
392-
} catch (error) {
393-
throw new ERR_INVALID_TYPESCRIPT_SYNTAX(error);
394-
}
395-
}
396-
397381
/** @type {import('internal/util/types')} */
398382
let _TYPES = null;
399383
/**

0 commit comments

Comments
 (0)