Skip to content

Commit 08b4d35

Browse files
authored
chore: simpler const parsing (#10347)
* chore: simpler const parsing * fix * fix --------- Co-authored-by: Rich Harris <[email protected]>
1 parent a84d02f commit 08b4d35

File tree

1 file changed

+14
-33
lines changed
  • packages/svelte/src/compiler/phases/1-parse/state

1 file changed

+14
-33
lines changed

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

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -550,50 +550,31 @@ function special(parser) {
550550
}
551551

552552
if (parser.eat('const')) {
553-
// {@const a = b}
554-
const start_index = parser.index - 5;
555-
parser.require_whitespace();
553+
parser.allow_whitespace();
556554

557-
let end_index = parser.index;
558-
/** @type {import('estree').VariableDeclaration | undefined} */
559-
let declaration = undefined;
555+
const id = read_context(parser);
556+
parser.allow_whitespace();
560557

561-
// Can't use parse_expression_at here, so we try to parse until we find the correct range
562-
const dummy_spaces = parser.template.substring(0, start_index).replace(/[^\n]/g, ' ');
563-
while (true) {
564-
end_index = parser.template.indexOf('}', end_index + 1);
565-
if (end_index === -1) break;
566-
try {
567-
const node = parse(
568-
dummy_spaces + parser.template.substring(start_index, end_index),
569-
parser.ts
570-
).body[0];
571-
if (node?.type === 'VariableDeclaration') {
572-
declaration = node;
573-
break;
574-
}
575-
} catch (e) {
576-
continue;
577-
}
578-
}
558+
parser.eat('=', true);
559+
parser.allow_whitespace();
579560

580-
if (
581-
declaration === undefined ||
582-
declaration.declarations.length !== 1 ||
583-
declaration.declarations[0].init === undefined
584-
) {
585-
error(start, 'invalid-const');
586-
}
561+
const init = read_expression(parser);
562+
parser.allow_whitespace();
587563

588-
parser.index = end_index;
589564
parser.eat('}', true);
590565

591566
parser.append(
592567
/** @type {import('#compiler').ConstTag} */ ({
593568
type: 'ConstTag',
594569
start,
595570
end: parser.index,
596-
declaration
571+
declaration: {
572+
type: 'VariableDeclaration',
573+
kind: 'const',
574+
declarations: [{ type: 'VariableDeclarator', id, init }],
575+
start: start + 1,
576+
end: parser.index - 1
577+
}
597578
})
598579
);
599580
}

0 commit comments

Comments
 (0)