File tree Expand file tree Collapse file tree 5 files changed +45
-1
lines changed
src/compiler/phases/3-transform/client/visitors
tests/runtime-runes/samples/derived-fn-destructure Expand file tree Collapse file tree 5 files changed +45
-1
lines changed Original file line number Diff line number Diff line change 1+ ---
2+ " svelte " : patch
3+ ---
4+
5+ fix: correctly handle closure passed to $derived.by when destructuring
Original file line number Diff line number Diff line change @@ -301,7 +301,7 @@ export const javascript_visitors_runes = {
301301 declarations . push (
302302 b . declarator (
303303 b . id ( object_id ) ,
304- b . call ( '$.derived' , b . thunk ( rune === '$derived.by' ? b . call ( value ) : value ) )
304+ b . call ( '$.derived' , rune === '$derived.by' ? value : b . thunk ( value ) )
305305 )
306306 ) ;
307307 declarations . push (
Original file line number Diff line number Diff line change 1+ import { test } from '../../test' ;
2+ import { log } from './log.js' ;
3+
4+ export default test ( {
5+ before_test ( ) {
6+ log . length = 0 ;
7+ } ,
8+
9+ html : `<button>0</button>` ,
10+
11+ async test ( { assert, target, window } ) {
12+ const btn = target . querySelector ( 'button' ) ;
13+ const clickEvent = new window . Event ( 'click' , { bubbles : true } ) ;
14+ await btn ?. dispatchEvent ( clickEvent ) ;
15+
16+ assert . htmlEqual ( target . innerHTML , `<button>2</button>` ) ;
17+ assert . deepEqual ( log , [ 'create_derived' ] ) ;
18+ }
19+ } ) ;
Original file line number Diff line number Diff line change 1+ /** @type {any[] } */
2+ export const log = [ ] ;
Original file line number Diff line number Diff line change 1+ <script >
2+ import {log } from ' ./log.js'
3+
4+ let count = $state (0 );
5+ function create_derived () {
6+ log .push (' create_derived' );
7+ return () => {
8+ return {
9+ get double () {
10+ return count * 2 ;
11+ }
12+ }
13+ }
14+ }
15+ let {double} = $derived .by (create_derived ());
16+ </script >
17+
18+ <button on:click ={() => count ++ }>{double }</button >
You can’t perform that action at this time.
0 commit comments