@@ -394,33 +394,42 @@ function parseSourceAST(
394
394
hookParsedMetadata . originalSourceCode =
395
395
sourceMetadata . originalSourceCode ;
396
396
} else {
397
- // TypeScript is the most commonly used typed JS variant so let's default to it
398
- // unless we detect explicit Flow usage via the "@flow" pragma.
399
- const plugin =
400
- originalSourceCode . indexOf ( '@flow' ) > 0 ? 'flow' : 'typescript' ;
401
-
402
- // TODO (named hooks) This is probably where we should check max source length,
403
- // rather than in loadSourceAndMetatada -> loadSourceFiles().
404
- const originalSourceAST = withSyncPerfMeasurements (
405
- '[@babel/parser] parse(originalSourceCode)' ,
406
- ( ) =>
407
- parse ( originalSourceCode , {
408
- sourceType : 'unambiguous' ,
409
- plugins : [ 'jsx' , plugin ] ,
410
- } ) ,
411
- ) ;
412
- hookParsedMetadata . originalSourceAST = originalSourceAST ;
397
+ try {
398
+ // TypeScript is the most commonly used typed JS variant so let's default to it
399
+ // unless we detect explicit Flow usage via the "@flow" pragma.
400
+ const plugin =
401
+ originalSourceCode . indexOf ( '@flow' ) > 0 ? 'flow' : 'typescript' ;
402
+
403
+ // TODO (named hooks) This is probably where we should check max source length,
404
+ // rather than in loadSourceAndMetatada -> loadSourceFiles().
405
+ // TODO(#22319): Support source files that are html files with inline script tags.
406
+ const originalSourceAST = withSyncPerfMeasurements (
407
+ '[@babel/parser] parse(originalSourceCode)' ,
408
+ ( ) =>
409
+ parse ( originalSourceCode , {
410
+ errorRecovery : true ,
411
+ sourceType : 'unambiguous' ,
412
+ plugins : [ 'jsx' , plugin ] ,
413
+ } ) ,
414
+ ) ;
415
+ hookParsedMetadata . originalSourceAST = originalSourceAST ;
413
416
414
- if ( __DEBUG__ ) {
415
- console . log (
416
- `parseSourceAST() Caching source metadata for "${ originalSourceURL } "` ,
417
+ if ( __DEBUG__ ) {
418
+ console . log (
419
+ `parseSourceAST() Caching source metadata for "${ originalSourceURL } "` ,
420
+ ) ;
421
+ }
422
+
423
+ originalURLToMetadataCache . set ( originalSourceURL , {
424
+ originalSourceAST,
425
+ originalSourceCode,
426
+ } ) ;
427
+ } catch ( error ) {
428
+ throw new Error (
429
+ `Failed to parse source file: ${ originalSourceURL } \n\n` +
430
+ `Original error: ${ error } ` ,
417
431
) ;
418
432
}
419
-
420
- originalURLToMetadataCache . set ( originalSourceURL , {
421
- originalSourceAST,
422
- originalSourceCode,
423
- } ) ;
424
433
}
425
434
} ,
426
435
) ;
0 commit comments