Skip to content

feat: update error message for snippet binding and assignments #11168

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

Merged
merged 5 commits into from
Apr 14, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/wicked-wasps-allow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

feat: update error message for snippet binding and assignments
1 change: 1 addition & 0 deletions packages/svelte/src/compiler/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ const runes = {
'duplicate-props-rune': () => `Cannot use $props() more than once`,
'invalid-each-assignment': () =>
`Cannot reassign or bind to each block argument in runes mode. Use the array and index variables instead (e.g. 'array[i] = value' instead of 'entry = value')`,
'invalid-snippet-assignment': () => `Cannot reassign or bind to snippet parameter`,
'invalid-derived-call': () => `$derived.call(...) has been replaced with $derived.by(...)`,
'conflicting-property-name': () =>
`Cannot have a property and a component export with the same name`
Expand Down
21 changes: 16 additions & 5 deletions packages/svelte/src/compiler/phases/2-analyze/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,10 @@ const validation = {
if (context.state.analysis.runes && binding.kind === 'each') {
error(node, 'invalid-each-assignment');
}

if (binding.kind === 'snippet') {
error(node, 'invalid-snippet-assignment');
}
}

if (node.name === 'group') {
Expand Down Expand Up @@ -1011,14 +1015,21 @@ function validate_no_const_assignment(node, argument, scope, is_binding) {
function validate_assignment(node, argument, state) {
validate_no_const_assignment(node, argument, state.scope, false);

if (state.analysis.runes && argument.type === 'Identifier') {
if (argument.type === 'Identifier') {
const binding = state.scope.get(argument.name);
if (binding?.kind === 'derived') {
error(node, 'invalid-derived-assignment');

if (state.analysis.runes) {
if (binding?.kind === 'derived') {
error(node, 'invalid-derived-assignment');
}

if (binding?.kind === 'each') {
error(node, 'invalid-each-assignment');
}
}

if (binding?.kind === 'each') {
error(node, 'invalid-each-assignment');
if (binding?.kind === 'snippet') {
error(node, 'invalid-snippet-assignment');
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/svelte/src/compiler/phases/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ export function create_scopes(ast, root, allow_reactive_declarations, parent) {

for (const param of node.parameters) {
for (const id of extract_identifiers(param)) {
child_scope.declare(id, 'each', 'let');
child_scope.declare(id, 'snippet', 'let');
}
}

Expand Down
4 changes: 3 additions & 1 deletion packages/svelte/src/compiler/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,8 @@ export interface Binding {
* - `rest_prop`: A rest prop
* - `state`: A state variable
* - `derived`: A derived variable
* - `each`: An each block context variable
* - `each`: An each block parameter
* - `snippet`: A snippet parameter
* - `store_sub`: A $store value
* - `legacy_reactive`: A `$:` declaration
* - `legacy_reactive_import`: An imported binding that is mutated inside the component
Expand All @@ -267,6 +268,7 @@ export interface Binding {
| 'frozen_state'
| 'derived'
| 'each'
| 'snippet'
| 'store_sub'
| 'legacy_reactive'
| 'legacy_reactive_import';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { test } from '../../test';

export default test({
error: {
code: 'invalid-snippet-assignment',
message: 'Cannot reassign or bind to snippet parameter'
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{#snippet foo(value)}
<input bind:value={value}>
{/snippet}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { test } from '../../test';

export default test({
error: {
code: 'invalid-snippet-assignment',
message: 'Cannot reassign or bind to snippet parameter'
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{#snippet foo(value)}
<button onclick={() => value += 1}>click</button>
{/snippet}
4 changes: 3 additions & 1 deletion packages/svelte/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,8 @@ declare module 'svelte/compiler' {
* - `rest_prop`: A rest prop
* - `state`: A state variable
* - `derived`: A derived variable
* - `each`: An each block context variable
* - `each`: An each block parameter
* - `snippet`: A snippet parameter
* - `store_sub`: A $store value
* - `legacy_reactive`: A `$:` declaration
* - `legacy_reactive_import`: An imported binding that is mutated inside the component
Expand All @@ -732,6 +733,7 @@ declare module 'svelte/compiler' {
| 'frozen_state'
| 'derived'
| 'each'
| 'snippet'
| 'store_sub'
| 'legacy_reactive'
| 'legacy_reactive_import';
Expand Down