@@ -550,31 +550,50 @@ function special(parser) {
550550 }
551551
552552 if ( parser . eat ( 'const' ) ) {
553- parser . allow_whitespace ( ) ;
553+ // {@const a = b }
554+ const start_index = parser . index - 5 ;
555+ parser . require_whitespace ( ) ;
554556
555- const id = read_context ( parser ) ;
556- parser . allow_whitespace ( ) ;
557+ let end_index = parser . index ;
558+ /** @type {import('estree').VariableDeclaration | undefined } */
559+ let declaration = undefined ;
557560
558- parser . eat ( '=' , true ) ;
559- parser . allow_whitespace ( ) ;
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+ }
560579
561- const init = read_expression ( parser ) ;
562- parser . allow_whitespace ( ) ;
580+ if (
581+ declaration === undefined ||
582+ declaration . declarations . length !== 1 ||
583+ declaration . declarations [ 0 ] . init === undefined
584+ ) {
585+ error ( start , 'invalid-const' ) ;
586+ }
563587
588+ parser . index = end_index ;
564589 parser . eat ( '}' , true ) ;
565590
566591 parser . append (
567592 /** @type {import('#compiler').ConstTag } */ ( {
568593 type : 'ConstTag' ,
569594 start,
570595 end : parser . index ,
571- declaration : {
572- type : 'VariableDeclaration' ,
573- kind : 'const' ,
574- declarations : [ { type : 'VariableDeclarator' , id, init } ] ,
575- start : start + 1 ,
576- end : parser . index - 1
577- }
596+ declaration
578597 } )
579598 ) ;
580599 }
0 commit comments