@@ -17,6 +17,7 @@ const {
1717const {
1818 ERR_INVALID_ARG_TYPE ,
1919 ERR_INVALID_RETURN_PROPERTY_VALUE ,
20+ ERR_INVALID_TYPESCRIPT_SYNTAX ,
2021} = require ( 'internal/errors' ) . codes ;
2122const { BuiltinModule } = require ( 'internal/bootstrap/realm' ) ;
2223
@@ -315,44 +316,37 @@ function getBuiltinModule(id) {
315316 return normalizedId ? require ( normalizedId ) : undefined ;
316317}
317318
318- /**
319- * TypeScript parsing function, by default Amaro.transformSync.
320- * @type {Function }
321- */
322- let typeScriptParser ;
323319/**
324320 * The TypeScript parsing mode, either 'strip-only' or 'transform'.
325321 * @type {string }
326322 */
327- let typeScriptParsingMode ;
328- /**
329- * Whether source maps are enabled for TypeScript parsing.
330- * @type {boolean }
331- */
332- let sourceMapEnabled ;
323+ const getTypeScriptParsingMode = getLazy ( ( ) =>
324+ ( getOptionValue ( '--experimental-transform-types' ) ? 'transform' : 'strip-only' ) ,
325+ ) ;
333326
334327/**
335328 * Load the TypeScript parser.
336- * @param {Function } parser - A function that takes a string of TypeScript code
337329 * and returns an object with a `code` property.
338330 * @returns {Function } The TypeScript parser function.
339331 */
340- function loadTypeScriptParser ( parser ) {
341- if ( typeScriptParser ) {
342- return typeScriptParser ;
343- }
332+ const loadTypeScriptParser = getLazy ( ( ) => {
333+ const amaro = require ( 'internal/deps/amaro/dist/index' ) ;
334+ return amaro . transformSync ;
335+ } ) ;
344336
345- if ( parser ) {
346- typeScriptParser = parser ;
347- } else {
348- const amaro = require ( 'internal/deps/amaro/dist/index' ) ;
349- // Default option for Amaro is to perform Type Stripping only.
350- typeScriptParsingMode = getOptionValue ( '--experimental-transform-types' ) ? 'transform' : 'strip-only' ;
351- sourceMapEnabled = getOptionValue ( '--enable-source-maps' ) ;
352- // Curry the transformSync function with the default options.
353- typeScriptParser = amaro . transformSync ;
337+ /**
338+ *
339+ * @param {string } source the source code
340+ * @param {object } options the options to pass to the parser
341+ * @returns {TransformOutput } an object with a `code` property.
342+ */
343+ function parseTypeScript ( source , options ) {
344+ const parse = loadTypeScriptParser ( ) ;
345+ try {
346+ return parse ( source , options ) ;
347+ } catch ( error ) {
348+ throw new ERR_INVALID_TYPESCRIPT_SYNTAX ( error ) ;
354349 }
355- return typeScriptParser ;
356350}
357351
358352/**
@@ -367,14 +361,13 @@ function loadTypeScriptParser(parser) {
367361 */
368362function stripTypeScriptTypes ( source , filename ) {
369363 assert ( typeof source === 'string' ) ;
370- const parse = loadTypeScriptParser ( ) ;
371364 const options = {
372365 __proto__ : null ,
373- mode : typeScriptParsingMode ,
374- sourceMap : sourceMapEnabled ,
366+ mode : getTypeScriptParsingMode ( ) ,
367+ sourceMap : getOptionValue ( '--enable-source-maps' ) ,
375368 filename,
376369 } ;
377- const { code, map } = parse ( source , options ) ;
370+ const { code, map } = parseTypeScript ( source , options ) ;
378371 if ( map ) {
379372 // TODO(@marco-ippolito) When Buffer.transcode supports utf8 to
380373 // base64 transformation, we should change this line.
0 commit comments