Skip to content

Commit 37da634

Browse files
committed
fix: tweak const tag parsing
fixes #9711
1 parent f88895e commit 37da634

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

.changeset/odd-shoes-cheat.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: tweak const tag parsing

packages/svelte/src/compiler/phases/1-parse/state/tag.js

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -530,22 +530,21 @@ function special(parser) {
530530

531531
if (parser.eat('const')) {
532532
// {@const a = b}
533+
const start_index = parser.index - 5;
533534
parser.require_whitespace();
534535

535-
const CONST_LENGTH = 'const '.length;
536-
parser.index = parser.index - CONST_LENGTH;
537-
538536
let end_index = parser.index;
539537
/** @type {import('estree').VariableDeclaration | undefined} */
540538
let declaration = undefined;
541539

542-
const dummy_spaces = parser.template.substring(0, parser.index).replace(/[^\n]/g, ' ');
540+
// Can't use parse_expression_at here, so we try to parse until we find the correct range
541+
const dummy_spaces = parser.template.substring(0, start_index).replace(/[^\n]/g, ' ');
543542
while (true) {
544543
end_index = parser.template.indexOf('}', end_index + 1);
545544
if (end_index === -1) break;
546545
try {
547546
const node = parse(
548-
dummy_spaces + parser.template.substring(parser.index, end_index),
547+
dummy_spaces + parser.template.substring(start_index, end_index),
549548
parser.ts
550549
).body[0];
551550
if (node?.type === 'VariableDeclaration') {
@@ -568,12 +567,6 @@ function special(parser) {
568567
parser.index = end_index;
569568
parser.eat('}', true);
570569

571-
const id = declaration.declarations[0].id;
572-
if (id.type === 'Identifier') {
573-
// Tidy up some stuff left behind by acorn-typescript
574-
id.end = (id.start ?? 0) + id.name.length;
575-
}
576-
577570
parser.append(
578571
/** @type {import('#compiler').ConstTag} */ ({
579572
type: 'ConstTag',

0 commit comments

Comments
 (0)