Skip to content

Commit 430f1d4

Browse files
authored
Merge pull request #44 from rixo/fix-bindings
Fix bindings #43
2 parents 8870a9b + 69fe435 commit 430f1d4

File tree

5 files changed

+362
-30
lines changed

5 files changed

+362
-30
lines changed

.changeset/khaki-pets-exercise.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte-hmr': patch
3+
---
4+
5+
Fix preserving bind: directive (fixes #43)

packages/svelte-hmr-spec/test/bindings.spec.js

Lines changed: 254 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ describe('bindings', () => {
5656
5757
`
5858

59-
// TODO should depend on preserveLocalState option
60-
testHmr.skip`
59+
testHmr`
6160
# resets bound values when owner is updated
6261
6362
--- App.svelte ---
@@ -86,4 +85,257 @@ describe('bindings', () => {
8685
<input type="number" />
8786
<div>123</div>
8887
`
88+
89+
testHmr`
90+
# instance function are preserved when binding to instance
91+
92+
--- App.svelte ---
93+
94+
<script>
95+
import {onMount} from 'svelte'
96+
import Foo from './Foo.svelte'
97+
98+
let foo
99+
let x
100+
101+
const update = () => {
102+
x = foo.get()
103+
}
104+
105+
onMount(update)
106+
</script>
107+
108+
<Foo bind:this={foo} />
109+
110+
<x-focus>{x}</x-focus>
111+
112+
<button on:click={update} />
113+
114+
--- Foo.svelte ---
115+
116+
<script>
117+
::0 export const get = () => 1
118+
::1 export const get = () => 2
119+
</script>
120+
121+
* * * * *
122+
123+
::0::
124+
1
125+
::1::
126+
${clickButton()}
127+
2
128+
`
129+
130+
testHmr`
131+
# const function bindings are preserved
132+
133+
--- App.svelte ---
134+
135+
<script>
136+
import {onMount} from 'svelte'
137+
import Foo from './Foo.svelte'
138+
139+
let get
140+
let x
141+
142+
const update = () => {
143+
x = get()
144+
}
145+
146+
onMount(update)
147+
</script>
148+
149+
<Foo bind:get />
150+
151+
<x-focus>{x}</x-focus>
152+
153+
<button on:click={update} />
154+
155+
--- Foo.svelte ---
156+
157+
<script>
158+
::0 export const get = () => 1
159+
::1 export const get = () => 2
160+
</script>
161+
162+
* * * * *
163+
164+
::0::
165+
1
166+
::1::
167+
${clickButton()}
168+
2
169+
`
170+
171+
testHmr`
172+
# const function bindings are preserved when variables change
173+
174+
--- App.svelte ---
175+
176+
<script>
177+
import {onMount} from 'svelte'
178+
import Foo from './Foo.svelte'
179+
180+
let get
181+
let x
182+
183+
const update = () => {
184+
x = get && get()
185+
}
186+
187+
onMount(update)
188+
</script>
189+
190+
<Foo bind:get />
191+
192+
<x-focus>{x}</x-focus>
193+
194+
<button on:click={update} />
195+
196+
--- Foo.svelte ---
197+
198+
<script>
199+
::0 export const get = () => 1
200+
201+
::1 let foo = 'FOO'
202+
::1 export let bar = 'BAR'
203+
::1 export const get = () => 2
204+
::1 $: console.log(foo + bar)
205+
206+
::2 let foo = 'FOO'
207+
::2 $: console.log(foo)
208+
::2 export const get = () => 3
209+
210+
::3 export const set = () => {}
211+
212+
::4 let foo = 'FOO'; let bar = 'BAR'; let baz = 'BAZ'; let bat = 'BAT';
213+
::4 $: console.log(foo + bar + baz + bat)
214+
::4 export const get = () => 4
215+
</script>
216+
217+
* * * * *
218+
219+
::0::
220+
1
221+
::1:: exported function order in variables changes
222+
1
223+
${clickButton()}
224+
2
225+
::2:: exported function order in variables changes again
226+
2
227+
${clickButton()}
228+
3
229+
::3:: exported function disappears
230+
3
231+
${clickButton()}
232+
undefined
233+
::4:: exported function comes back (at another index)
234+
undefined
235+
${clickButton()}
236+
4
237+
`
238+
239+
testHmr`
240+
# let function bindings are preserved
241+
242+
--- App.svelte ---
243+
244+
<script>
245+
import {onMount} from 'svelte'
246+
import Foo from './Foo.svelte'
247+
248+
let get
249+
let x
250+
251+
const update = () => {
252+
x = get()
253+
}
254+
255+
onMount(update)
256+
</script>
257+
258+
<Foo bind:get />
259+
260+
<x-focus>{x}</x-focus>
261+
262+
<button id="update" on:click={update} />
263+
264+
--- Foo.svelte ---
265+
266+
<script>
267+
::0 export let get = () => 1
268+
::1 export let get = () => 2
269+
270+
export const change = () => {
271+
get = () => 3
272+
}
273+
</script>
274+
275+
<button id="change-let" on:click={change} />
276+
277+
* * * * *
278+
279+
::0::
280+
1
281+
::1::
282+
${clickButton('#update')}
283+
2
284+
${clickButton('#change-let')}
285+
2
286+
${clickButton('#update')}
287+
3
288+
`
289+
290+
testHmr`
291+
# binding to a prop that does not exists yet
292+
293+
--- App.svelte ---
294+
295+
<script>
296+
import {onMount} from 'svelte'
297+
import Foo from './Foo.svelte'
298+
299+
let get
300+
let x
301+
302+
const update = () => {
303+
x = get && get()
304+
}
305+
306+
onMount(update)
307+
</script>
308+
309+
<Foo bind:get />
310+
311+
<x-focus>{x}</x-focus>
312+
313+
<button on:click={update} />
314+
315+
--- Foo.svelte ---
316+
317+
<script>
318+
::0 export let bet = () => 1
319+
::1 export let get = () => 2
320+
::2 export let bet = () => 3
321+
::3 export let get = () => 4
322+
</script>
323+
324+
* * * * *
325+
326+
::0::
327+
undefined
328+
::1::
329+
undefined
330+
${clickButton()}
331+
2
332+
::2:: doesn't reuse a wrong variable in the right place
333+
2
334+
${clickButton()}
335+
undefined
336+
::3:: remembers older future prop
337+
undefined
338+
${clickButton()}
339+
4
340+
`
89341
})

0 commit comments

Comments
 (0)