Skip to content
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

fix: check that snippet is not rendered as a component #9423

Merged
merged 4 commits into from
Nov 13, 2023
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/good-pianos-jump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: check that snippet is not rendered as a component
Original file line number Diff line number Diff line change
Expand Up @@ -850,7 +850,14 @@ function serialize_inline_component(node, component_name, context) {
b.thunk(b.array(props_and_spreads.map((p) => (Array.isArray(p) ? b.object(p) : p))))
);
/** @param {import('estree').Identifier} node_id */
let fn = (node_id) => b.call(component_name, node_id, props_expression);
let fn = (node_id) =>
b.call(
context.state.options.dev
? b.call('$.validate_component', b.id(component_name))
: component_name,
node_id,
props_expression
);

if (bind_this !== null) {
const prev = fn;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,12 @@ function serialize_inline_component(node, component_name, context) {
/** @type {import('estree').Statement} */
let statement = b.stmt(
(typeof component_name === 'string' ? b.call : b.maybe_call)(
component_name,
context.state.options.dev
? b.call(
'$.validate_component',
typeof component_name === 'string' ? b.id(component_name) : component_name
)
: component_name,
b.id('$$payload'),
props_expression
)
Expand Down
17 changes: 14 additions & 3 deletions packages/svelte/src/internal/client/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,13 @@ export function loop_guard(timeout) {
};
}

const symbol = Symbol.for('svelte.snippet');
const snippet_symbol = Symbol.for('svelte.snippet');

/**
* @param {any} fn
*/
export function add_snippet_symbol(fn) {
fn[symbol] = true;
fn[snippet_symbol] = true;
return fn;
}

Expand All @@ -117,11 +117,22 @@ export function add_snippet_symbol(fn) {
* @param {any} snippet_fn
*/
export function validate_snippet(snippet_fn) {
if (snippet_fn[symbol] !== true) {
if (snippet_fn[snippet_symbol] !== true) {
throw new Error(
'The argument to `{@render ...}` must be a snippet function, not a component or some other kind of function. ' +
'If you want to dynamically render one snippet or another, use `$derived` and pass its result to `{@render ...}`.'
);
}
return snippet_fn;
}

/**
* Validate that the function behind `<Component />` isn't a snippet.
* @param {any} component_fn
*/
export function validate_component(component_fn) {
if (component_fn?.[snippet_symbol] === true) {
throw new Error('A snippet must be rendered with `{@render ...}`');
}
return component_fn;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { test } from '../../test';

export default test({
compileOptions: {
dev: true
},
error: 'A snippet must be rendered with `{@render ...}`'
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{#snippet Foo()}
<p>hello</p>
{/snippet}

<Foo />