Skip to content

Commit d7dbccc

Browse files
committed
more tests and tweaks
1 parent dda350a commit d7dbccc

File tree

5 files changed

+71
-1
lines changed

5 files changed

+71
-1
lines changed

packages/svelte/src/compiler/phases/3-transform/client/visitors/SvelteBoundary.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,25 @@ export function SvelteBoundary(node, context) {
2727

2828
for (const attribute of node.attributes) {
2929
if (attribute.type === 'SpreadAttribute') {
30+
const value = /** @type {Expression} */ (context.visit(attribute.expression, context.state));
3031
has_spread = true;
31-
props_and_spreads.push(attribute.expression);
32+
33+
if (attribute.metadata.expression.has_state) {
34+
props_and_spreads.push(b.thunk(value));
35+
} else {
36+
props_and_spreads.push(value);
37+
}
3238
continue;
3339
}
40+
3441
if (
3542
attribute.type !== 'Attribute' ||
3643
attribute.value === true ||
3744
Array.isArray(attribute.value)
3845
) {
3946
continue;
4047
}
48+
4149
if (attribute.name === 'onerror' || attribute.name === 'failed') {
4250
const value = /** @type {Expression} */ (
4351
context.visit(attribute.value.expression, context.state)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { flushSync } from 'svelte';
2+
import { test } from '../../test';
3+
4+
export default test({
5+
test({ assert, target, logs }) {
6+
const [btn1, btn2] = target.querySelectorAll('button');
7+
8+
btn2?.click();
9+
btn1?.click();
10+
flushSync();
11+
12+
assert.deepEqual(logs, ['error caught!!!']);
13+
}
14+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<script>
2+
function throw_error() {
3+
throw new Error('test')
4+
}
5+
6+
let count = $state(0);
7+
let onerror = $state((e) => console.log('error caught'));
8+
</script>
9+
10+
<svelte:boundary {onerror}>
11+
{count > 0 ? throw_error() : null}
12+
</svelte:boundary>
13+
14+
<button onclick={() => count++}>+</button>
15+
<button onclick={() => onerror = () => console.log('error caught!!!')}>change error message</button>
16+
17+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { flushSync } from 'svelte';
2+
import { test } from '../../test';
3+
4+
export default test({
5+
test({ assert, target, logs }) {
6+
const [btn1, btn2] = target.querySelectorAll('button');
7+
8+
btn2?.click();
9+
btn1?.click();
10+
flushSync();
11+
12+
assert.deepEqual(logs, ['error caught!!!']);
13+
}
14+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<script>
2+
function throw_error() {
3+
throw new Error('test')
4+
}
5+
6+
let count = $state(0);
7+
let props = $state({ onerror: (e) => console.log('error caught') });
8+
</script>
9+
10+
<svelte:boundary {...props}>
11+
{count > 0 ? throw_error() : null}
12+
</svelte:boundary>
13+
14+
<button onclick={() => count++}>+</button>
15+
<button onclick={() => props = { onerror: () => console.log('error caught!!!') }}>change error message</button>
16+
17+

0 commit comments

Comments
 (0)