@@ -284,39 +284,6 @@ function processExports(mdx, node, mapping, esm) {
284
284
return esm + '\n'
285
285
}
286
286
287
- /**
288
- * @param {Program | undefined } expression
289
- * @returns {boolean }
290
- */
291
- function hasAwaitExpression ( expression ) {
292
- let awaitExpression = false
293
- if ( expression ) {
294
- walk ( expression , {
295
- enter ( node ) {
296
- if (
297
- awaitExpression ||
298
- node . type === 'ArrowFunctionExpression' ||
299
- node . type === 'FunctionDeclaration' ||
300
- node . type === 'FunctionExpression'
301
- ) {
302
- this . skip ( )
303
- return
304
- }
305
-
306
- if (
307
- node . type === 'AwaitExpression' ||
308
- ( node . type === 'ForOfStatement' && node . await )
309
- ) {
310
- awaitExpression = true
311
- this . skip ( )
312
- }
313
- }
314
- } )
315
- }
316
-
317
- return awaitExpression
318
- }
319
-
320
287
/**
321
288
* @param {string } mdx
322
289
* @param {Root } ast
@@ -472,6 +439,73 @@ function getEmbeddedCodes(mdx, ast, checkMdx, jsxImportSource) {
472
439
updateMarkdownFromOffsets ( startOffset , endOffset )
473
440
}
474
441
442
+ /**
443
+ * @param {Program } program
444
+ * @param {number } lastIndex
445
+ * @returns {number }
446
+ */
447
+ function processJsxExpression ( program , lastIndex ) {
448
+ let newIndex = lastIndex
449
+ let functionNesting = 0
450
+ walk ( program , {
451
+ enter ( node ) {
452
+ switch ( node . type ) {
453
+ case 'JSXIdentifier' : {
454
+ if ( ! isInjectableComponent ( node . name , variables ) ) {
455
+ return
456
+ }
457
+
458
+ jsx =
459
+ addOffset ( jsxMapping , mdx , jsx , newIndex , node . start ) +
460
+ '_components.'
461
+ newIndex = node . start
462
+ break
463
+ }
464
+
465
+ case 'ArrowFunctionExpression' :
466
+ case 'FunctionDeclaration' :
467
+ case 'FunctionExpression' : {
468
+ functionNesting ++
469
+ break
470
+ }
471
+
472
+ case 'AwaitExpression' : {
473
+ if ( ! functionNesting ) {
474
+ hasAwait = true
475
+ }
476
+
477
+ break
478
+ }
479
+
480
+ case 'ForOfStatement' : {
481
+ if ( ! functionNesting ) {
482
+ hasAwait ||= node . await
483
+ }
484
+
485
+ break
486
+ }
487
+
488
+ default :
489
+ }
490
+ } ,
491
+
492
+ leave ( node ) {
493
+ switch ( node . type ) {
494
+ case 'ArrowFunctionExpression' :
495
+ case 'FunctionDeclaration' :
496
+ case 'FunctionExpression' : {
497
+ functionNesting --
498
+ break
499
+ }
500
+
501
+ default :
502
+ }
503
+ }
504
+ } )
505
+
506
+ return newIndex
507
+ }
508
+
475
509
visit (
476
510
ast ,
477
511
( node ) => {
@@ -525,38 +559,53 @@ function getEmbeddedCodes(mdx, ast, checkMdx, jsxImportSource) {
525
559
}
526
560
527
561
updateMarkdownFromOffsets ( start , end )
562
+
563
+ let lastIndex = start + 1
564
+ jsx = addOffset ( jsxMapping , mdx , jsx + jsxIndent , start , lastIndex )
528
565
if ( isInjectableComponent ( node . name , variables ) ) {
529
- const openingStart = start + 1
566
+ jsx += '_components.'
567
+ }
568
+
569
+ if ( node . name ) {
530
570
jsx = addOffset (
531
571
jsxMapping ,
532
572
mdx ,
533
- addOffset ( jsxMapping , mdx , jsx + jsxIndent , start , openingStart ) +
534
- '_components.' ,
535
- openingStart ,
536
- end
573
+ jsx ,
574
+ lastIndex ,
575
+ lastIndex + node . name . length
537
576
)
538
- } else {
539
- jsx = addOffset ( jsxMapping , mdx , jsx + jsxIndent , start , end )
577
+ lastIndex += node . name . length
578
+ }
579
+
580
+ for ( const attribute of node . attributes ) {
581
+ if ( typeof attribute . value !== 'object' ) {
582
+ continue
583
+ }
584
+
585
+ const program = attribute . value ?. data ?. estree
586
+
587
+ if ( program ) {
588
+ lastIndex = processJsxExpression ( program , lastIndex )
589
+ }
540
590
}
541
591
592
+ jsx = addOffset ( jsxMapping , mdx , jsx , lastIndex , end )
542
593
break
543
594
}
544
595
545
596
case 'mdxFlowExpression' :
546
597
case 'mdxTextExpression' : {
547
598
updateMarkdownFromNode ( node )
548
599
const program = node . data ?. estree
600
+ jsx += jsxIndent
549
601
550
- if ( program ?. body . length === 0 ) {
551
- jsx = addOffset ( jsxMapping , mdx , jsx + jsxIndent , start , start + 1 )
602
+ if ( program ?. body . length ) {
603
+ const newIndex = processJsxExpression ( program , start )
604
+ jsx = addOffset ( jsxMapping , mdx , jsx , newIndex , end )
605
+ } else {
606
+ jsx = addOffset ( jsxMapping , mdx , jsx , start , start + 1 )
552
607
jsx = addOffset ( jsxMapping , mdx , jsx , end - 1 , end )
553
608
esm = addOffset ( esmMapping , mdx , esm , start + 1 , end - 1 ) + '\n'
554
- } else {
555
- if ( program ) {
556
- hasAwait ||= hasAwaitExpression ( program )
557
- }
558
-
559
- jsx = addOffset ( jsxMapping , mdx , jsx + jsxIndent , start , end )
560
609
}
561
610
562
611
break
0 commit comments