diff --git a/.changeset/odd-shoes-cheat.md b/.changeset/odd-shoes-cheat.md new file mode 100644 index 0000000000..05f652e2cc --- /dev/null +++ b/.changeset/odd-shoes-cheat.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: tweak const tag parsing diff --git a/packages/svelte/src/compiler/phases/1-parse/state/tag.js b/packages/svelte/src/compiler/phases/1-parse/state/tag.js index 76e66d9b1b..36a66ac59f 100644 --- a/packages/svelte/src/compiler/phases/1-parse/state/tag.js +++ b/packages/svelte/src/compiler/phases/1-parse/state/tag.js @@ -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') { @@ -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',