From 74f735003d578809e6b2623bd9342a1cfb32f5fe Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Thu, 29 May 2025 22:36:57 -0400 Subject: [PATCH] more conservative --- .../phases/2-analyze/visitors/EachBlock.js | 28 +++++++++++++------ .../samples/each-block-const/output.svelte | 2 +- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/packages/svelte/src/compiler/phases/2-analyze/visitors/EachBlock.js b/packages/svelte/src/compiler/phases/2-analyze/visitors/EachBlock.js index dc5e398974..e1a044ee88 100644 --- a/packages/svelte/src/compiler/phases/2-analyze/visitors/EachBlock.js +++ b/packages/svelte/src/compiler/phases/2-analyze/visitors/EachBlock.js @@ -2,6 +2,7 @@ /** @import { Context } from '../types' */ /** @import { Scope } from '../../scope' */ import * as e from '../../../errors.js'; +import { extract_identifiers } from '../../../utils/ast.js'; import { mark_subtree_dynamic } from './shared/fragment.js'; import { validate_block_not_empty, validate_opening_tag } from './shared/utils.js'; @@ -38,21 +39,32 @@ export function EachBlock(node, context) { if (node.key) context.visit(node.key); if (node.fallback) context.visit(node.fallback); + let mutated = false; + // collect transitive dependencies... for (const binding of node.metadata.expression.dependencies) { collect_transitive_dependencies(binding, node.metadata.transitive_deps); } + if (node.context) { + for (const id of extract_identifiers(node.context)) { + const binding = context.state.scope.get(id.name); + if (binding?.mutated) mutated = true; + } + } + // ...and ensure they are marked as state, so they can be turned // into mutable sources and invalidated - for (const binding of node.metadata.transitive_deps) { - if ( - binding.kind === 'normal' && - (binding.declaration_kind === 'const' || - binding.declaration_kind === 'let' || - binding.declaration_kind === 'var') - ) { - binding.kind = 'state'; + if (mutated) { + for (const binding of node.metadata.transitive_deps) { + if ( + binding.kind === 'normal' && + (binding.declaration_kind === 'const' || + binding.declaration_kind === 'let' || + binding.declaration_kind === 'var') + ) { + binding.kind = 'state'; + } } } diff --git a/packages/svelte/tests/migrate/samples/each-block-const/output.svelte b/packages/svelte/tests/migrate/samples/each-block-const/output.svelte index df153d8dd6..d2057d6f19 100644 --- a/packages/svelte/tests/migrate/samples/each-block-const/output.svelte +++ b/packages/svelte/tests/migrate/samples/each-block-const/output.svelte @@ -1,5 +1,5 @@ {#each foo as f}{/each}