Skip to content

Commit cbbc92d

Browse files
committed
prettier fix: multiple comments inside nested elements
1 parent 47b7d9b commit cbbc92d

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

packages/prettier-plugin/src/index.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1875,6 +1875,18 @@ message.push(/* Some test comment */ greet(/* Some text */ \`Ripple\`));`;
18751875
expect(result).toBeWithNewline(expected);
18761876
});
18771877

1878+
it('should not move commented composite elements to the outside of parent element', async () => {
1879+
const expected = `component Child({ children, NonExistent, ...props }) {
1880+
<div {...props}>
1881+
// <children />
1882+
// <NonExistent />
1883+
</div>
1884+
}`;
1885+
1886+
const result = await format(expected, { singleQuote: true });
1887+
expect(result).toBeWithNewline(expected);
1888+
});
1889+
18781890
it('should keep comments inside function with one statement at the top', async () => {
18791891
const expected = `component App() {
18801892
const something = 5;

packages/ripple/src/compiler/phases/1-parse/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2115,9 +2115,12 @@ function get_comment_handlers(source, comments, index = 0) {
21152115
}
21162116
// Handle empty Element nodes the same way as empty BlockStatements
21172117
if (node.type === 'Element' && (!node.children || node.children.length === 0)) {
2118-
if (comments[0].start < node.end && comments[0].end < node.end) {
2118+
// Collect all comments that fall within this empty element
2119+
while (comments[0] && comments[0].start < node.end && comments[0].end < node.end) {
21192120
comment = /** @type {CommentWithLocation} */ (comments.shift());
21202121
(node.innerComments ||= []).push(comment);
2122+
}
2123+
if (node.innerComments && node.innerComments.length > 0) {
21212124
return;
21222125
}
21232126
}

0 commit comments

Comments
 (0)