Skip to content

Commit 069ad1d

Browse files
committed
temporarily revert const parsing changes, to get prettier working again (???)
1 parent 0107dc3 commit 069ad1d

File tree

1 file changed

+34
-12
lines changed
  • packages/svelte/src/compiler/phases/1-parse/state

1 file changed

+34
-12
lines changed

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

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import read_expression from '../read/expression.js';
33
import { error } from '../../../errors.js';
44
import { create_fragment } from '../utils/create.js';
55
import { walk } from 'zimmerframe';
6+
import { parse } from '../acorn.js';
67

78
const regex_whitespace_with_closing_curly_brace = /^\s*}/;
89

@@ -549,29 +550,50 @@ function special(parser) {
549550
}
550551

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

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

557-
parser.eat('=', true);
558-
parser.allow_whitespace();
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+
}
559579

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

588+
parser.index = end_index;
563589
parser.eat('}', true);
564590

565591
parser.append(
566592
/** @type {import('#compiler').ConstTag} */ ({
567593
type: 'ConstTag',
568594
start,
569595
end: parser.index,
570-
declaration: {
571-
type: 'VariableDeclaration',
572-
kind: 'const',
573-
declarations: [{ type: 'VariableDeclarator', id, init }]
574-
}
596+
declaration
575597
})
576598
);
577599
}

0 commit comments

Comments
 (0)