@@ -2,8 +2,8 @@ import read_context from '../read/context.js';
22import read_expression from '../read/expression.js' ;
33import { error } from '../../../errors.js' ;
44import { create_fragment } from '../utils/create.js' ;
5- import { parse_expression_at } from '../acorn.js' ;
65import { walk } from 'zimmerframe' ;
6+ import { parse } from '../acorn.js' ;
77
88const regex_whitespace_with_closing_curly_brace = / ^ \s * } / ;
99
@@ -532,21 +532,54 @@ function special(parser) {
532532 // {@const a = b }
533533 parser . require_whitespace ( ) ;
534534
535- const expression = read_expression ( parser ) ;
535+ const CONST_LENGTH = 'const ' . length ;
536+ parser . index = parser . index - CONST_LENGTH ;
537+
538+ let end_index = parser . index ;
539+ /** @type {import('estree').VariableDeclaration | undefined } */
540+ let declaration = undefined ;
536541
537- if ( ! ( expression . type === 'AssignmentExpression' && expression . operator === '=' ) ) {
542+ const dummy_spaces = parser . template . substring ( 0 , parser . index ) . replace ( / [ ^ \n ] / g, ' ' ) ;
543+ while ( true ) {
544+ end_index = parser . template . indexOf ( '}' , end_index + 1 ) ;
545+ if ( end_index === - 1 ) break ;
546+ try {
547+ const node = parse (
548+ dummy_spaces + parser . template . substring ( parser . index , end_index ) ,
549+ parser . ts
550+ ) . body [ 0 ] ;
551+ if ( node ?. type === 'VariableDeclaration' ) {
552+ declaration = node ;
553+ break ;
554+ }
555+ } catch ( e ) {
556+ continue ;
557+ }
558+ }
559+
560+ if (
561+ declaration === undefined ||
562+ declaration . declarations . length !== 1 ||
563+ declaration . declarations [ 0 ] . init === undefined
564+ ) {
538565 error ( start , 'invalid-const' ) ;
539566 }
540567
541- parser . allow_whitespace ( ) ;
568+ parser . index = end_index ;
542569 parser . eat ( '}' , true ) ;
543570
571+ const id = declaration . declarations [ 0 ] . id ;
572+ if ( id . type === 'Identifier' ) {
573+ // Tidy up some stuff left behind by acorn-typescript
574+ id . end = ( id . start ?? 0 ) + id . name . length ;
575+ }
576+
544577 parser . append (
545578 /** @type {import('#compiler').ConstTag } */ ( {
546579 type : 'ConstTag' ,
547580 start,
548581 end : parser . index ,
549- expression
582+ declaration
550583 } )
551584 ) ;
552585 }
0 commit comments