Skip to content

Commit 1280a60

Browse files
committed
simplify, handle object and array expressions
1 parent b6ec803 commit 1280a60

File tree

3 files changed

+351
-81
lines changed

3 files changed

+351
-81
lines changed

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,12 @@ function get_comment_handlers(source) {
109109
if (parent === undefined || node.end !== parent.end) {
110110
const slice = source.slice(node.end, comments[0].start);
111111
const is_last_in_body =
112-
// BlockStatement and Program nodes have a body property
113-
parent?.body?.length &&
114-
Array.isArray(parent?.body) &&
115-
parent.body.indexOf(node) === parent.body.length - 1;
112+
((parent?.type === 'BlockStatement' || parent?.type === 'Program') &&
113+
parent.body.indexOf(node) === parent.body.length - 1) ||
114+
(parent?.type === 'ArrayExpression' &&
115+
parent.elements.indexOf(node) === parent.elements.length - 1) ||
116+
(parent?.type === 'ObjectExpression' &&
117+
parent.properties.indexOf(node) === parent.properties.length - 1);
116118

117119
if (is_last_in_body) {
118120
// Special case: There can be multiple trailing comments after the last node in a block,
@@ -121,10 +123,7 @@ function get_comment_handlers(source) {
121123

122124
while (comments.length) {
123125
const comment = comments[0];
124-
if (parent && comment.start > parent.end) break;
125-
126-
const slice = source.slice(end, comment.start);
127-
if (node.end === end ? !/^[,)\s]*$/.test(slice) : slice.trim() !== '') break;
126+
if (parent && comment.start >= parent.end) break;
128127

129128
(node.trailingComments ||= []).push(comment);
130129
comments.shift();

packages/svelte/tests/parser-legacy/samples/javascript-comments/input.svelte

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,20 @@
1414
/* trailing comment 2 */
1515
/* trailing comment 3 */
1616
}
17+
18+
const array = [
19+
// leading comment 1
20+
// leading comment 2
21+
1, // trailing comment 1
22+
/* trailing comment 2 */
23+
];
24+
25+
const object = {
26+
// leading comment 1
27+
// leading comment 2
28+
a: 1, // trailing comment 1
29+
/* trailing comment 2 */
30+
};
1731
</script>
1832

1933
<button

0 commit comments

Comments
 (0)