File tree 2 files changed +60
-0
lines changed
2 files changed +60
-0
lines changed Original file line number Diff line number Diff line change @@ -602,9 +602,33 @@ export function recmaDocument(options) {
602
602
argument = argument . children [ 0 ]
603
603
}
604
604
605
+ let awaitExpression = false
606
+
607
+ walk ( argument , {
608
+ enter ( node ) {
609
+ if (
610
+ node . type === 'ArrowFunctionExpression' ||
611
+ node . type === 'FunctionDeclaration' ||
612
+ node . type === 'FunctionExpression'
613
+ ) {
614
+ return this . skip ( )
615
+ }
616
+
617
+ if (
618
+ node . type === 'AwaitExpression' ||
619
+ /* c8 ignore next 2 -- can only occur in a function (which then can
620
+ * only be async, so skipped it) */
621
+ ( node . type === 'ForOfStatement' && node . await )
622
+ ) {
623
+ awaitExpression = true
624
+ }
625
+ }
626
+ } )
627
+
605
628
return [
606
629
{
607
630
type : 'FunctionDeclaration' ,
631
+ async : awaitExpression ,
608
632
id : { type : 'Identifier' , name : '_createMdxContent' } ,
609
633
params : [ { type : 'Identifier' , name : 'props' } ] ,
610
634
body : {
Original file line number Diff line number Diff line change @@ -973,6 +973,42 @@ test('@mdx-js/mdx: compile', async function (t) {
973
973
}
974
974
)
975
975
976
+ await t . test (
977
+ 'should support an `await` expression in content (GH-2242)' ,
978
+ async function ( ) {
979
+ const element = React . createElement (
980
+ await run ( await compile ( '{await Promise.resolve(42)}' ) )
981
+ )
982
+
983
+ try {
984
+ // Not supported yet by React, so it throws.
985
+ renderToStaticMarkup ( element )
986
+ assert . fail ( )
987
+ } catch ( error ) {
988
+ assert . match (
989
+ String ( error ) ,
990
+ / O b j e c t s a r e n o t v a l i d a s a R e a c t c h i l d \( f o u n d : \[ o b j e c t P r o m i s e ] \) /
991
+ )
992
+ }
993
+ }
994
+ )
995
+
996
+ await t . test (
997
+ 'should not detect `await` inside functions (GH-2242)' ,
998
+ async function ( ) {
999
+ const value = `{(function () {
1000
+ return 21
1001
+
1002
+ async function unused() {
1003
+ await Promise.resolve(42)
1004
+ }
1005
+ })()}`
1006
+ const element = React . createElement ( await run ( await compile ( value ) ) )
1007
+
1008
+ assert . equal ( renderToStaticMarkup ( element ) , '21' )
1009
+ }
1010
+ )
1011
+
976
1012
await t . test ( 'should support source maps' , async function ( ) {
977
1013
const base = new URL ( 'context/' , import . meta. url )
978
1014
const url = new URL ( 'sourcemap.js' , base )
You can’t perform that action at this time.
0 commit comments