@@ -3,6 +3,7 @@ import read_expression from '../read/expression.js';
3
3
import { error } from '../../../errors.js' ;
4
4
import { create_fragment } from '../utils/create.js' ;
5
5
import { walk } from 'zimmerframe' ;
6
+ import { parse } from '../acorn.js' ;
6
7
7
8
const regex_whitespace_with_closing_curly_brace = / ^ \s * } / ;
8
9
@@ -549,29 +550,50 @@ function special(parser) {
549
550
}
550
551
551
552
if ( parser . eat ( 'const' ) ) {
552
- parser . allow_whitespace ( ) ;
553
+ // {@const a = b }
554
+ const start_index = parser . index - 5 ;
555
+ parser . require_whitespace ( ) ;
553
556
554
- const id = read_context ( parser ) ;
555
- parser . allow_whitespace ( ) ;
557
+ let end_index = parser . index ;
558
+ /** @type {import('estree').VariableDeclaration | undefined } */
559
+ let declaration = undefined ;
556
560
557
- parser . eat ( '=' , true ) ;
558
- 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
+ }
559
579
560
- const init = read_expression ( parser ) ;
561
- 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
+ }
562
587
588
+ parser . index = end_index ;
563
589
parser . eat ( '}' , true ) ;
564
590
565
591
parser . append (
566
592
/** @type {import('#compiler').ConstTag } */ ( {
567
593
type : 'ConstTag' ,
568
594
start,
569
595
end : parser . index ,
570
- declaration : {
571
- type : 'VariableDeclaration' ,
572
- kind : 'const' ,
573
- declarations : [ { type : 'VariableDeclarator' , id, init } ]
574
- }
596
+ declaration
575
597
} )
576
598
) ;
577
599
}
0 commit comments