feat: Use more efficient parsing for @const

pull/10320/head
S. Elliott Johnson 2 years ago
parent a7be93c538
commit eb26b84df6

@ -266,14 +266,10 @@ function open(parser) {
} }
if (parser.match('snippet')) { if (parser.match('snippet')) {
const snippet_declaraion_end = find_matching_bracket( const snippet_declaraion_end = find_matching_bracket(parser.template, parser.index, '{');
parser.template,
parser.index,
parser.template[start]
);
if (!snippet_declaraion_end) { if (!snippet_declaraion_end) {
error(parser.current(), 'TODO', 'Expected a closing curly bracket'); error(start, 'TODO', 'Expected a closing curly bracket');
} }
// we'll eat this later, so save it // we'll eat this later, so save it
@ -551,36 +547,24 @@ function special(parser) {
return; return;
} }
if (parser.eat('const')) { if (parser.match('const')) {
// {@const a = b} // {@const a = b}
const start_index = parser.index - 5; const start_index = parser.index;
parser.require_whitespace();
let end_index = parser.index; let end_index = find_matching_bracket(parser.template, start_index, '{');
/** @type {import('estree').VariableDeclaration | undefined} */ if (!end_index) {
let declaration = undefined; error(start, 'invalid-const');
}
// 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, ' '); const dummy_spaces = parser.template.substring(0, start_index).replace(/[^\n]/g, ' ');
while (true) { let declaration = parse(
end_index = parser.template.indexOf('}', end_index + 1); dummy_spaces + parser.template.substring(start_index, end_index),
if (end_index === -1) break; parser.ts
try { ).body[0];
const node = parse(
dummy_spaces + parser.template.substring(start_index, end_index),
parser.ts
).body[0];
if (node?.type === 'VariableDeclaration') {
declaration = node;
break;
}
} catch (e) {
continue;
}
}
if ( if (
declaration === undefined || declaration === undefined ||
declaration.type !== 'VariableDeclaration' ||
declaration.declarations.length !== 1 || declaration.declarations.length !== 1 ||
declaration.declarations[0].init === undefined declaration.declarations[0].init === undefined
) { ) {

Loading…
Cancel
Save