Skip to content

Commit 540a31c

Browse files
committed
fix: ensure computed props are wrapped in derived
1 parent b20b461 commit 540a31c

File tree

6 files changed

+57
-3
lines changed

6 files changed

+57
-3
lines changed

.changeset/sharp-tomatoes-learn.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: ensure computed props are wrapped in derived

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -782,13 +782,20 @@ function serialize_inline_component(node, component_name, context) {
782782
if (attribute.metadata.dynamic) {
783783
let arg = value;
784784

785-
const contains_call_expression =
785+
// When we have a non-simple computation, anything other than an Identifier or Member expression,
786+
// then there's a good chance it needs to be memoized to avoid over-firing when read within the
787+
// child component.
788+
const should_wrap_in_derived =
786789
Array.isArray(attribute.value) &&
787790
attribute.value.some((n) => {
788-
return n.type === 'ExpressionTag' && n.metadata.contains_call_expression;
791+
return (
792+
n.type === 'ExpressionTag' &&
793+
n.expression.type !== 'Identifier' &&
794+
n.expression.type !== 'MemberExpression'
795+
);
789796
});
790797

791-
if (contains_call_expression) {
798+
if (should_wrap_in_derived) {
792799
const id = b.id(context.state.scope.generate(attribute.name));
793800
context.state.init.push(b.var(id, b.call('$.derived', b.thunk(value))));
794801
arg = b.call('$.get', id);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<script>
2+
import { log } from './log.js';
3+
4+
const {active} = $props();
5+
$effect.pre(() => {
6+
log.push('active changed', active)
7+
});
8+
</script>
9+
10+
<p>Item is {active ? 'active' : 'inactive'}</p>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { test } from '../../test';
2+
import { flushSync } from 'svelte';
3+
import { log } from './log.js';
4+
5+
export default test({
6+
before_test() {
7+
log.length = 0;
8+
},
9+
10+
async test({ assert, target }) {
11+
log.length = 0;
12+
13+
const input = /** @type {HTMLInputElement} */ (target.querySelector('input'));
14+
input.value = '1';
15+
flushSync(() => input.dispatchEvent(new window.Event('input')));
16+
17+
assert.deepEqual(log, ['active changed', false, 'active changed', true]);
18+
}
19+
});
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/** @type {any[]} */
2+
export const log = [];
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<script>
2+
import Item from './Item.svelte';
3+
let activeIndex = $state(0);
4+
</script>
5+
6+
<input bind:value={activeIndex} type="number" min="0" max={4} />
7+
<Item active={activeIndex == 0} />
8+
<Item active={activeIndex == 1} />
9+
<Item active={activeIndex == 2} />
10+
<Item active={activeIndex == 3} />
11+
<Item active={activeIndex == 4} />

0 commit comments

Comments
 (0)