@@ -9,18 +9,18 @@ import { should_proxy } from '../utils.js';
99 * @param {Context } context
1010 */
1111export function ObjectExpression ( node , context ) {
12- /**
13- * @typedef {[string, NonNullable<ReturnType<typeof get_rune>>] } ReactiveProperty
14- */
1512let has_runes = false ;
1613const valid_property_runes = [ '$state' , '$derived' , '$state.raw' , '$derived.by' ] ;
1714/** @type {Statement[] } */
1815const body = [ ] ;
19- /** @type {Map<Property, ReactiveProperty> } */
20- const sources = new Map ( ) ;
2116let counter = 0 ;
17+ /** @type {(Property | SpreadElement)[] } */
18+ const properties = [ ] ;
2219for ( let property of node . properties ) {
23- if ( property . type !== 'Property' ) continue ;
20+ if ( property . type !== 'Property' ) {
21+ properties . push ( /** @type {SpreadElement } */ ( context . visit ( property ) ) ) ;
22+ continue ;
23+ }
2424const rune = get_rune ( property . value , context . state . scope ) ;
2525if ( rune && valid_property_runes . includes ( rune ) ) {
2626has_runes = true ;
@@ -30,41 +30,23 @@ export function ObjectExpression(node, context) {
3030let value = /** @type {Expression } */ (
3131context . visit ( /** @type {CallExpression } */ ( property . value ) . arguments [ 0 ] ?? b . void0 )
3232) ;
33+ const key = /** @type {Expression } */ ( context . visit ( property . key ) ) ;
3334value =
3435rune === '$derived'
3536? b . thunk ( value )
3637: rune === '$state' && should_proxy ( value , context . state . scope )
3738? b . call ( '$.proxy' , value )
3839: value ;
39- /** @type {ReactiveProperty } */
40- const source = [ name , rune ] ;
41- sources . set ( property , source ) ;
42- body . push ( b . let ( name , b . call ( call , value ) ) ) ;
43- }
44- }
45- if ( ! has_runes ) {
46- context . next ( ) ;
47- return ;
48- }
49- /** @type {(Property | SpreadElement)[] } */
50- const properties = [ ] ;
51- for ( let property of node . properties ) {
52- if ( property . type === 'SpreadElement' ) {
53- properties . push ( /** @type {SpreadElement } */ ( context . visit ( property ) ) ) ;
54- continue ;
55- }
56- if ( sources . has ( property ) ) {
57- const [ name , rune ] = /** @type {ReactiveProperty } */ ( sources . get ( property ) ) ;
5840properties . push (
5941b . prop (
6042'get' ,
61- /** @type { Expression } */ ( context . visit ( /** @type { Expression } */ ( property . key ) ) ) ,
43+ key ,
6244b . function ( null , [ ] , b . block ( [ b . return ( b . call ( '$.get' , b . id ( name ) ) ) ] ) ) ,
6345property . computed
6446) ,
6547b . prop (
6648'set' ,
67- /** @type { Expression } */ ( context . visit ( property . key ) ) ,
49+ key ,
6850b . function (
6951null ,
7052[ b . id ( '$$value' ) ] ,
@@ -77,10 +59,15 @@ export function ObjectExpression(node, context) {
7759property . computed
7860)
7961) ;
62+ body . push ( b . let ( name , b . call ( call , value ) ) ) ;
8063} else {
8164properties . push ( /** @type {Property } */ ( context . visit ( property ) ) ) ;
8265}
8366}
67+ if ( ! has_runes ) {
68+ context . next ( ) ;
69+ return ;
70+ }
8471body . push ( b . return ( b . object ( properties ) ) ) ;
8572return b . call ( b . arrow ( [ ] , b . block ( body ) ) ) ;
8673}
0 commit comments