@@ -15,6 +15,7 @@ const {
15
15
const {
16
16
ERR_INVALID_ARG_TYPE ,
17
17
ERR_INVALID_RETURN_PROPERTY_VALUE ,
18
+ ERR_INVALID_TYPESCRIPT_SYNTAX ,
18
19
} = require ( 'internal/errors' ) . codes ;
19
20
const { BuiltinModule } = require ( 'internal/bootstrap/realm' ) ;
20
21
@@ -312,44 +313,37 @@ function getBuiltinModule(id) {
312
313
return normalizedId ? require ( normalizedId ) : undefined ;
313
314
}
314
315
315
- /**
316
- * TypeScript parsing function, by default Amaro.transformSync.
317
- * @type {Function }
318
- */
319
- let typeScriptParser ;
320
316
/**
321
317
* The TypeScript parsing mode, either 'strip-only' or 'transform'.
322
318
* @type {string }
323
319
*/
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
+ ) ;
330
323
331
324
/**
332
325
* Load the TypeScript parser.
333
- * @param {Function } parser - A function that takes a string of TypeScript code
334
326
* and returns an object with a `code` property.
335
327
* @returns {Function } The TypeScript parser function.
336
328
*/
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
+ } ) ;
341
333
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 ) ;
351
346
}
352
- return typeScriptParser ;
353
347
}
354
348
355
349
/**
@@ -364,14 +358,13 @@ function loadTypeScriptParser(parser) {
364
358
*/
365
359
function stripTypeScriptTypes ( source , filename ) {
366
360
assert ( typeof source === 'string' ) ;
367
- const parse = loadTypeScriptParser ( ) ;
368
361
const options = {
369
362
__proto__ : null ,
370
- mode : typeScriptParsingMode ,
371
- sourceMap : sourceMapEnabled ,
363
+ mode : getTypeScriptParsingMode ( ) ,
364
+ sourceMap : getOptionValue ( '--enable-source-maps' ) ,
372
365
filename,
373
366
} ;
374
- const { code, map } = parse ( source , options ) ;
367
+ const { code, map } = parseTypeScript ( source , options ) ;
375
368
if ( map ) {
376
369
// TODO(@marco-ippolito) When Buffer.transcode supports utf8 to
377
370
// base64 transformation, we should change this line.
0 commit comments