Skip to content

fix: assign correct scope to attributes of named slot #12476

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 1 commit into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/cold-shrimps-hug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: assign correct scope to attributes of named slot
14 changes: 9 additions & 5 deletions packages/svelte/src/compiler/phases/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -386,16 +386,20 @@ export function create_scopes(ast, root, allow_reactive_declarations, parent) {
Component(node, { state, visit, path }) {
state.scope.reference(b.id(node.name), path);

for (const attribute of node.attributes) {
visit(attribute);
}

// let:x is super weird:
// - for the default slot, its scope only applies to children that are not slots themselves
// - for named slots, its scope applies to the component itself, too
const [scope, is_default_slot] = analyze_let_directives(node, state.scope);
if (!is_default_slot) {
if (is_default_slot) {
for (const attribute of node.attributes) {
visit(attribute);
}
} else {
scopes.set(node, scope);

for (const attribute of node.attributes) {
visit(attribute, { ...state, scope });
}
}

for (const child of node.fragment.nodes) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script lang="ts">
export let onclick;
</script>

<button {onclick}>
<slot />
</button>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<slot name="item" item={1} />
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { flushSync } from 'svelte';
import { test } from '../../test';

export default test({
test({ assert, logs, target }) {
const btn = target.querySelector('button');

btn?.click();
flushSync();
assert.deepEqual(logs, [1]);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<script lang="ts">
import Parent from './Parent.svelte';
import Child from './Child.svelte';
</script>

<Parent>
<Child slot="item" let:item onclick={() => console.log(item)}>asd</Child>
</Parent>
Loading