Skip to content

Commit

Permalink
fix: tweak const tag parsing
Browse files Browse the repository at this point in the history
fixes #9711
  • Loading branch information
dummdidumm committed Nov 30, 2023
1 parent f88895e commit 37da634
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/odd-shoes-cheat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: tweak const tag parsing
15 changes: 4 additions & 11 deletions packages/svelte/src/compiler/phases/1-parse/state/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -530,22 +530,21 @@ function special(parser) {

if (parser.eat('const')) {
// {@const a = b}
const start_index = parser.index - 5;
parser.require_whitespace();

const CONST_LENGTH = 'const '.length;
parser.index = parser.index - CONST_LENGTH;

let end_index = parser.index;
/** @type {import('estree').VariableDeclaration | undefined} */
let declaration = undefined;

const dummy_spaces = parser.template.substring(0, parser.index).replace(/[^\n]/g, ' ');
// Can't use parse_expression_at here, so we try to parse until we find the correct range
const dummy_spaces = parser.template.substring(0, start_index).replace(/[^\n]/g, ' ');
while (true) {
end_index = parser.template.indexOf('}', end_index + 1);
if (end_index === -1) break;
try {
const node = parse(
dummy_spaces + parser.template.substring(parser.index, end_index),
dummy_spaces + parser.template.substring(start_index, end_index),
parser.ts
).body[0];
if (node?.type === 'VariableDeclaration') {
Expand All @@ -568,12 +567,6 @@ function special(parser) {
parser.index = end_index;
parser.eat('}', true);

const id = declaration.declarations[0].id;
if (id.type === 'Identifier') {
// Tidy up some stuff left behind by acorn-typescript
id.end = (id.start ?? 0) + id.name.length;
}

parser.append(
/** @type {import('#compiler').ConstTag} */ ({
type: 'ConstTag',
Expand Down

0 comments on commit 37da634

Please sign in to comment.