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

feat: add support for {@const} inside snippet block #9904

Merged
merged 5 commits into from Dec 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/chatty-cups-drop.md
@@ -0,0 +1,5 @@
---
'svelte': patch
---

feat: add support for `{@const}` inside snippet block
2 changes: 1 addition & 1 deletion packages/svelte/src/compiler/errors.js
Expand Up @@ -333,7 +333,7 @@ const compiler_options = {
/** @satisfies {Errors} */
const const_tag = {
'invalid-const-placement': () =>
`{@const} must be the immediate child of {#if}, {:else if}, {:else}, {#each}, {:then}, {:catch}, <svelte:fragment> or <Component>`
`{@const} must be the immediate child of {#snippet}, {#if}, {:else if}, {:else}, {#each}, {:then}, {:catch}, <svelte:fragment> or <Component>`
};

/** @satisfies {Errors} */
Expand Down
Expand Up @@ -476,6 +476,7 @@ export const validation = {
grand_parent?.type !== 'SvelteComponent' &&
grand_parent?.type !== 'EachBlock' &&
grand_parent?.type !== 'AwaitBlock' &&
grand_parent?.type !== 'SnippetBlock' &&
((grand_parent?.type !== 'RegularElement' && grand_parent?.type !== 'SvelteElement') ||
!grand_parent.attributes.some((a) => a.type === 'Attribute' && a.name === 'slot')))
) {
Expand Down
@@ -0,0 +1,16 @@
import { test } from '../../test';

export default test({
html: `<button>0</button>`,
async test({ assert, target }) {
const btn = target.querySelector('button');

assert.htmlEqual(target.innerHTML, '<button>0</button>');

await btn?.click();
assert.htmlEqual(target.innerHTML, '<button>2</button>');

await btn?.click();
assert.htmlEqual(target.innerHTML, '<button>4</button>');
}
});
@@ -0,0 +1,10 @@
<script>
let count = $state(0);
</script>

{#snippet counter()}
{@const doubled = count * 2}
<button on:click={() => (count += 1)}>{doubled}</button>
{/snippet}

{@render counter()}
@@ -1,7 +1,7 @@
[
{
"code": "invalid-const-placement",
"message": "{@const} must be the immediate child of {#if}, {:else if}, {:else}, {#each}, {:then}, {:catch}, <svelte:fragment> or <Component>",
"message": "{@const} must be the immediate child of {#snippet}, {#if}, {:else if}, {:else}, {#each}, {:then}, {:catch}, <svelte:fragment> or <Component>",
"start": { "line": 5, "column": 0 },
"end": { "line": 5, "column": 18 }
}
Expand Down
@@ -1,7 +1,7 @@
[
{
"code": "invalid-const-placement",
"message": "{@const} must be the immediate child of {#if}, {:else if}, {:else}, {#each}, {:then}, {:catch}, <svelte:fragment> or <Component>",
"message": "{@const} must be the immediate child of {#snippet}, {#if}, {:else if}, {:else}, {#each}, {:then}, {:catch}, <svelte:fragment> or <Component>",
"start": { "line": 7, "column": 4 },
"end": { "line": 7, "column": 18 }
}
Expand Down