33import * as b from '../../../../utils/builders.js' ;
44import { get_rune } from '../../../scope.js' ;
55import { should_proxy } from '../utils.js' ;
6+ import { walk } from 'zimmerframe' ;
67
78/**
89 * @param {ObjectExpression } node
@@ -25,24 +26,47 @@ export function ObjectExpression(node, context) {
2526 } ) ;
2627 }
2728 }
28- if ( ! has_runes ) return ;
29+ if ( ! has_runes ) {
30+ context . next ( ) ;
31+ return ;
32+ }
2933 let body = [ ] ;
3034 let sources = new Map ( ) ;
35+ let has_this_reference = false ;
3136 let counter = 0 ;
37+ let to_push = [ ] ;
3238 for ( let { rune, property } of reactive_properties ) {
3339 const name = context . state . scope . generate ( `$$${ ++ counter } ` ) ;
34- const deep = rune !== '$state.raw' ;
3540 const call = rune . match ( / ^ \$ s t a t e / ) ? '$.state' : '$.derived' ;
3641 /** @type {Expression } */
3742 let value = /** @type {Expression } */ ( context . visit ( property . value . arguments [ 0 ] ?? b . void0 ) ) ;
43+ value = walk ( value , null , {
44+ FunctionExpression ( ) {
45+ return ;
46+ } ,
47+ //@ts -ignore
48+ FunctionDeclaration ( ) {
49+ return ;
50+ } ,
51+ ObjectExpression ( ) {
52+ return ;
53+ } ,
54+ ThisExpression ( ) {
55+ has_this_reference = true ;
56+ return b . id ( '$$object' ) ;
57+ } ,
58+ ClassBody ( ) {
59+ return ;
60+ }
61+ } ) ;
3862 value =
3963 rune === '$derived'
4064 ? b . thunk ( value )
4165 : rune === '$state' && should_proxy ( value , context . state . scope )
4266 ? b . call ( '$.proxy' , value )
4367 : value ;
4468 sources . set ( property , [ name , rune ] ) ;
45- body . push ( b . let ( name , b . call ( call , value ) ) ) ;
69+ to_push . push ( b . let ( name , b . call ( call , value ) ) ) ;
4670 }
4771 /** @type {(Property | SpreadElement)[] } */
4872 let properties = [ ] ;
@@ -79,6 +103,13 @@ export function ObjectExpression(node, context) {
79103 properties . push ( /** @type {Property } */ ( context . visit ( property ) ) ) ;
80104 }
81105 }
82- body . push ( b . return ( b . object ( properties ) ) ) ;
106+ if ( has_this_reference ) {
107+ body . push ( b . let ( '$$object' , b . object ( properties ) ) ) ;
108+ body . push ( ...to_push ) ;
109+ body . push ( b . return ( b . id ( '$$object' ) ) ) ;
110+ } else {
111+ body . push ( ...to_push ) ;
112+ body . push ( b . return ( b . object ( properties ) ) ) ;
113+ }
83114 return b . call ( b . arrow ( [ ] , b . block ( body ) ) ) ;
84115}
0 commit comments