Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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/polite-mirrors-arrive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: error for rune usage in {@const} tag initialization
6 changes: 6 additions & 0 deletions documentation/docs/98-reference/.generated/compile-errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

```
Expand Down
4 changes: 4 additions & 0 deletions packages/svelte/messages/compile-errors/template.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions packages/svelte/src/compiler/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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';

/**
Expand All @@ -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);
Copy link
Member

Choose a reason for hiding this comment

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

should we start with the more generic "rune X cannot be used here" errors that already exist? that way we don't need to invent another error code

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh i knew we had an error like this...i tried to search for it and miserably failed 😄

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh actually we don't have a generic one...we only have specific ones (i think)...i'll check better after TWIS!

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah confirmed there's no generic rune X cannot be used here...should we create a generic error like this instead of the specific one?

Copy link
Member

Choose a reason for hiding this comment

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

My thinking was that you check which rune it is and then throw one of the more specific "X can only be used at ..." errors - I haven't checked the error message itself though, so if you find that's not possible then yes maybe a more generic error message.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah the other error messages are very specific... I'll create a rune_cannot_be_used error

}
}

const parent = context.path.at(-1);
const grand_parent = context.path.at(-2);

Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{#snippet test()}
{@const der = $derived(0)}
{/snippet}
Loading