@@ -34,6 +34,11 @@ function isImplicitThis(node: angular.AST, text: string): boolean {
34
34
return start >= end || / ^ \s + $ / . test ( text . slice ( start , end ) ) ;
35
35
}
36
36
37
+ type NodeTransformOptions = {
38
+ isInParentParens ?: boolean ;
39
+ parent ?: angular . AST ;
40
+ } ;
41
+
37
42
class Transformer extends Source {
38
43
#node;
39
44
#text;
@@ -105,12 +110,19 @@ class Transformer extends Source {
105
110
) ;
106
111
}
107
112
108
- #transform< T extends NGNode > ( node : angular . AST , isInParentParens = false ) {
109
- return this . #transformNode( node , isInParentParens ) as T &
110
- LocationInformation ;
113
+ #transform< T extends NGNode > (
114
+ node : angular . AST ,
115
+ options ?: NodeTransformOptions ,
116
+ ) {
117
+ return this . #transformNode( node , options ) as T & LocationInformation ;
111
118
}
112
119
113
- #transformNode( node : angular . AST , isInParentParens = false ) : NGNode {
120
+ #transformNode( node : angular . AST , options ?: NodeTransformOptions ) : NGNode {
121
+ const { isInParentParens } = {
122
+ isInParentParens : false ,
123
+ ...options ,
124
+ } ;
125
+
114
126
if ( node instanceof angular . Interpolation ) {
115
127
const { expressions } = node ;
116
128
@@ -374,7 +386,11 @@ class Transformer extends Source {
374
386
const { receiver, args } = node ;
375
387
const tArgs =
376
388
args . length === 1
377
- ? [ this . #transform< babel . Expression > ( args [ 0 ] , true ) ]
389
+ ? [
390
+ this . #transform< babel . Expression > ( args [ 0 ] , {
391
+ isInParentParens : true ,
392
+ } ) ,
393
+ ]
378
394
: ( args as angular . AST [ ] ) . map < babel . Expression > ( ( node ) =>
379
395
this . #transform( node ) ,
380
396
) ;
@@ -523,6 +539,54 @@ class Transformer extends Source {
523
539
) ;
524
540
}
525
541
542
+ if ( node instanceof angular . TemplateLiteral ) {
543
+ const { elements, expressions } = node ;
544
+
545
+ return this . #create< babel . TemplateLiteral > ( {
546
+ type : 'TemplateLiteral' ,
547
+ quasis : elements . map ( ( element ) =>
548
+ this . #transform( element , { parent : node } ) ,
549
+ ) ,
550
+ expressions : expressions . map ( ( expression ) =>
551
+ this . #transform( expression ) ,
552
+ ) ,
553
+ ...node . sourceSpan ,
554
+ } ) ;
555
+ }
556
+
557
+ if ( node instanceof angular . TemplateLiteralElement ) {
558
+ const templateLiteral = options ! . parent ! as angular . TemplateLiteral ;
559
+ const elementIndex = templateLiteral . elements . indexOf ( node ) ;
560
+ const isFirst = elementIndex === 0 ;
561
+ const isLast = elementIndex === templateLiteral . elements . length - 1 ;
562
+ const start = isFirst
563
+ ? templateLiteral . sourceSpan . start + 1
564
+ : node . sourceSpan . start ;
565
+
566
+ let end ;
567
+ if ( isLast ) {
568
+ end = templateLiteral . sourceSpan . end - 1 ;
569
+ } else {
570
+ const nextExpression = templateLiteral . expressions [ elementIndex ] ;
571
+ end = this . getCharacterLastIndex ( '$' , nextExpression . sourceSpan . start ) ;
572
+ }
573
+ const raw = this . text . slice ( start , end ) ;
574
+
575
+ return this . #create< babel . TemplateElement > (
576
+ {
577
+ type : 'TemplateElement' ,
578
+ value : {
579
+ cooked : node . text ,
580
+ raw,
581
+ } ,
582
+ start : start ,
583
+ end : end ,
584
+ tail : isLast ,
585
+ } ,
586
+ { stripSpaces : false } ,
587
+ ) ;
588
+ }
589
+
526
590
// istanbul ignore next
527
591
throw Object . assign ( new Error ( 'Unexpected node' ) , { node } ) ;
528
592
}
0 commit comments