Skip to content

Commit

Permalink
Partial fix for a component not properly retrieving let: bindings whe…
Browse files Browse the repository at this point in the history
…n it fills a named slot
  • Loading branch information
dimfeld committed Feb 5, 2024
1 parent d23805a commit f6233c8
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,7 @@ function serialize_inline_component(node, component_name, context) {
}
}

let is_named_parent_slot = false;
for (const attribute of node.attributes) {
if (attribute.type === 'LetDirective') {
default_lets.push(
Expand Down Expand Up @@ -811,6 +812,10 @@ function serialize_inline_component(node, component_name, context) {
continue;
}

if (attribute.name === 'slot') {
is_named_parent_slot = true;
}

const [, value] = serialize_attribute_value(attribute.value, context);

if (attribute.metadata.dynamic) {
Expand Down Expand Up @@ -863,6 +868,12 @@ function serialize_inline_component(node, component_name, context) {
}
}

if (is_named_parent_slot) {
// This component is filling a named slot in its parent component, so get the relevant
// attributes from the parent slot.
context.state.init.push(...default_lets);
}

if (Object.keys(events).length > 0) {
const events_expression = b.object(
Object.keys(events).map((name) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,7 @@ function serialize_inline_component(node, component_name, context) {
}
}

let is_named_parent_slot = false;
for (const attribute of node.attributes) {
if (attribute.type === 'LetDirective') {
default_lets.push(
Expand All @@ -840,6 +841,10 @@ function serialize_inline_component(node, component_name, context) {
continue;
}

if (attribute.name === 'slot') {
is_named_parent_slot = true;
}

const value = serialize_attribute_value(attribute.value, context, false, true);
push_prop(b.prop('init', b.key(attribute.name), value));
} else if (attribute.type === 'BindDirective') {
Expand All @@ -862,6 +867,12 @@ function serialize_inline_component(node, component_name, context) {
}
}

if (is_named_parent_slot) {
// This component is filling a named slot in its parent component, so get the relevant
// attributes from the parent slot.
context.state.init.push(...default_lets);
}

/** @type {import('estree').Statement[]} */
const snippet_declarations = [];

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<script>
export let things;
</script>

<div>
{#each things as thing}
<slot name="foo" {thing}/>
{/each}
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<script>
export let thing;
</script>
<span>{thing}</span>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { test } from '../../test';

export default test({
get props() {
return { things: [1, 2, 3] };
},

html: `
<div>
<span>1</span>
<span>2</span>
<span>3</span>
</div>`,

test({ assert, component, target }) {
component.things = [1, 2, 3, 4];
assert.htmlEqual(
target.innerHTML,
`
<div>
<span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
</div>
`
);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<script>
import Nested from './Nested.svelte';
import SlotInner from './SlotInner.svelte';
export let things;
</script>

<Nested {things}>
<!-- <SlotInner slot="foo" let:thing {thing} /> -->
<div slot="foo" let:thing>{thing}</div>
</Nested>

0 comments on commit f6233c8

Please sign in to comment.