@ -24,15 +24,15 @@ export function ConstTag(node, context) {
expression = b . call ( '$.tag' , expression , b . literal ( declaration . id . name ) ) ;
expression = b . call ( '$.tag' , expression , b . literal ( declaration . id . name ) ) ;
}
}
context . state . consts . push ( b . const ( declaration . id , expression ) ) ;
context . state . transform [ declaration . id . name ] = { read : get _value } ;
context . state . transform [ declaration . id . name ] = { read : get _value } ;
// we need to eagerly evaluate the expression in order to hit any
add _const _declaration (
// 'Cannot access x before initialization' errors
context . state ,
if ( dev ) {
declaration . id ,
context . state . consts . push ( b . stmt ( b . call ( '$.get' , declaration . id ) ) ) ;
expression ,
}
node . metadata . expression . has _await ,
context . state . scope . get _bindings ( declaration )
) ;
} else {
} else {
const identifiers = extract _identifiers ( declaration . id ) ;
const identifiers = extract _identifiers ( declaration . id ) ;
const tmp = b . id ( context . state . scope . generate ( 'computed_const' ) ) ;
const tmp = b . id ( context . state . scope . generate ( 'computed_const' ) ) ;
@ -69,13 +69,13 @@ export function ConstTag(node, context) {
expression = b . call ( '$.tag' , expression , b . literal ( '[@const]' ) ) ;
expression = b . call ( '$.tag' , expression , b . literal ( '[@const]' ) ) ;
}
}
context. state . consts . push ( b . const ( tmp , expression ) ) ;
add_const _declaration (
context . state ,
// we need to eagerly evaluate the expression in order to hit any
tmp ,
// 'Cannot access x before initialization' errors
expression ,
if ( dev ) {
node . metadata . expression . has _await ,
context . state . consts. push ( b . stmt ( b . call ( '$.get' , tmp ) ) ) ;
context . state . scope. get _bindings ( declaration )
}
);
for ( const node of identifiers ) {
for ( const node of identifiers ) {
context . state . transform [ node . name ] = {
context . state . transform [ node . name ] = {
@ -84,3 +84,39 @@ export function ConstTag(node, context) {
}
}
}
}
}
}
/ * *
* @ param { ComponentContext [ 'state' ] } state
* @ param { import ( 'estree' ) . Identifier } id
* @ param { import ( 'estree' ) . Expression } expression
* @ param { boolean } has _await
* @ param { import ( '#compiler' ) . Binding [ ] } bindings
* /
function add _const _declaration ( state , id , expression , has _await , bindings ) {
// we need to eagerly evaluate the expression in order to hit any
// 'Cannot access x before initialization' errors
const after = dev ? [ b . stmt ( b . call ( '$.get' , id ) ) ] : [ ] ;
if ( has _await || state . async _consts ) {
const run = ( state . async _consts ? ? = {
id : b . id ( state . scope . generate ( 'promises' ) ) ,
thunks : [ ]
} ) ;
state . consts . push ( b . let ( id ) ) ;
const assignment = b . assignment ( '=' , id , expression ) ;
const body = after . length === 0 ? assignment : b . block ( [ b . stmt ( assignment ) , ... after ] ) ;
run . thunks . push ( b . thunk ( body , has _await ) ) ;
const blocker = b . member ( run . id , b . literal ( run . thunks . length - 1 ) , true ) ;
for ( const binding of bindings ) {
binding . blocker = blocker ;
}
} else {
state . consts . push ( b . const ( id , expression ) ) ;
state . consts . push ( ... after ) ;
}
}