@@ -35,10 +35,6 @@ export function EachBlock(node, context) {
3535 context . state . template . push ( '<!>' ) ;
3636 }
3737
38- if ( each_node_meta . array_name !== null ) {
39- context . state . init . push ( b . const ( each_node_meta . array_name , b . thunk ( collection ) ) ) ;
40- }
41-
4238 let flags = 0 ;
4339
4440 if ( node . metadata . keyed && node . index ) {
@@ -120,8 +116,21 @@ export function EachBlock(node, context) {
120116 return [ array , ...transitive_dependencies ] ;
121117 } ) ;
122118
123- if ( each_node_meta . array_name ) {
124- indirect_dependencies . push ( b . call ( each_node_meta . array_name ) ) ;
119+ /** @type {Identifier | null } */
120+ let collection_id = null ;
121+
122+ // Check if inner scope shadows something from outer scope.
123+ // This is necessary because we need access to the array expression of the each block
124+ // in the inner scope if bindings are used, in order to invalidate the array.
125+ for ( const [ name ] of context . state . scope . declarations ) {
126+ if ( context . state . scope . parent ?. get ( name ) != null ) {
127+ collection_id = context . state . scope . root . unique ( '$$array' ) ;
128+ break ;
129+ }
130+ }
131+
132+ if ( collection_id ) {
133+ indirect_dependencies . push ( b . call ( collection_id ) ) ;
125134 } else {
126135 indirect_dependencies . push ( collection ) ;
127136
@@ -195,7 +204,7 @@ export function EachBlock(node, context) {
195204 // TODO 6.0 this only applies in legacy mode, reassignments are
196205 // forbidden in runes mode
197206 return b . member (
198- each_node_meta . array_name ? b . call ( each_node_meta . array_name ) : collection ,
207+ collection_id ? b . call ( collection_id ) : collection ,
199208 ( flags & EACH_INDEX_REACTIVE ) !== 0 ? get_value ( index ) : index ,
200209 true
201210 ) ;
@@ -207,7 +216,7 @@ export function EachBlock(node, context) {
207216 uses_index = true ;
208217
209218 const left = b . member (
210- each_node_meta . array_name ? b . call ( each_node_meta . array_name ) : collection ,
219+ collection_id ? b . call ( collection_id ) : collection ,
211220 ( flags & EACH_INDEX_REACTIVE ) !== 0 ? get_value ( index ) : index ,
212221 true
213222 ) ;
@@ -285,18 +294,19 @@ export function EachBlock(node, context) {
285294
286295 const { is_async } = node . metadata . expression ;
287296
288- const thunk = each_node_meta . array_name ?? b . thunk ( collection , is_async ) ;
297+ const thunk = b . thunk ( collection , is_async ) ;
298+
299+ const render_args = [ b . id ( '$$anchor' ) , item ] ;
300+ if ( uses_index || collection_id ) render_args . push ( index ) ;
301+ if ( collection_id ) render_args . push ( collection_id ) ;
289302
290303 /** @type {Expression[] } */
291304 const args = [
292305 context . state . node ,
293306 b . literal ( flags ) ,
294307 is_async ? b . thunk ( b . call ( '$.get' , b . id ( '$$collection' ) ) ) : thunk ,
295308 key_function ,
296- b . arrow (
297- uses_index ? [ b . id ( '$$anchor' ) , item , index ] : [ b . id ( '$$anchor' ) , item ] ,
298- b . block ( declarations . concat ( block . body ) )
299- )
309+ b . arrow ( render_args , b . block ( declarations . concat ( block . body ) ) )
300310 ] ;
301311
302312 if ( node . fallback ) {
0 commit comments