Skip to content

Commit 570f649

Browse files
authored
fix: add props to state_referenced_locally (#17266)
* add props to state_referenced_locally * changeset
1 parent a72167b commit 570f649

File tree

4 files changed

+53
-1
lines changed

4 files changed

+53
-1
lines changed

.changeset/curly-phones-cross.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
add props to state_referenced_locally

packages/svelte/src/compiler/phases/2-analyze/visitors/Identifier.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ export function Identifier(node, context) {
114114
binding.initial.arguments[0].type !== 'SpreadElement' &&
115115
!should_proxy(binding.initial.arguments[0], context.state.scope)))) ||
116116
binding.kind === 'raw_state' ||
117-
binding.kind === 'derived') &&
117+
binding.kind === 'derived' ||
118+
binding.kind === 'prop') &&
118119
// We're only concerned with reads here
119120
(parent.type !== 'AssignmentExpression' || parent.left !== node) &&
120121
parent.type !== 'UpdateExpression'

packages/svelte/tests/validator/samples/static-state-reference/input.svelte

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,21 @@
88
console.log(count);
99
console.log(doubled);
1010
11+
let {
12+
prop
13+
} = $props();
14+
let prop_state = $state(prop);
15+
let prop_derived = $derived(prop);
16+
console.log(prop);
17+
console.log(prop_derived);
18+
1119
// writes are okay
1220
count++;
1321
count = 1;
1422
obj.a++;
1523
obj.a = 1;
24+
prop_state = 1;
25+
prop_derived = 1;
1626
1727
// `count` here is correctly identified as a non-reference
1828
let typed: { count: number } | null = null;

packages/svelte/tests/validator/samples/static-state-reference/warnings.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,41 @@
3434
"column": 20,
3535
"line": 9
3636
}
37+
},
38+
{
39+
"code": "state_referenced_locally",
40+
"end": {
41+
"column": 29,
42+
"line": 14
43+
},
44+
"message": "This reference only captures the initial value of `prop`. Did you mean to reference it inside a closure instead?",
45+
"start": {
46+
"column": 25,
47+
"line": 14
48+
}
49+
},
50+
{
51+
"code": "state_referenced_locally",
52+
"end": {
53+
"column": 17,
54+
"line": 16
55+
},
56+
"message": "This reference only captures the initial value of `prop`. Did you mean to reference it inside a closure instead?",
57+
"start": {
58+
"column": 13,
59+
"line": 16
60+
}
61+
},
62+
{
63+
"code": "state_referenced_locally",
64+
"end": {
65+
"column": 25,
66+
"line": 17
67+
},
68+
"message": "This reference only captures the initial value of `prop_derived`. Did you mean to reference it inside a closure instead?",
69+
"start": {
70+
"column": 13,
71+
"line": 17
72+
}
3773
}
3874
]

0 commit comments

Comments
 (0)