-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
fix: recurse into $derived
for ownership validation
#15166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'svelte': patch | ||
--- | ||
|
||
fix: recurse into `$derived` for ownership validation |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
...velte/tests/runtime-runes/samples/non-local-mutation-with-binding-8/CounterBinding.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<script> | ||
let { linked3 = $bindable(), linked4 = $bindable() } = $props(); | ||
</script> | ||
|
||
<p>Binding</p> | ||
<button onclick={() => linked3.count++}>Increment Linked 1 ({linked3.count})</button> | ||
<button onclick={() => linked4.count++}>Increment Linked 2 ({linked4.count})</button> |
13 changes: 13 additions & 0 deletions
13
...velte/tests/runtime-runes/samples/non-local-mutation-with-binding-8/CounterContext.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<script> | ||
import { getContext } from 'svelte'; | ||
const linked1 = getContext('linked1'); | ||
const linked2 = getContext('linked2'); | ||
</script> | ||
|
||
<p>Context</p> | ||
<button onclick={() => linked1.linked.current.count++} | ||
>Increment Linked 1 ({linked1.linked.current.count})</button | ||
> | ||
<button onclick={() => linked2.linked.current.count++} | ||
>Increment Linked 2 ({linked2.linked.current.count})</button | ||
> |
34 changes: 34 additions & 0 deletions
34
packages/svelte/tests/runtime-runes/samples/non-local-mutation-with-binding-8/_config.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { flushSync } from 'svelte'; | ||
import { test } from '../../test'; | ||
|
||
// Tests that ownership is widened with $derived (on class or on its own) that contains $state | ||
export default test({ | ||
compileOptions: { | ||
dev: true | ||
}, | ||
|
||
test({ assert, target, warnings }) { | ||
const [root, counter_context1, counter_context2, counter_binding1, counter_binding2] = | ||
target.querySelectorAll('button'); | ||
|
||
counter_context1.click(); | ||
counter_context2.click(); | ||
counter_binding1.click(); | ||
counter_binding2.click(); | ||
flushSync(); | ||
|
||
assert.equal(warnings.length, 0); | ||
|
||
root.click(); | ||
flushSync(); | ||
counter_context1.click(); | ||
counter_context2.click(); | ||
counter_binding1.click(); | ||
counter_binding2.click(); | ||
flushSync(); | ||
|
||
assert.equal(warnings.length, 0); | ||
}, | ||
|
||
warnings: [] | ||
}); |
82 changes: 82 additions & 0 deletions
82
packages/svelte/tests/runtime-runes/samples/non-local-mutation-with-binding-8/main.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<script> | ||
import CounterBinding from './CounterBinding.svelte'; | ||
import CounterContext from './CounterContext.svelte'; | ||
import { setContext } from 'svelte'; | ||
|
||
let counter = $state({ count: 0 }); | ||
|
||
class Linked { | ||
#getter; | ||
linked = $derived.by(() => { | ||
const state = $state({ current: $state.snapshot(this.#getter()) }); | ||
return state; | ||
}); | ||
|
||
constructor(fn) { | ||
this.#getter = fn; | ||
} | ||
} | ||
|
||
const linked1 = $derived.by(() => { | ||
const state = $state({ current: $state.snapshot(counter) }); | ||
return state; | ||
}); | ||
const linked2 = new Linked(() => counter); | ||
|
||
setContext('linked1', { | ||
get linked() { | ||
return linked1; | ||
} | ||
}); | ||
setContext('linked2', linked2); | ||
|
||
const linked3 = $derived.by(() => { | ||
const state = $state({ current: $state.snapshot(counter) }); | ||
return state; | ||
}); | ||
const linked4 = new Linked(() => counter); | ||
</script> | ||
|
||
<p>Parent</p> | ||
<button onclick={() => counter.count++}> | ||
Increment Original ({counter.count}) | ||
</button> | ||
|
||
<CounterContext /> | ||
<CounterBinding bind:linked3={linked3.current} bind:linked4={linked4.linked.current} /> | ||
|
||
<!-- <script> | ||
import { createPortalKey } from 'svelte'; | ||
let x = createPortalKey(); | ||
let count = $state(0); | ||
let root = $state(); | ||
|
||
$effect(() => { | ||
root = document.querySelector('#root'); | ||
}) | ||
</script> | ||
|
||
<div> | ||
<svelte:portal for={x}></svelte:portal> | ||
</div> | ||
|
||
<button onclick={() => count++}>increment</button> | ||
|
||
<svelte:portal target={x}> | ||
hello {count} | ||
</svelte:portal> | ||
|
||
{#if count < 5} | ||
<svelte:portal target={x}> | ||
<span>hello2 {count}</span> | ||
</svelte:portal> | ||
{/if} | ||
|
||
<p></p> | ||
|
||
<svelte:portal target="{root}"> | ||
I'm rendered in the | ||
<button onclick={() => root = document.querySelector('p')}> | ||
{root === document.querySelector('#root') ? 'root' : 'p'} | ||
</button> | ||
</svelte:portal> --> | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.