From 3c3a542c058b2b1e68e57e77f5255bca6842c793 Mon Sep 17 00:00:00 2001 From: Simon H <5968653+dummdidumm@users.noreply.github.com> Date: Wed, 17 Jul 2024 14:06:24 +0200 Subject: [PATCH] fix: error on snippet rest parameters (#12472) closes #12464 --- packages/svelte/messages/compile-errors/template.md | 2 +- packages/svelte/src/compiler/errors.js | 4 ++-- .../svelte/src/compiler/phases/2-analyze/validation.js | 6 ++++++ .../compiler-errors/samples/snippet-rest-args/_config.js | 9 +++++++++ .../samples/snippet-rest-args/main.svelte | 3 +++ 5 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 packages/svelte/tests/compiler-errors/samples/snippet-rest-args/_config.js create mode 100644 packages/svelte/tests/compiler-errors/samples/snippet-rest-args/main.svelte diff --git a/packages/svelte/messages/compile-errors/template.md b/packages/svelte/messages/compile-errors/template.md index 517e5e730f..9b43e99820 100644 --- a/packages/svelte/messages/compile-errors/template.md +++ b/packages/svelte/messages/compile-errors/template.md @@ -250,7 +250,7 @@ ## snippet_invalid_rest_parameter -> snippets do not support rest parameters; use an array instead +> Snippets do not support rest parameters; use an array instead ## snippet_shadowing_prop diff --git a/packages/svelte/src/compiler/errors.js b/packages/svelte/src/compiler/errors.js index b092e262db..b4dcad4e4c 100644 --- a/packages/svelte/src/compiler/errors.js +++ b/packages/svelte/src/compiler/errors.js @@ -1108,12 +1108,12 @@ export function snippet_conflict(node) { } /** - * snippets do not support rest parameters; use an array instead + * Snippets do not support rest parameters; use an array instead * @param {null | number | NodeLike} node * @returns {never} */ export function snippet_invalid_rest_parameter(node) { - e(node, "snippet_invalid_rest_parameter", "snippets do not support rest parameters; use an array instead"); + e(node, "snippet_invalid_rest_parameter", "Snippets do not support rest parameters; use an array instead"); } /** diff --git a/packages/svelte/src/compiler/phases/2-analyze/validation.js b/packages/svelte/src/compiler/phases/2-analyze/validation.js index fcbb227a71..a5b22c666f 100644 --- a/packages/svelte/src/compiler/phases/2-analyze/validation.js +++ b/packages/svelte/src/compiler/phases/2-analyze/validation.js @@ -674,6 +674,12 @@ const validation = { SnippetBlock(node, context) { validate_block_not_empty(node.body, context); + for (const arg of node.parameters) { + if (arg.type === 'RestElement') { + e.snippet_invalid_rest_parameter(arg); + } + } + context.next({ ...context.state, parent_element: null }); const { path } = context; diff --git a/packages/svelte/tests/compiler-errors/samples/snippet-rest-args/_config.js b/packages/svelte/tests/compiler-errors/samples/snippet-rest-args/_config.js new file mode 100644 index 0000000000..f305520831 --- /dev/null +++ b/packages/svelte/tests/compiler-errors/samples/snippet-rest-args/_config.js @@ -0,0 +1,9 @@ +import { test } from '../../test'; + +export default test({ + error: { + code: 'snippet_invalid_rest_parameter', + message: 'Snippets do not support rest parameters; use an array instead', + position: [19, 26] + } +}); diff --git a/packages/svelte/tests/compiler-errors/samples/snippet-rest-args/main.svelte b/packages/svelte/tests/compiler-errors/samples/snippet-rest-args/main.svelte new file mode 100644 index 0000000000..f345bf1956 --- /dev/null +++ b/packages/svelte/tests/compiler-errors/samples/snippet-rest-args/main.svelte @@ -0,0 +1,3 @@ +{#snippet children(...args)} + {args} +{/snippet}