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
pull/9522/merge
Simon H 4 months ago committed by GitHub
parent 1ac313594c
commit 74f8e261c6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -460,6 +460,25 @@ export function analyze_component(root, options) {
) {
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'
) {
warn(warnings, binding.node, [], 'non-state-reference', name);
continue outer;
}
}
continue inner;
}
}
warn(warnings, binding.node, [], 'non-state-reference', name);

@ -0,0 +1,3 @@
import { test } from '../../test';
export default test({});

@ -0,0 +1,11 @@
<script>
let no_need;
let does_need1;
let does_need2 = $state();
</script>
<div bind:this={no_need}></div>
{#if true}
<div bind:this={does_need1}></div>
<div bind:this={does_need2}></div>
{/if}

@ -0,0 +1,14 @@
[
{
"code": "non-state-reference",
"message": "does_need1 is updated, but is not declared with $state(...). Changing its value will not correctly trigger updates.",
"start": {
"column": 5,
"line": 3
},
"end": {
"column": 15,
"line": 3
}
}
]
Loading…
Cancel
Save