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