From 7123bf3a131a53b1ba9414f0d44150a94d0132e3 Mon Sep 17 00:00:00 2001 From: Abishek Raj R R Date: Sat, 21 Mar 2026 18:04:31 +0530 Subject: [PATCH] fix: remove trailing semicolon from {@const} tag printer (#17962) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #17720 ## Problem The `{@const}` tag was being printed with a trailing semicolon, producing invalid Svelte syntax like: {@const a = 1;} This happened because the `ConstTag` visitor in the printer was delegating to esrap's `VariableDeclaration` handler, which always appends a semicolon (correct for JS, but wrong for Svelte template syntax). ## Solution Instead of delegating to `VariableDeclaration`, the `ConstTag` visitor now manually prints the tag by: - Writing `{@const ` directly - Iterating through declarators and visiting each one - Separating multiple declarations with commas - Closing with `}` — no trailing semicolon ## Before {@const a = 1;} {@const a = 1, b = 2;} ## After {@const a = 1} {@const a = 1, b = 2} ## Changes - `packages/svelte/src/compiler/print/index.js` — fixed `ConstTag` visitor - `packages/svelte/tests/print/samples/const-tag/output.svelte` — updated expected test output --------- Co-authored-by: Claude Opus 4.6 Co-authored-by: Rich Harris Co-authored-by: Rich Harris --- .changeset/two-onions-end.md | 5 +++++ packages/svelte/src/compiler/print/index.js | 9 +++++++-- .../svelte/tests/print/samples/const-tag/output.svelte | 2 +- 3 files changed, 13 insertions(+), 3 deletions(-) create mode 100644 .changeset/two-onions-end.md diff --git a/.changeset/two-onions-end.md b/.changeset/two-onions-end.md new file mode 100644 index 0000000000..9e6a56bbf9 --- /dev/null +++ b/.changeset/two-onions-end.md @@ -0,0 +1,5 @@ +--- +"svelte": patch +--- + +fix: remove trailing semicolon from {@const} tag printer diff --git a/packages/svelte/src/compiler/print/index.js b/packages/svelte/src/compiler/print/index.js index 160aeb4345..26dc1b88e8 100644 --- a/packages/svelte/src/compiler/print/index.js +++ b/packages/svelte/src/compiler/print/index.js @@ -592,8 +592,13 @@ const svelte_visitors = (comments) => ({ }, ConstTag(node, context) { - context.write('{@'); - context.visit(node.declaration); + context.write('{@const '); + const declarators = node.declaration.declarations; + for (let i = 0; i < declarators.length; i++) { + if (i > 0) context.write(', '); + context.visit(declarators[i]); + } + context.write('}'); }, diff --git a/packages/svelte/tests/print/samples/const-tag/output.svelte b/packages/svelte/tests/print/samples/const-tag/output.svelte index d9d8addcbb..dded998a84 100644 --- a/packages/svelte/tests/print/samples/const-tag/output.svelte +++ b/packages/svelte/tests/print/samples/const-tag/output.svelte @@ -3,6 +3,6 @@ {#each boxes as box} - {@const area = box.width * box.height;} + {@const area = box.width * box.height} {box.width} * {box.height} = {area} {/each}