fix: error for rune usage in {@const} tag initialization

const-rune-usage-error
paoloricciuti 8 months ago
parent a9d1f46dbb
commit 79be365d29

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: error for rune usage in {@const} tag initialization

@ -190,6 +190,12 @@ Cyclical dependency detected: %cycle%
`{@const}` must be the immediate child of `{#snippet}`, `{#if}`, `{:else if}`, `{:else}`, `{#each}`, `{:then}`, `{:catch}`, `<svelte:fragment>`, `<svelte:boundary` or `<Component>`
```
### const_tag_invalid_rune_usage
```
Can't use %name% as initialization of a `{@const}` tag
```
### constant_assignment
```

@ -120,6 +120,10 @@
> `{@const}` must be the immediate child of `{#snippet}`, `{#if}`, `{:else if}`, `{:else}`, `{#each}`, `{:then}`, `{:catch}`, `<svelte:fragment>`, `<svelte:boundary` or `<Component>`
## const_tag_invalid_rune_usage
> Can't use %name% as initialization of a `{@const}` tag
## debug_tag_invalid_arguments
> {@debug ...} arguments must be identifiers, not arbitrary expressions

@ -896,6 +896,16 @@ export function const_tag_invalid_placement(node) {
e(node, 'const_tag_invalid_placement', `\`{@const}\` must be the immediate child of \`{#snippet}\`, \`{#if}\`, \`{:else if}\`, \`{:else}\`, \`{#each}\`, \`{:then}\`, \`{:catch}\`, \`<svelte:fragment>\`, \`<svelte:boundary\` or \`<Component>\`\nhttps://svelte.dev/e/const_tag_invalid_placement`);
}
/**
* Can't use %name% as initialization of a `{@const}` tag
* @param {null | number | NodeLike} node
* @param {string} name
* @returns {never}
*/
export function const_tag_invalid_rune_usage(node, name) {
e(node, 'const_tag_invalid_rune_usage', `Can't use ${name} as initialization of a \`{@const}\` tag\nhttps://svelte.dev/e/const_tag_invalid_rune_usage`);
}
/**
* {@debug ...} arguments must be identifiers, not arbitrary expressions
* @param {null | number | NodeLike} node

@ -1,6 +1,7 @@
/** @import { AST } from '#compiler' */
/** @import { Context } from '../types' */
import * as e from '../../../errors.js';
import { get_rune } from '../../scope.js';
import { validate_opening_tag } from './shared/utils.js';
/**
@ -12,6 +13,13 @@ export function ConstTag(node, context) {
validate_opening_tag(node, context.state, '@');
}
for (let declaration of node.declaration.declarations) {
const rune = get_rune(declaration.init, context.state.scope);
if (rune) {
e.const_tag_invalid_rune_usage(declaration.init, rune);
}
}
const parent = context.path.at(-1);
const grand_parent = context.path.at(-2);

@ -0,0 +1,14 @@
[
{
"code": "const_tag_invalid_rune_usage",
"message": "Can't use $derived as initialization of a `{@const}` tag",
"start": {
"line": 2,
"column": 15
},
"end": {
"line": 2,
"column": 26
}
}
]

@ -0,0 +1,3 @@
{#snippet test()}
{@const der = $derived(0)}
{/snippet}
Loading…
Cancel
Save