Skip to content

Commit

Permalink
feat: add support for {@const} inside snippet block (#9904)
Browse files Browse the repository at this point in the history
* check for snippet block

* change the error msg

* edit tests

* changeset

* test
  • Loading branch information
navorite committed Dec 13, 2023
1 parent b0511a5 commit 55656f5
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 3 deletions.
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

1 comment on commit 55656f5

@vercel
Copy link

@vercel vercel bot commented on 55656f5 Dec 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

svelte-5-preview – ./sites/svelte-5-preview

svelte-5-preview-svelte.vercel.app
svelte-5-preview.vercel.app
svelte-octane.vercel.app
svelte-5-preview-git-main-svelte.vercel.app

Please sign in to comment.