You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Implemented by reusing the `async_body` function inside `Fragment.js`. Also removes the ability to reference a `{@const ...}` of an implicit child inside a boundary pending/failed snippet:
- existing duplication of consts can have unintended side effects, e.g. async consts would unexpectedly called multiple times
- what if a const is the reason for the failure of a boundary, but is then referenced in the failed snippet?
- what if an async const is referenced in a pending snippet? deadlock
- inconsistent with how it behaves for components where this already does not work
Implemented via the experimental flag so the behavior change only applies there as this is a breaking change strictly speaking. Also added a compiler error for this.
closes#16462
`{@const}` must be the immediate child of `{#snippet}`, `{#if}`, `{:else if}`, `{:else}`, `{#each}`, `{:then}`, `{:catch}`, `<svelte:fragment>`, `<svelte:boundary` or `<Component>`
197
197
```
198
198
199
+
### const_tag_invalid_reference
200
+
201
+
```
202
+
The `{@const %name% = ...}` declaration is not available in this snippet
203
+
```
204
+
205
+
The following is an error:
206
+
207
+
```svelte
208
+
<svelte:boundary>
209
+
{@const foo = 'bar'}
210
+
211
+
{#snippet failed()}
212
+
{foo}
213
+
{/snippet}
214
+
</svelte:boundary>
215
+
```
216
+
217
+
Here, `foo` is not available inside `failed`. The top level code inside `<svelte:boundary>` becomes part of the implicit `children` snippet, in other words the above code is equivalent to this:
Copy file name to clipboardExpand all lines: packages/svelte/messages/compile-errors/template.md
+43Lines changed: 43 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -124,6 +124,49 @@
124
124
125
125
> `{@const}` must be the immediate child of `{#snippet}`, `{#if}`, `{:else if}`, `{:else}`, `{#each}`, `{:then}`, `{:catch}`, `<svelte:fragment>`, `<svelte:boundary` or `<Component>`
126
126
127
+
## const_tag_invalid_reference
128
+
129
+
> The `{@const %name% = ...}` declaration is not available in this snippet
130
+
131
+
The following is an error:
132
+
133
+
```svelte
134
+
<svelte:boundary>
135
+
{@const foo = 'bar'}
136
+
137
+
{#snippet failed()}
138
+
{foo}
139
+
{/snippet}
140
+
</svelte:boundary>
141
+
```
142
+
143
+
Here, `foo` is not available inside `failed`. The top level code inside `<svelte:boundary>` becomes part of the implicit `children` snippet, in other words the above code is equivalent to this:
144
+
145
+
```svelte
146
+
<svelte:boundary>
147
+
{#snippet children()}
148
+
{@const foo = 'bar'}
149
+
{/snippet}
150
+
151
+
{#snippet failed()}
152
+
{foo}
153
+
{/snippet}
154
+
</svelte:boundary>
155
+
```
156
+
157
+
The same applies to components:
158
+
159
+
```svelte
160
+
<Component>
161
+
{@const foo = 'bar'}
162
+
163
+
{#snippet someProp()}
164
+
<!-- error -->
165
+
{foo}
166
+
{/snippet}
167
+
</Component>
168
+
```
169
+
127
170
## debug_tag_invalid_arguments
128
171
129
172
> {@debug ...} arguments must be identifiers, not arbitrary expressions
Copy file name to clipboardExpand all lines: packages/svelte/src/compiler/errors.js
+10Lines changed: 10 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -985,6 +985,16 @@ export function const_tag_invalid_placement(node) {
985
985
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`);
986
986
}
987
987
988
+
/**
989
+
* The `{@const %name% = ...}` declaration is not available in this snippet
e(node,'const_tag_invalid_reference',`The \`{@const ${name} = ...}\` declaration is not available in this snippet \nhttps://svelte.dev/e/const_tag_invalid_reference`);
996
+
}
997
+
988
998
/**
989
999
* {@debug ...} arguments must be identifiers, not arbitrary expressions
0 commit comments