1- /** @import { Expression, Pattern, Identifier } from 'estree' */
1+ /** @import { Expression, Pattern } from 'estree' */
22/** @import { AST } from '#compiler' */
33/** @import { ComponentContext } from '../types' */
44import { dev } from '../../../../state.js' ;
@@ -12,29 +12,29 @@ import { get_value } from './shared/declarations.js';
1212 * @param {ComponentContext } context
1313 */
1414export function ConstTag ( node , context ) {
15- /** @type {Identifier } */
16- let id ;
17-
1815 const declaration = node . declaration . declarations [ 0 ] ;
1916 // TODO we can almost certainly share some code with $derived(...)
2017 if ( declaration . id . type === 'Identifier' ) {
21- id = declaration . id ;
2218 context . state . init . push (
2319 b . const (
24- id ,
20+ declaration . id ,
2521 create_derived (
2622 context . state ,
2723 b . thunk ( /** @type {Expression } */ ( context . visit ( declaration . init ) ) )
2824 )
2925 )
3026 ) ;
3127
32- context . state . transform [ id . name ] = { read : get_value } ;
28+ context . state . transform [ declaration . id . name ] = { read : get_value } ;
3329
34- id = declaration . id ;
30+ // we need to eagerly evaluate the expression in order to hit any
31+ // 'Cannot access x before initialization' errors
32+ if ( dev ) {
33+ context . state . init . push ( b . stmt ( b . call ( '$.get' , declaration . id ) ) ) ;
34+ }
3535 } else {
3636 const identifiers = extract_identifiers ( declaration . id ) ;
37- id = b . id ( context . state . scope . generate ( 'computed_const' ) ) ;
37+ const tmp = b . id ( context . state . scope . generate ( 'computed_const' ) ) ;
3838
3939 const transform = { ...context . state . transform } ;
4040
@@ -59,26 +59,18 @@ export function ConstTag(node, context) {
5959 ] )
6060 ) ;
6161
62- context . state . init . push ( b . const ( id , create_derived ( context . state , fn ) ) ) ;
62+ context . state . init . push ( b . const ( tmp , create_derived ( context . state , fn ) ) ) ;
63+
64+ // we need to eagerly evaluate the expression in order to hit any
65+ // 'Cannot access x before initialization' errors
66+ if ( dev ) {
67+ context . state . init . push ( b . stmt ( b . call ( '$.get' , tmp ) ) ) ;
68+ }
6369
6470 for ( const node of identifiers ) {
6571 context . state . transform [ node . name ] = {
66- read : ( node ) => b . member ( b . call ( '$.get' , id ) , node )
72+ read : ( node ) => b . member ( b . call ( '$.get' , tmp ) , node )
6773 } ;
6874 }
6975 }
70-
71- // we need to eagerly evaluate the expression in order to hit any
72- // 'Cannot access x before initialization' errors
73- if ( dev ) {
74- const parent = context . path . at ( - 1 ) ;
75- const boundary = parent ?. type === 'SvelteBoundary' ? parent : null ;
76- const statement = b . stmt ( b . call ( '$.get' , id ) ) ;
77- if ( boundary ) {
78- boundary . const_dev_statements ||= [ ] ;
79- boundary . const_dev_statements . push ( statement ) ;
80- } else {
81- context . state . init . push ( statement ) ;
82- }
83- }
8476}
0 commit comments