@@ -555,35 +555,53 @@ export function transformAST(
555
555
*/
556
556
function unwrapMacro ( node : CallExpression ) {
557
557
const argsLength = node . arguments . length
558
- s . remove ( node . callee . start ! + offset , node . callee . end ! + offset )
559
- if ( ! argsLength ) {
558
+ const bracketStart = node . callee . end ! + offset
559
+
560
+ s . remove ( node . callee . start ! + offset , bracketStart )
561
+
562
+ if ( argsLength === 1 ) {
563
+ const bracketEnd = node . arguments [ argsLength - 1 ] . end ! + offset
564
+
565
+ // remove brackets of macros
566
+ s . remove ( bracketStart , bracketStart + 1 )
567
+ s . remove ( bracketEnd , bracketEnd + 1 )
568
+
569
+ // avoid traversal of like `$$(a)`
570
+ if ( node . arguments [ 0 ] . type !== "CallExpression" ) {
571
+ return ;
572
+ }
573
+ }
574
+
575
+ // avoid invalid traversal for $
576
+ if ( ! escapeScope || node . extra ) {
560
577
return
561
- } else if ( argsLength === 1 ) {
562
- // remove brackets of macro
563
- s . remove ( node . callee . end ! + offset , node . arguments [ 0 ] . start ! + offset )
564
- s . remove ( node . arguments [ argsLength - 1 ] . end ! + offset , node . end ! + offset )
565
- } else {
566
- // handle some edge cases for macro $$
567
- // resolve nested
568
- let i = parentStack . length - 1
569
- while ( i >= 0 ) {
570
- const curParent = parentStack [ i -- ]
571
- // see @__tests__ :$$ with some edge cases
572
- if (
573
- curParent . type === 'VariableDeclarator' ||
574
- curParent . type === 'AssignmentExpression' ||
575
- ( curParent . type === 'ExpressionStatement' &&
576
- ( curParent . expression . extra ||
577
- ( curParent . expression . type === 'CallExpression' &&
578
- curParent . expression . arguments . includes ( node ) ) ) )
579
- ) {
580
- return
581
- }
578
+ }
579
+
580
+ // fix some edge cases for macro $$, eg. $$(a,b)
581
+ // resolve nested
582
+ let i = parentStack . length - 1
583
+ while ( i >= 0 ) {
584
+ const curParent = parentStack [ i -- ]
585
+ if (
586
+ curParent . type === 'ExpressionStatement' &&
587
+ isIsolateExpression ( curParent . expression , node )
588
+ ) {
589
+ // split the unwrapped code `()()` to `();()`
590
+ s . appendLeft ( node . callee . start ! + offset , ';' )
591
+ return
582
592
}
583
- s . appendLeft ( node . callee . start ! + offset , ';' )
584
593
}
585
594
}
586
595
596
+ function isIsolateExpression ( expression : Expression , node : CallExpression ) {
597
+ return (
598
+ expression . type === 'CallExpression' &&
599
+ expression . callee . type === 'Identifier' &&
600
+ expression . callee . name === escapeSymbol &&
601
+ ! expression . arguments . includes ( node )
602
+ )
603
+ }
604
+
587
605
// check root scope first
588
606
walkScope ( ast , true )
589
607
; ( walk as any ) ( ast , {
@@ -657,8 +675,8 @@ export function transformAST(
657
675
}
658
676
659
677
if ( callee === escapeSymbol ) {
660
- unwrapMacro ( node )
661
678
escapeScope = node
679
+ unwrapMacro ( node )
662
680
}
663
681
664
682
// TODO remove when out of experimental
0 commit comments