You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fix: proxify values when assigning using ||=, &&= and ??= operators (#14273)
* add failing test for #14268
* simplify
* proxify values when using ||=, &&= and ??= assignment operators
* proxify values assigned to private state fields
* changeset
* fix
* fix
* add warning
* update test
Copy file name to clipboardExpand all lines: documentation/docs/98-reference/.generated/client-warnings.md
+32Lines changed: 32 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,37 @@
1
1
<!-- This file is generated by scripts/process-messages/index.js. Do not edit! -->
2
2
3
+
### assignment_value_stale
4
+
5
+
```
6
+
Assignment to `%property%` property (%location%) will evaluate to the right-hand side, not the value of `%property%` following the assignment. This may result in unexpected behaviour.
7
+
```
8
+
9
+
Given a case like this...
10
+
11
+
```svelte
12
+
<script>
13
+
let object = $state({ array: null });
14
+
15
+
function add() {
16
+
(object.array ??= []).push(object.array.length);
17
+
}
18
+
</script>
19
+
20
+
<button onclick={add}>add</button>
21
+
<p>items: {JSON.stringify(object.items)}</p>
22
+
```
23
+
24
+
...the array being pushed to when the button is first clicked is the `[]` on the right-hand side of the assignment, but the resulting value of `object.array` is an empty state proxy. As a result, the pushed value will be discarded.
25
+
26
+
You can fix this by separating it into two statements:
Copy file name to clipboardExpand all lines: packages/svelte/messages/client-warnings/warnings.md
+30Lines changed: 30 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,3 +1,33 @@
1
+
## assignment_value_stale
2
+
3
+
> Assignment to `%property%` property (%location%) will evaluate to the right-hand side, not the value of `%property%` following the assignment. This may result in unexpected behaviour.
4
+
5
+
Given a case like this...
6
+
7
+
```svelte
8
+
<script>
9
+
let object = $state({ array: null });
10
+
11
+
function add() {
12
+
(object.array ??= []).push(object.array.length);
13
+
}
14
+
</script>
15
+
16
+
<button onclick={add}>add</button>
17
+
<p>items: {JSON.stringify(object.items)}</p>
18
+
```
19
+
20
+
...the array being pushed to when the button is first clicked is the `[]` on the right-hand side of the assignment, but the resulting value of `object.array` is an empty state proxy. As a result, the pushed value will be discarded.
21
+
22
+
You can fix this by separating it into two statements:
23
+
24
+
```js
25
+
functionadd() {
26
+
object.array??= [];
27
+
object.array.push(object.array.length);
28
+
}
29
+
```
30
+
1
31
## binding_property_non_reactive
2
32
3
33
> `%binding%` is binding to a non-reactive property
Copy file name to clipboardExpand all lines: packages/svelte/src/internal/client/warnings.js
+14Lines changed: 14 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -5,6 +5,20 @@ import { DEV } from 'esm-env';
5
5
varbold='font-weight: bold';
6
6
varnormal='font-weight: normal';
7
7
8
+
/**
9
+
* Assignment to `%property%` property (%location%) will evaluate to the right-hand side, not the value of `%property%` following the assignment. This may result in unexpected behaviour.
console.warn(`%c[svelte] assignment_value_stale\n%cAssignment to \`${property}\` property (${location}) will evaluate to the right-hand side, not the value of \`${property}\` following the assignment. This may result in unexpected behaviour.`,bold,normal);
16
+
}else{
17
+
// TODO print a link to the documentation
18
+
console.warn("assignment_value_stale");
19
+
}
20
+
}
21
+
8
22
/**
9
23
* `%binding%` (%location%) is binding to a non-reactive property
'Assignment to `items` property (main.svelte:5:24) will evaluate to the right-hand side, not the value of `items` following the assignment. This may result in unexpected behaviour.'
0 commit comments