print
Rich Harris 3 months ago
parent 97c5d98f79
commit 74ec76688d

@ -76,7 +76,8 @@ export function parse_expression_at(source, comments, typescript, index) {
const { onComment, add_comments } = get_comment_handlers( const { onComment, add_comments } = get_comment_handlers(
source, source,
/** @type {CommentWithLocation[]} */ (comments) /** @type {CommentWithLocation[]} */ (comments),
index
); );
const ast = parser.parseExpressionAt(source, index, { const ast = parser.parseExpressionAt(source, index, {
@ -97,8 +98,9 @@ export function parse_expression_at(source, comments, typescript, index) {
* in JS code and so that `prettier-plugin-svelte` doesn't remove all comments when formatting. * in JS code and so that `prettier-plugin-svelte` doesn't remove all comments when formatting.
* @param {string} source * @param {string} source
* @param {CommentWithLocation[]} comments * @param {CommentWithLocation[]} comments
* @param {number} index
*/ */
function get_comment_handlers(source, comments) { function get_comment_handlers(source, comments, index = 0) {
return { return {
/** /**
* @param {boolean} block * @param {boolean} block
@ -131,7 +133,9 @@ function get_comment_handlers(source, comments) {
add_comments(ast) { add_comments(ast) {
if (comments.length === 0) return; if (comments.length === 0) return;
comments = comments.slice(); comments = comments
.filter((comment) => comment.start >= index)
.map(({ type, value, start, end }) => ({ type, value, start, end }));
walk(ast, null, { walk(ast, null, {
_(node, { next, path }) { _(node, { next, path }) {

@ -34,6 +34,8 @@ export function get_loose_identifier(parser, opening_token) {
*/ */
export default function read_expression(parser, opening_token, disallow_loose) { export default function read_expression(parser, opening_token, disallow_loose) {
try { try {
let comment_index = parser.root.comments.length;
const node = parse_expression_at( const node = parse_expression_at(
parser.template, parser.template,
parser.root.comments, parser.root.comments,
@ -43,8 +45,13 @@ export default function read_expression(parser, opening_token, disallow_loose) {
let num_parens = 0; let num_parens = 0;
if (node.leadingComments !== undefined && node.leadingComments.length > 0) { let i = parser.root.comments.length;
parser.index = node.leadingComments.at(-1).end; while (i-- > comment_index) {
const comment = parser.root.comments[i];
if (comment.end < node.start) {
parser.index = comment.end;
break;
}
} }
for (let i = parser.index; i < /** @type {number} */ (node.start); i += 1) { for (let i = parser.index; i < /** @type {number} */ (node.start); i += 1) {
@ -52,9 +59,9 @@ export default function read_expression(parser, opening_token, disallow_loose) {
} }
let index = /** @type {number} */ (node.end); let index = /** @type {number} */ (node.end);
if (node.trailingComments !== undefined && node.trailingComments.length > 0) {
index = node.trailingComments.at(-1).end; const last_comment = parser.root.comments.at(-1);
} if (last_comment && last_comment.end > index) index = last_comment.end;
while (num_parens > 0) { while (num_parens > 0) {
const char = parser.template[index]; const char = parser.template[index];

Loading…
Cancel
Save