@@ -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 ) ;
@@ -283,16 +292,17 @@ export function EachBlock(node, context) {
283292 ) ;
284293 }
285294
295+ const render_args = [ b . id ( '$$anchor' ) , item ] ;
296+ if ( uses_index || collection_id ) render_args . push ( index ) ;
297+ if ( collection_id ) render_args . push ( collection_id ) ;
298+
286299 /** @type {Expression[] } */
287300 const args = [
288301 context . state . node ,
289302 b . literal ( flags ) ,
290- each_node_meta . array_name ? each_node_meta . array_name : b . thunk ( collection ) ,
303+ b . thunk ( collection ) ,
291304 key_function ,
292- b . arrow (
293- uses_index ? [ b . id ( '$$anchor' ) , item , index ] : [ b . id ( '$$anchor' ) , item ] ,
294- b . block ( declarations . concat ( block . body ) )
295- )
305+ b . arrow ( render_args , b . block ( declarations . concat ( block . body ) ) )
296306 ] ;
297307
298308 if ( node . fallback ) {
0 commit comments