@@ -15,6 +15,7 @@ const {
1515const {
1616 ERR_INVALID_ARG_TYPE ,
1717 ERR_INVALID_RETURN_PROPERTY_VALUE ,
18+ ERR_INVALID_TYPESCRIPT_SYNTAX ,
1819} = require ( 'internal/errors' ) . codes ;
1920const { BuiltinModule } = require ( 'internal/bootstrap/realm' ) ;
2021
@@ -312,44 +313,37 @@ function getBuiltinModule(id) {
312313 return normalizedId ? require ( normalizedId ) : undefined ;
313314}
314315
315- /**
316- * TypeScript parsing function, by default Amaro.transformSync.
317- * @type {Function }
318- */
319- let typeScriptParser ;
320316/**
321317 * The TypeScript parsing mode, either 'strip-only' or 'transform'.
322318 * @type {string }
323319 */
324- let typeScriptParsingMode ;
325- /**
326- * Whether source maps are enabled for TypeScript parsing.
327- * @type {boolean }
328- */
329- let sourceMapEnabled ;
320+ const getTypeScriptParsingMode = getLazy ( ( ) =>
321+ ( getOptionValue ( '--experimental-transform-types' ) ? 'transform' : 'strip-only' ) ,
322+ ) ;
330323
331324/**
332325 * Load the TypeScript parser.
333- * @param {Function } parser - A function that takes a string of TypeScript code
334326 * and returns an object with a `code` property.
335327 * @returns {Function } The TypeScript parser function.
336328 */
337- function loadTypeScriptParser ( parser ) {
338- if ( typeScriptParser ) {
339- return typeScriptParser ;
340- }
329+ const loadTypeScriptParser = getLazy ( ( ) => {
330+ const amaro = require ( 'internal/deps/amaro/dist/index' ) ;
331+ return amaro . transformSync ;
332+ } ) ;
341333
342- if ( parser ) {
343- typeScriptParser = parser ;
344- } else {
345- const amaro = require ( 'internal/deps/amaro/dist/index' ) ;
346- // Default option for Amaro is to perform Type Stripping only.
347- typeScriptParsingMode = getOptionValue ( '--experimental-transform-types' ) ? 'transform' : 'strip-only' ;
348- sourceMapEnabled = getOptionValue ( '--enable-source-maps' ) ;
349- // Curry the transformSync function with the default options.
350- typeScriptParser = amaro . transformSync ;
334+ /**
335+ *
336+ * @param {string } source the source code
337+ * @param {object } options the options to pass to the parser
338+ * @returns {TransformOutput } an object with a `code` property.
339+ */
340+ function parseTypeScript ( source , options ) {
341+ const parse = loadTypeScriptParser ( ) ;
342+ try {
343+ return parse ( source , options ) ;
344+ } catch ( error ) {
345+ throw new ERR_INVALID_TYPESCRIPT_SYNTAX ( error ) ;
351346 }
352- return typeScriptParser ;
353347}
354348
355349/**
@@ -364,14 +358,13 @@ function loadTypeScriptParser(parser) {
364358 */
365359function stripTypeScriptTypes ( source , filename ) {
366360 assert ( typeof source === 'string' ) ;
367- const parse = loadTypeScriptParser ( ) ;
368361 const options = {
369362 __proto__ : null ,
370- mode : typeScriptParsingMode ,
371- sourceMap : sourceMapEnabled ,
363+ mode : getTypeScriptParsingMode ( ) ,
364+ sourceMap : getOptionValue ( '--enable-source-maps' ) ,
372365 filename,
373366 } ;
374- const { code, map } = parse ( source , options ) ;
367+ const { code, map } = parseTypeScript ( source , options ) ;
375368 if ( map ) {
376369 // TODO(@marco-ippolito) When Buffer.transcode supports utf8 to
377370 // base64 transformation, we should change this line.
0 commit comments