Skip to content

Commit 74ec766

Browse files
committed
fixes
1 parent 97c5d98 commit 74ec766

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

packages/svelte/src/compiler/phases/1-parse/acorn.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ export function parse_expression_at(source, comments, typescript, index) {
7676

7777
const { onComment, add_comments } = get_comment_handlers(
7878
source,
79-
/** @type {CommentWithLocation[]} */ (comments)
79+
/** @type {CommentWithLocation[]} */ (comments),
80+
index
8081
);
8182

8283
const ast = parser.parseExpressionAt(source, index, {
@@ -97,8 +98,9 @@ export function parse_expression_at(source, comments, typescript, index) {
9798
* in JS code and so that `prettier-plugin-svelte` doesn't remove all comments when formatting.
9899
* @param {string} source
99100
* @param {CommentWithLocation[]} comments
101+
* @param {number} index
100102
*/
101-
function get_comment_handlers(source, comments) {
103+
function get_comment_handlers(source, comments, index = 0) {
102104
return {
103105
/**
104106
* @param {boolean} block
@@ -131,7 +133,9 @@ function get_comment_handlers(source, comments) {
131133
add_comments(ast) {
132134
if (comments.length === 0) return;
133135

134-
comments = comments.slice();
136+
comments = comments
137+
.filter((comment) => comment.start >= index)
138+
.map(({ type, value, start, end }) => ({ type, value, start, end }));
135139

136140
walk(ast, null, {
137141
_(node, { next, path }) {

packages/svelte/src/compiler/phases/1-parse/read/expression.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ export function get_loose_identifier(parser, opening_token) {
3434
*/
3535
export default function read_expression(parser, opening_token, disallow_loose) {
3636
try {
37+
let comment_index = parser.root.comments.length;
38+
3739
const node = parse_expression_at(
3840
parser.template,
3941
parser.root.comments,
@@ -43,18 +45,23 @@ export default function read_expression(parser, opening_token, disallow_loose) {
4345

4446
let num_parens = 0;
4547

46-
if (node.leadingComments !== undefined && node.leadingComments.length > 0) {
47-
parser.index = node.leadingComments.at(-1).end;
48+
let i = parser.root.comments.length;
49+
while (i-- > comment_index) {
50+
const comment = parser.root.comments[i];
51+
if (comment.end < node.start) {
52+
parser.index = comment.end;
53+
break;
54+
}
4855
}
4956

5057
for (let i = parser.index; i < /** @type {number} */ (node.start); i += 1) {
5158
if (parser.template[i] === '(') num_parens += 1;
5259
}
5360

5461
let index = /** @type {number} */ (node.end);
55-
if (node.trailingComments !== undefined && node.trailingComments.length > 0) {
56-
index = node.trailingComments.at(-1).end;
57-
}
62+
63+
const last_comment = parser.root.comments.at(-1);
64+
if (last_comment && last_comment.end > index) index = last_comment.end;
5865

5966
while (num_parens > 0) {
6067
const char = parser.template[index];

0 commit comments

Comments
 (0)