@@ -427,22 +427,75 @@ describe('when working with Worker threads', () => {
427427 } ) ;
428428} ) ;
429429
430- describe ( 'cjs & esm ambiguous syntax case' , ( ) => {
431- it ( 'should throw an ambiguous syntax error when using top-level await with require' , async ( ) => {
432- const { stderr, code, signal } = await spawnPromisified (
433- process . execPath ,
434- [
435- '--input-type=module' ,
436- '--eval' ,
437- `await 1;\nconst fs = require('fs');` ,
438- ]
439- ) ;
430+ describe ( 'maybe top-level await syntax errors that are not recognized as top-level await errors' , ( ) => {
431+ const expressions = [
432+ // string
433+ { expression : '""' } ,
434+ // number
435+ { expression : '0' } ,
436+ // boolean
437+ { expression : 'true' } ,
438+ // null
439+ { expression : 'null' } ,
440+ // undefined
441+ { expression : 'undefined' } ,
442+ // object
443+ { expression : '{}' } ,
444+ // array
445+ { expression : '[]' } ,
446+ // new
447+ { expression : 'new Date()' } ,
448+ // identifier
449+ { initialize : 'const a = 2;' , expression : 'a' } ,
450+ ] ;
451+ it ( 'should not crash the process' , async ( ) => {
452+ for ( const { expression, initialize } of expressions ) {
453+ const wrapperExpressions = [
454+ `function callAwait() {}; callAwait(await ${ expression } );` ,
455+ `if (await ${ expression } ) {}` ,
456+ `{ key: await ${ expression } }` ,
457+ `[await ${ expression } ]` ,
458+ `(await ${ expression } )` ,
459+ ] ;
460+ for ( const wrapperExpression of wrapperExpressions ) {
461+ const { code, signal, stdout, stderr } = await spawnPromisified ( process . execPath , [
462+ '--eval' ,
463+ `
464+ ${ initialize || '' }
465+ ${ wrapperExpression }
466+ ` ,
467+ ] ) ;
468+
469+ strictEqual ( stderr , '' ) ;
470+ strictEqual ( stdout , '' ) ;
471+ strictEqual ( code , 0 ) ;
472+ strictEqual ( signal , null ) ;
473+ }
474+ }
475+ } ) ;
440476
441- match (
442- stderr ,
443- / R e f e r e n c e E r r o r : C a n n o t d e t e r m i n e i n t e n d e d m o d u l e f o r m a t b e c a u s e b o t h r e q u i r e \( \) a n d t o p - l e v e l a w a i t a r e p r e s e n t \. I f t h e c o d e i s i n t e n d e d t o b e C o m m o n J S , w r a p a w a i t i n a n a s y n c f u n c t i o n \. I f t h e c o d e i s i n t e n d e d t o b e a n E S m o d u l e , r e p l a c e r e q u i r e \( \) w i t h i m p o r t \. /
444- ) ;
477+ it ( 'should crash when the expression is not valid' , async ( ) => {
478+ let { code, signal, stdout, stderr } = await spawnPromisified ( process . execPath , [
479+ '--eval' ,
480+ `
481+ function callAwait() {}
482+ callAwait(await "" "");
483+ ` ,
484+ ] ) ;
485+ match ( stderr , / S y n t a x E r r o r : m i s s i n g \) a f t e r a r g u m e n t l i s t / ) ;
486+ strictEqual ( stdout , '' ) ;
487+ strictEqual ( code , 1 ) ;
488+ strictEqual ( signal , null ) ;
445489
490+ ( { code, signal, stdout, stderr } = await spawnPromisified ( process . execPath , [
491+ '--eval' ,
492+ `
493+ function callAwait() {}
494+ if (a "") {}
495+ ` ,
496+ ] ) ) ;
497+ match ( stderr , / S y n t a x E r r o r : U n e x p e c t e d s t r i n g / ) ;
498+ strictEqual ( stdout , '' ) ;
446499 strictEqual ( code , 1 ) ;
447500 strictEqual ( signal , null ) ;
448501 } ) ;
0 commit comments