Skip to content

Commit 0cf6d56

Browse files
fix: increment and decrement edge case (#11506)
* fix: increment and decrement edge case * fix/simplify test * simplify * Apply suggestions from code review * golf --------- Co-authored-by: Rich Harris <[email protected]> Co-authored-by: Rich Harris <[email protected]>
1 parent 8318b3d commit 0cf6d56

File tree

4 files changed

+51
-4
lines changed

4 files changed

+51
-4
lines changed

.changeset/chilled-seas-jog.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+
fix: coerce incremented/decremented sources

packages/svelte/src/internal/client/runtime.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,7 +1011,7 @@ function get_parent_context(component_context) {
10111011
* @returns {number}
10121012
*/
10131013
export function update(signal, d = 1) {
1014-
const value = get(signal);
1014+
var value = +get(signal);
10151015
set(signal, value + d);
10161016
return value;
10171017
}
@@ -1022,9 +1022,7 @@ export function update(signal, d = 1) {
10221022
* @returns {number}
10231023
*/
10241024
export function update_pre(signal, d = 1) {
1025-
const value = get(signal) + d;
1026-
set(signal, value);
1027-
return value;
1025+
return set(signal, +get(signal) + d);
10281026
}
10291027

10301028
/**
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { flushSync } from 'svelte';
2+
import { test } from '../../test';
3+
4+
export default test({
5+
html: `
6+
<button>update</button>
7+
<p>0, 0, 0, 0</p>
8+
`,
9+
10+
test({ target, assert, logs }) {
11+
const btn = target.querySelector('button');
12+
flushSync(() => btn?.click());
13+
14+
assert.htmlEqual(
15+
target.innerHTML,
16+
`
17+
<button>update</button>
18+
<p>1, -1, 1, -1</p>
19+
`
20+
);
21+
22+
assert.deepEqual(logs, [0, 0, 1, -1]);
23+
}
24+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<script>
2+
let a = $state("0");
3+
let b = $state("0");
4+
let c = $state("0");
5+
let d = $state("0");
6+
7+
function update() {
8+
// @ts-expect-error
9+
console.log(a++);
10+
// @ts-expect-error
11+
console.log(b--);
12+
// @ts-expect-error
13+
console.log(++c);
14+
// @ts-expect-error
15+
console.log(--d);
16+
}
17+
</script>
18+
19+
<button onclick={update}>update</button>
20+
<p>{a}, {b}, {c}, {d}</p>

0 commit comments

Comments
 (0)