@@ -91,50 +91,32 @@ export function CallExpression(node, context) {
9191
9292 break ;
9393 case '$effect' :
94- case '$effect.pre' : {
95- const grand_parent = /** @type {SvelteNode } */ ( get_parent ( context . path , - 2 ) ) ;
96-
94+ case '$effect.pre' :
95+ case '$effect.lazy' : {
9796 if ( parent . type !== 'ExpressionStatement' ) {
9897 e . effect_invalid_placement ( node ) ;
9998 }
10099
101- if ( rune === '$effect.pre' ) {
102- if ( node . arguments . length !== 1 && node . arguments . length !== 2 ) {
103- e . rune_invalid_arguments_length ( node , rune , 'exactly one or two arguments' ) ;
100+ if ( rune === '$effect.lazy' ) {
101+ if ( node . arguments . length !== 2 ) {
102+ e . rune_invalid_arguments_length ( node , rune , 'exactly two arguments' ) ;
103+ }
104+ const state_ref = node . arguments [ 0 ] ;
105+
106+ if ( state_ref . type !== 'Identifier' ) {
107+ throw new Error ( 'Bad $effect.lazy, first argument must be an identifier' ) ;
104108 }
109+ const scope = context . state . scope ;
110+ const binding = /** @type {Binding } */ ( scope . get ( state_ref . name ) ) ;
105111
106- if ( node . arguments . length === 2 ) {
107- const linked = node . arguments [ 1 ] ;
108-
109- if ( linked . type !== 'ObjectExpression' ) {
110- throw new Error ( 'Bad $effect.pre, second argument must be an object' ) ;
111- }
112- const scope = context . state . scope ;
113-
114- for ( const property of linked . properties ) {
115- if (
116- property . type !== 'Property' ||
117- property . computed ||
118- property . key . type !== 'Identifier' ||
119- property . key . name !== 'bind' ||
120- property . value . type !== 'Identifier'
121- ) {
122- throw new Error (
123- 'Bad $effect.pre, second argument must be an object simply properties'
124- ) ;
125- }
126- const binding = /** @type {Binding } */ ( scope . get ( property . value . name ) ) ;
127-
128- if ( binding === null || binding . kind !== 'state' ) {
129- throw new Error ( 'Bad $effect.pre link, must be a local reference to $state' ) ;
130- }
131- const link = scope . generate ( 'effect' ) ;
132- ( binding . linked_effects ??= [ ] ) . push ( link ) ;
133- /** @type {SimpleCallExpression } */ ( node ) . metadata = {
134- link
135- } ;
136- }
112+ if ( binding === null || binding . kind !== 'state' ) {
113+ throw new Error ( 'Bad $effect.lazy link, must be a local reference to $state' ) ;
137114 }
115+ const id = scope . generate ( 'effect' ) ;
116+ ( binding . linked_effects ??= [ ] ) . push ( id ) ;
117+ /** @type {SimpleCallExpression } */ ( node ) . metadata = {
118+ id
119+ } ;
138120 } else if ( node . arguments . length !== 1 ) {
139121 e . rune_invalid_arguments_length ( node , rune , 'exactly one argument' ) ;
140122 }
@@ -203,7 +185,7 @@ export function CallExpression(node, context) {
203185 }
204186
205187 // `$inspect(foo)` or `$derived(foo) should not trigger the `static-state-reference` warning
206- if ( rune === '$inspect' || rune === '$derived' || rune === '$effect.pre ' ) {
188+ if ( rune === '$inspect' || rune === '$derived' || rune === '$effect.lazy ' ) {
207189 context . next ( { ...context . state , function_depth : context . state . function_depth + 1 } ) ;
208190 } else {
209191 context . next ( ) ;
0 commit comments