From dfb30aaddd7c46b943dac4ee324f818de1918713 Mon Sep 17 00:00:00 2001 From: Simon H <5968653+dummdidumm@users.noreply.github.com> Date: Fri, 3 May 2024 07:40:43 +0200 Subject: [PATCH] fix: only warn about non-reactive state in runes mode (#11434) Fixes #11269 --- .changeset/serious-bobcats-carry.md | 5 ++ .../src/compiler/phases/2-analyze/index.js | 86 +++++++++---------- 2 files changed, 48 insertions(+), 43 deletions(-) create mode 100644 .changeset/serious-bobcats-carry.md diff --git a/.changeset/serious-bobcats-carry.md b/.changeset/serious-bobcats-carry.md new file mode 100644 index 0000000000..d67abe1f82 --- /dev/null +++ b/.changeset/serious-bobcats-carry.md @@ -0,0 +1,5 @@ +--- +"svelte": patch +--- + +fix: only warn about non-reactive state in runes mode diff --git a/packages/svelte/src/compiler/phases/2-analyze/index.js b/packages/svelte/src/compiler/phases/2-analyze/index.js index c7b92d4af0..e4d6f6a4db 100644 --- a/packages/svelte/src/compiler/phases/2-analyze/index.js +++ b/packages/svelte/src/compiler/phases/2-analyze/index.js @@ -449,6 +449,49 @@ export function analyze_component(root, source, options) { merge(set_scope(scopes), validation_runes, runes_scope_tweaker, common_visitors) ); } + + // warn on any nonstate declarations that are a) reassigned and b) referenced in the template + for (const scope of [module.scope, instance.scope]) { + outer: for (const [name, binding] of scope.declarations) { + if (binding.kind === 'normal' && binding.reassigned) { + inner: for (const { path } of binding.references) { + if (path[0].type !== 'Fragment') continue; + for (let i = 1; i < path.length; i += 1) { + const type = path[i].type; + if ( + type === 'FunctionDeclaration' || + type === 'FunctionExpression' || + type === 'ArrowFunctionExpression' + ) { + continue inner; + } + // bind:this doesn't need to be a state reference if it will never change + if ( + type === 'BindDirective' && + /** @type {import('#compiler').BindDirective} */ (path[i]).name === 'this' + ) { + for (let j = i - 1; j >= 0; j -= 1) { + const type = path[j].type; + if ( + type === 'IfBlock' || + type === 'EachBlock' || + type === 'AwaitBlock' || + type === 'KeyBlock' + ) { + w.non_reactive_update(binding.node, name); + continue outer; + } + } + continue inner; + } + } + + w.non_reactive_update(binding.node, name); + continue outer; + } + } + } + } } else { instance.scope.declare(b.id('$$props'), 'rest_prop', 'synthetic'); instance.scope.declare(b.id('$$restProps'), 'rest_prop', 'synthetic'); @@ -508,49 +551,6 @@ export function analyze_component(root, source, options) { e.slot_snippet_conflict(analysis.slot_names.values().next().value); } - // warn on any nonstate declarations that are a) reassigned and b) referenced in the template - for (const scope of [module.scope, instance.scope]) { - outer: for (const [name, binding] of scope.declarations) { - if (binding.kind === 'normal' && binding.reassigned) { - inner: for (const { path } of binding.references) { - if (path[0].type !== 'Fragment') continue; - for (let i = 1; i < path.length; i += 1) { - const type = path[i].type; - if ( - type === 'FunctionDeclaration' || - type === 'FunctionExpression' || - type === 'ArrowFunctionExpression' - ) { - continue inner; - } - // bind:this doesn't need to be a state reference if it will never change - if ( - type === 'BindDirective' && - /** @type {import('#compiler').BindDirective} */ (path[i]).name === 'this' - ) { - for (let j = i - 1; j >= 0; j -= 1) { - const type = path[j].type; - if ( - type === 'IfBlock' || - type === 'EachBlock' || - type === 'AwaitBlock' || - type === 'KeyBlock' - ) { - w.non_reactive_update(binding.node, name); - continue outer; - } - } - continue inner; - } - } - - w.non_reactive_update(binding.node, name); - continue outer; - } - } - } - } - if (analysis.css.ast) { analyze_css(analysis.css.ast, analysis);