Skip to content

fix: Allow let: directive on slot element for Svelte 4 compatibility #10391

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 5 commits into from
Feb 5, 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/friendly-candles-relate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

fix: allow `let:` directives on slot elements
3 changes: 2 additions & 1 deletion packages/svelte/src/compiler/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,8 @@ const attributes = {

/** @satisfies {Errors} */
const slots = {
'invalid-slot-element-attribute': () => `<slot> can only receive attributes, not directives`,
'invalid-slot-element-attribute': () =>
`<slot> can only receive attributes and (optionally) let directives`,
'invalid-slot-attribute': () => `slot attribute must be a static value`,
/** @param {boolean} is_default */
'invalid-slot-name': (is_default) =>
Expand Down
3 changes: 2 additions & 1 deletion packages/svelte/src/compiler/phases/2-analyze/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,7 @@ const validation = {
parent === undefined ||
(parent.type !== 'Component' &&
parent.type !== 'RegularElement' &&
parent.type !== 'SlotElement' &&
parent.type !== 'SvelteElement' &&
parent.type !== 'SvelteComponent' &&
parent.type !== 'SvelteSelf' &&
Expand Down Expand Up @@ -609,7 +610,7 @@ const validation = {
error(attribute, 'invalid-slot-name', true);
}
}
} else if (attribute.type !== 'SpreadAttribute') {
} else if (attribute.type !== 'SpreadAttribute' && attribute.type !== 'LetDirective') {
error(attribute, 'invalid-slot-element-attribute');
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2916,6 +2916,9 @@ export const template_visitors = {
/** @type {import('estree').Expression[]} */
const spreads = [];

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

let is_default = true;

/** @type {import('estree').Expression} */
Expand All @@ -2931,16 +2934,21 @@ export const template_visitors = {
if (attribute.name === 'name') {
name = value;
is_default = false;
} else {
} else if (attribute.name !== 'slot') {
if (attribute.metadata.dynamic) {
props.push(b.get(attribute.name, [b.return(value)]));
} else {
props.push(b.init(attribute.name, value));
}
}
} else if (attribute.type === 'LetDirective') {
lets.push(/** @type {import('estree').ExpressionStatement} */ (context.visit(attribute)));
}
}

// Let bindings first, they can be used on attributes
context.state.init.push(...lets);

const props_expression =
spreads.length === 0
? b.object(props)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1576,6 +1576,9 @@ const template_visitors = {
/** @type {import('estree').Expression[]} */
const spreads = [];

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

/** @type {import('estree').Expression} */
let expression = b.member_id('$$props.children');

Expand All @@ -1586,16 +1589,21 @@ const template_visitors = {
const value = serialize_attribute_value(attribute.value, context);
if (attribute.name === 'name') {
expression = b.member(b.member_id('$$props.$$slots'), value, true, true);
} else {
} else if (attribute.name !== 'slot') {
if (attribute.metadata.dynamic) {
props.push(b.get(attribute.name, [b.return(value)]));
} else {
props.push(b.init(attribute.name, value));
}
}
} else if (attribute.type === 'LetDirective') {
lets.push(/** @type {import('estree').ExpressionStatement} */ (context.visit(attribute)));
}
}

// Let bindings first, they can be used on attributes
context.state.init.push(...lets);

const props_expression =
spreads.length === 0
? b.object(props)
Expand Down
1 change: 1 addition & 0 deletions packages/svelte/src/compiler/phases/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ export function create_scopes(ast, root, allow_reactive_declarations, parent) {
},

SvelteFragment,
SlotElement: SvelteFragment,
SvelteElement: SvelteFragment,
RegularElement: SvelteFragment,

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { test } from '../../test';

export default test({
html: `<div><div slot="x">5</div></div>`
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
<slot name="x" foo={5} />
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<script>
import Outer from './outer.svelte';
</script>

<Outer>
<div slot="x" let:foo>
{foo}
</div>
</Outer>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<script>
import Inner from './inner.svelte';
</script>

<Inner>
<slot name="x" slot="x" let:foo {foo}>
{foo}
</slot>
</Inner>