fix: remove trailing semicolon from {@const} tag printer (#17962)

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 <noreply@anthropic.com>
Co-authored-by: Rich Harris <hello@rich-harris.dev>
Co-authored-by: Rich Harris <rich.harris@vercel.com>
pull/17965/head
Abishek Raj R R 5 months ago committed by GitHub
parent 8e4de9b145
commit 7123bf3a13
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
"svelte": patch
---
fix: remove trailing semicolon from {@const} tag printer

@ -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('}');
},

@ -3,6 +3,6 @@
</script>
{#each boxes as box}
{@const area = box.width * box.height;}
{@const area = box.width * box.height}
{box.width} * {box.height} = {area}
{/each}

Loading…
Cancel
Save