@@ -9,18 +9,18 @@ import { should_proxy } from '../utils.js';
9
9
* @param {Context } context
10
10
*/
11
11
export function ObjectExpression ( node , context ) {
12
- /**
13
- * @typedef {[string, NonNullable<ReturnType<typeof get_rune>>] } ReactiveProperty
14
- */
15
12
let has_runes = false ;
16
13
const valid_property_runes = [ '$state' , '$derived' , '$state.raw' , '$derived.by' ] ;
17
14
/** @type {Statement[] } */
18
15
const body = [ ] ;
19
- /** @type {Map<Property, ReactiveProperty> } */
20
- const sources = new Map ( ) ;
21
16
let counter = 0 ;
17
+ /** @type {(Property | SpreadElement)[] } */
18
+ const properties = [ ] ;
22
19
for ( 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
+ }
24
24
const rune = get_rune ( property . value , context . state . scope ) ;
25
25
if ( rune && valid_property_runes . includes ( rune ) ) {
26
26
has_runes = true ;
@@ -30,41 +30,23 @@ export function ObjectExpression(node, context) {
30
30
let value = /** @type {Expression } */ (
31
31
context . visit ( /** @type {CallExpression } */ ( property . value ) . arguments [ 0 ] ?? b . void0 )
32
32
) ;
33
+ const key = /** @type {Expression } */ ( context . visit ( property . key ) ) ;
33
34
value =
34
35
rune === '$derived'
35
36
? b . thunk ( value )
36
37
: rune === '$state' && should_proxy ( value , context . state . scope )
37
38
? b . call ( '$.proxy' , value )
38
39
: 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 ) ) ;
58
40
properties . push (
59
41
b . prop (
60
42
'get' ,
61
- /** @type { Expression } */ ( context . visit ( /** @type { Expression } */ ( property . key ) ) ) ,
43
+ key ,
62
44
b . function ( null , [ ] , b . block ( [ b . return ( b . call ( '$.get' , b . id ( name ) ) ) ] ) ) ,
63
45
property . computed
64
46
) ,
65
47
b . prop (
66
48
'set' ,
67
- /** @type { Expression } */ ( context . visit ( property . key ) ) ,
49
+ key ,
68
50
b . function (
69
51
null ,
70
52
[ b . id ( '$$value' ) ] ,
@@ -77,10 +59,15 @@ export function ObjectExpression(node, context) {
77
59
property . computed
78
60
)
79
61
) ;
62
+ body . push ( b . let ( name , b . call ( call , value ) ) ) ;
80
63
} else {
81
64
properties . push ( /** @type {Property } */ ( context . visit ( property ) ) ) ;
82
65
}
83
66
}
67
+ if ( ! has_runes ) {
68
+ context . next ( ) ;
69
+ return ;
70
+ }
84
71
body . push ( b . return ( b . object ( properties ) ) ) ;
85
72
return b . call ( b . arrow ( [ ] , b . block ( body ) ) ) ;
86
73
}
0 commit comments