@@ -8,42 +8,33 @@ import * as b from '../../../../utils/builders.js';
88 * @param {ComponentContext } context
99 */
1010export function IfBlock ( node , context ) {
11- const { state } = context ;
12-
1311 const consequent = /** @type {BlockStatement } */ ( context . visit ( node . consequent ) ) ;
12+ const alternate = node . alternate && /** @type {BlockStatement } */ ( context . visit ( node . alternate ) ) ;
1413
15- state . template . push_quasi ( '<!>' ) ;
14+ context . state . template . push_quasi ( '<!>' ) ;
1615
1716 // compile to if rather than $.if for non-reactive if statements
1817 // NOTE: doesn't handle else/elseif yet or mismatches. also, this probably breaks transition locality
1918 if ( ! node . metadata . expression . has_state && ! node . elseif ) {
20- state . init . push (
19+ context . state . init . push (
2120 b . block ( [
22- b . let ( b . id ( '$$anchor' ) , state . node ) ,
21+ b . let ( b . id ( '$$anchor' ) , context . state . node ) ,
2322 b . stmt ( b . call ( '$.next' ) ) ,
24- b . if (
25- node . test ,
26- consequent ,
27- node . alternate ? /** @type {BlockStatement } */ ( context . visit ( node . alternate ) ) : undefined
28- )
23+ b . if ( node . test , consequent , alternate ?? undefined )
2924 ] )
3025 ) ;
3126
3227 return ;
3328 }
3429
3530 const args = [
36- state . node ,
31+ context . state . node ,
3732 b . thunk ( /** @type {Expression } */ ( context . visit ( node . test ) ) ) ,
3833 b . arrow ( [ b . id ( '$$anchor' ) ] , consequent )
3934 ] ;
4035
41- if ( node . alternate || node . elseif ) {
42- args . push (
43- node . alternate
44- ? b . arrow ( [ b . id ( '$$anchor' ) ] , /** @type {BlockStatement } */ ( context . visit ( node . alternate ) ) )
45- : b . literal ( null )
46- ) ;
36+ if ( alternate || node . elseif ) {
37+ args . push ( alternate ? b . arrow ( [ b . id ( '$$anchor' ) ] , alternate ) : b . literal ( null ) ) ;
4738 }
4839
4940 if ( node . elseif ) {
@@ -71,5 +62,5 @@ export function IfBlock(node, context) {
7162 args . push ( b . literal ( true ) ) ;
7263 }
7364
74- state . init . push ( b . stmt ( b . call ( '$.if' , ...args ) ) ) ;
65+ context . state . init . push ( b . stmt ( b . call ( '$.if' , ...args ) ) ) ;
7566}
0 commit comments