@@ -550,50 +550,31 @@ function special(parser) {
550
550
}
551
551
552
552
if ( parser . eat ( 'const' ) ) {
553
- // {@const a = b }
554
- const start_index = parser . index - 5 ;
555
- parser . require_whitespace ( ) ;
553
+ parser . allow_whitespace ( ) ;
556
554
557
- let end_index = parser . index ;
558
- /** @type {import('estree').VariableDeclaration | undefined } */
559
- let declaration = undefined ;
555
+ const id = read_context ( parser ) ;
556
+ parser . allow_whitespace ( ) ;
560
557
561
- // Can't use parse_expression_at here, so we try to parse until we find the correct range
562
- const dummy_spaces = parser . template . substring ( 0 , start_index ) . replace ( / [ ^ \n ] / g, ' ' ) ;
563
- while ( true ) {
564
- end_index = parser . template . indexOf ( '}' , end_index + 1 ) ;
565
- if ( end_index === - 1 ) break ;
566
- try {
567
- const node = parse (
568
- dummy_spaces + parser . template . substring ( start_index , end_index ) ,
569
- parser . ts
570
- ) . body [ 0 ] ;
571
- if ( node ?. type === 'VariableDeclaration' ) {
572
- declaration = node ;
573
- break ;
574
- }
575
- } catch ( e ) {
576
- continue ;
577
- }
578
- }
558
+ parser . eat ( '=' , true ) ;
559
+ parser . allow_whitespace ( ) ;
579
560
580
- if (
581
- declaration === undefined ||
582
- declaration . declarations . length !== 1 ||
583
- declaration . declarations [ 0 ] . init === undefined
584
- ) {
585
- error ( start , 'invalid-const' ) ;
586
- }
561
+ const init = read_expression ( parser ) ;
562
+ parser . allow_whitespace ( ) ;
587
563
588
- parser . index = end_index ;
589
564
parser . eat ( '}' , true ) ;
590
565
591
566
parser . append (
592
567
/** @type {import('#compiler').ConstTag } */ ( {
593
568
type : 'ConstTag' ,
594
569
start,
595
570
end : parser . index ,
596
- declaration
571
+ declaration : {
572
+ type : 'VariableDeclaration' ,
573
+ kind : 'const' ,
574
+ declarations : [ { type : 'VariableDeclarator' , id, init } ] ,
575
+ start : start + 1 ,
576
+ end : parser . index - 1
577
+ }
597
578
} )
598
579
) ;
599
580
}
0 commit comments