From f0c47c31bcb08007cce3a825b07de2873ef26970 Mon Sep 17 00:00:00 2001 From: Dominic Gannaway Date: Mon, 4 Dec 2023 19:31:09 +0000 Subject: [PATCH] fix: allow duplicate snippet declaration names (#9759) Fixes #9756 --- .changeset/khaki-moose-arrive.md | 5 +++++ .../3-transform/client/visitors/template.js | 2 +- packages/svelte/src/compiler/phases/scope.js | 2 +- .../snippet-duplicate-children/Child.svelte | 4 ++++ .../samples/snippet-duplicate-children/_config.js | 5 +++++ .../snippet-duplicate-children/main.svelte | 15 +++++++++++++++ 6 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 .changeset/khaki-moose-arrive.md create mode 100644 packages/svelte/tests/runtime-runes/samples/snippet-duplicate-children/Child.svelte create mode 100644 packages/svelte/tests/runtime-runes/samples/snippet-duplicate-children/_config.js create mode 100644 packages/svelte/tests/runtime-runes/samples/snippet-duplicate-children/main.svelte diff --git a/.changeset/khaki-moose-arrive.md b/.changeset/khaki-moose-arrive.md new file mode 100644 index 0000000000..5e78822226 --- /dev/null +++ b/.changeset/khaki-moose-arrive.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: allow duplicate snippet declaration names diff --git a/packages/svelte/src/compiler/phases/3-transform/client/visitors/template.js b/packages/svelte/src/compiler/phases/3-transform/client/visitors/template.js index ef281d3197..e619f25247 100644 --- a/packages/svelte/src/compiler/phases/3-transform/client/visitors/template.js +++ b/packages/svelte/src/compiler/phases/3-transform/client/visitors/template.js @@ -2453,7 +2453,7 @@ export const template_visitors = { body = /** @type {import('estree').BlockStatement} */ (context.visit(node.body)); } - context.state.init.push(b.function_declaration(node.expression, args, body)); + context.state.init.push(b.const(node.expression, b.arrow(args, body))); if (context.state.options.dev) { context.state.init.push(b.stmt(b.call('$.add_snippet_symbol', node.expression))); } diff --git a/packages/svelte/src/compiler/phases/scope.js b/packages/svelte/src/compiler/phases/scope.js index e8e56bfe78..60e29e61cb 100644 --- a/packages/svelte/src/compiler/phases/scope.js +++ b/packages/svelte/src/compiler/phases/scope.js @@ -375,7 +375,7 @@ export function create_scopes(ast, root, allow_reactive_declarations, parent) { scopes.set(child, state.scope); visit(child); } else if (child.type === 'SnippetBlock') { - visit(child); + visit(child, { scope }); } else { visit(child, { scope }); } diff --git a/packages/svelte/tests/runtime-runes/samples/snippet-duplicate-children/Child.svelte b/packages/svelte/tests/runtime-runes/samples/snippet-duplicate-children/Child.svelte new file mode 100644 index 0000000000..441f7f5ee4 --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/snippet-duplicate-children/Child.svelte @@ -0,0 +1,4 @@ + +
{@render children()}
diff --git a/packages/svelte/tests/runtime-runes/samples/snippet-duplicate-children/_config.js b/packages/svelte/tests/runtime-runes/samples/snippet-duplicate-children/_config.js new file mode 100644 index 0000000000..1c5430b02f --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/snippet-duplicate-children/_config.js @@ -0,0 +1,5 @@ +import { test } from '../../test'; + +export default test({ + html: `
Hello
World
` +}); diff --git a/packages/svelte/tests/runtime-runes/samples/snippet-duplicate-children/main.svelte b/packages/svelte/tests/runtime-runes/samples/snippet-duplicate-children/main.svelte new file mode 100644 index 0000000000..6d745ccc96 --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/snippet-duplicate-children/main.svelte @@ -0,0 +1,15 @@ + + + + {#snippet children()} + Hello + {/snippet} + + + + {#snippet children()} + World + {/snippet} +