Skip to content

Commit 74f8e26

Browse files
authored
fix: prevent false positive non-state warnings for bind:this (#10674)
`bind:this` doesn't need to be a state reference if it will never change fixes #10435
1 parent 1ac3135 commit 74f8e26

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed

packages/svelte/src/compiler/phases/2-analyze/index.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,25 @@ export function analyze_component(root, options) {
460460
) {
461461
continue inner;
462462
}
463+
// bind:this doesn't need to be a state reference if it will never change
464+
if (
465+
type === 'BindDirective' &&
466+
/** @type {import('#compiler').BindDirective} */ (path[i]).name === 'this'
467+
) {
468+
for (let j = i - 1; j >= 0; j -= 1) {
469+
const type = path[j].type;
470+
if (
471+
type === 'IfBlock' ||
472+
type === 'EachBlock' ||
473+
type === 'AwaitBlock' ||
474+
type === 'KeyBlock'
475+
) {
476+
warn(warnings, binding.node, [], 'non-state-reference', name);
477+
continue outer;
478+
}
479+
}
480+
continue inner;
481+
}
463482
}
464483

465484
warn(warnings, binding.node, [], 'non-state-reference', name);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { test } from '../../test';
2+
3+
export default test({});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<script>
2+
let no_need;
3+
let does_need1;
4+
let does_need2 = $state();
5+
</script>
6+
7+
<div bind:this={no_need}></div>
8+
{#if true}
9+
<div bind:this={does_need1}></div>
10+
<div bind:this={does_need2}></div>
11+
{/if}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[
2+
{
3+
"code": "non-state-reference",
4+
"message": "does_need1 is updated, but is not declared with $state(...). Changing its value will not correctly trigger updates.",
5+
"start": {
6+
"column": 5,
7+
"line": 3
8+
},
9+
"end": {
10+
"column": 15,
11+
"line": 3
12+
}
13+
}
14+
]

0 commit comments

Comments
 (0)