Skip to content

Commit a9ffe15

Browse files
committed
cleaner error messages
1 parent c8294cc commit a9ffe15

File tree

3 files changed

+42
-28
lines changed

3 files changed

+42
-28
lines changed
Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/** @import { Expression, MemberExpression, SequenceExpression, SpreadElement, Literal, Super, UpdateExpression, ExpressionStatement } from 'estree' */
1+
/** @import { CallExpression, Expression, SpreadElement, Super } from 'estree' */
22
/** @import { ComponentClientTransformState } from '../client/types.js' */
33
/** @import { ComponentServerTransformState } from '../server/types.js' */
44
import * as b from '#compiler/builders';
@@ -17,20 +17,33 @@ export function handle_spread_binding(spread_expression, state, visit) {
1717
const visited_expression = /** @type {Expression} */ (visit(spread_expression.argument));
1818
state.init.push(b.const(id, visited_expression));
1919

20-
// Create conditional expressions that work for both arrays and objects
21-
// Array.isArray($$bindings) ? $$bindings[0] : $$bindings.get
22-
const get = b.conditional(
23-
b.call('Array.isArray', id),
24-
b.member(id, b.literal(0), true),
25-
b.member(id, b.id('get'))
26-
);
20+
const noop = b.arrow([], b.block([]));
21+
22+
// Generate helper variables for clearer error messages
23+
const get = b.id(state.scope.generate(id.name + '_get'));
24+
const set = b.id(state.scope.generate(id.name + '_set'));
2725

28-
// Array.isArray($$bindings) ? $$bindings[1] : $$bindings.set
29-
const set = b.conditional(
30-
b.call('Array.isArray', id),
31-
b.member(id, b.literal(1), true),
32-
b.member(id, b.id('set'))
26+
const getter = b.logical(
27+
'??',
28+
b.conditional(
29+
b.call('Array.isArray', id),
30+
b.member(id, b.literal(0), true),
31+
b.member(id, b.id('get'))
32+
),
33+
noop
3334
);
35+
const setter = b.logical(
36+
'??',
37+
b.conditional(
38+
b.call('Array.isArray', id),
39+
b.member(id, b.literal(1), true),
40+
b.member(id, b.id('set'))
41+
),
42+
noop
43+
);
44+
45+
state.init.push(b.const(get, getter));
46+
state.init.push(b.const(set, setter));
3447

3548
return { get, set };
3649
}

packages/svelte/tests/runtime-runes/samples/bind-spread/_config.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ export default test({
99

1010
assert.htmlEqual(target.innerHTML, `<input type="checkbox" >`.repeat(checkboxes.length));
1111

12-
flushSync(() => {
13-
checkboxes.forEach((checkbox) => checkbox.click());
14-
});
12+
checkboxes.forEach((checkbox) => checkbox.click());
13+
1514
assert.deepEqual(logs, [
1615
'getArrayBindings',
1716
'getObjectBindings',
Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,35 @@
11
<script>
22
let check = $state(true);
33
4-
let check_bindings = [
5-
() => check,
6-
(v) => {
7-
console.log('check', v);
8-
check = v;
9-
}
10-
];
4+
const get = () => check;
5+
const set = (v) => {
6+
console.log('check', v);
7+
check = v;
8+
};
9+
const bindings = [get, set];
10+
const nested = {deep: {
11+
bindings: [get, set],}
12+
};
1113
1214
function getArrayBindings() {
1315
console.log('getArrayBindings');
14-
return check_bindings;
16+
return [get, set];
1517
}
1618
1719
function getObjectBindings() {
1820
console.log('getObjectBindings');
19-
const [get, set] = check_bindings;
2021
return { get, set };
2122
}
2223
</script>
2324

25+
<input type="checkbox" bind:checked={get, set} />
2426

25-
<input type="checkbox" bind:checked={check_bindings[0], check_bindings[1]} />
27+
<input type="checkbox" bind:checked={...bindings} />
2628

27-
<input type="checkbox" bind:checked={...check_bindings} />
29+
<input type="checkbox" bind:checked={...nested.deep.bindings} />
2830

2931
<input type="checkbox" bind:checked={...getArrayBindings()} />
3032

31-
<input type="checkbox" bind:checked={...(() => check_bindings)()} />
33+
<input type="checkbox" bind:checked={...(() => [get, set])()} />
3234

3335
<input type="checkbox" bind:checked={...getObjectBindings()} />

0 commit comments

Comments
 (0)