Skip to content

Commit 6fc77c0

Browse files
committed
fix(prettier): keep comments inside a space expression beside JSX child comments
A {" "} with a comment in its braces was treated as comment-run whitespace, so the comment was never printed.
1 parent 5bd12c4 commit 6fc77c0

3 files changed

Lines changed: 49 additions & 5 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@tsrx/prettier-plugin': patch
3+
---
4+
5+
A comment inside `{" "}` stays when that expression is beside a comment
6+
between JSX children. It was treated as the comment run's space, so the
7+
comment was dropped.

‎packages/tsrx-prettier-plugin/src/parse.js‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -363,11 +363,21 @@ class Adapter {
363363
describeCommentRuns(children, start, end) {
364364
const isBlank = (/** @type {Node | undefined} */ child) =>
365365
child?.type === 'JSXText' && /^[ \t\r\n]*$/u.test(child.value);
366-
// `{" "}` renders a space whatever is around it.
367-
const isSpace = (/** @type {Node | undefined} */ child) =>
368-
child?.type === 'JSXExpressionContainer' &&
369-
child.expression.type === 'Literal' &&
370-
child.expression.value === ' ';
366+
// `{" "}` renders a space whatever is around it. A comment inside the
367+
// braces belongs to that expression: `isJsxWhitespaceExpression` keeps
368+
// the node once Prettier reattaches the comment, so it is a child, not
369+
// whitespace in the run. (The parser has already cleared `comments`.)
370+
const isSpace = (/** @type {Node | undefined} */ child) => {
371+
if (
372+
child?.type !== 'JSXExpressionContainer' ||
373+
child.expression.type !== 'Literal' ||
374+
child.expression.value !== ' '
375+
) {
376+
return false;
377+
}
378+
const { start, end } = child;
379+
return !this.comments.some((comment) => comment.start > start && comment.end < end);
380+
};
371381
const isGap = (/** @type {Node | undefined} */ child) => isBlank(child) || isSpace(child);
372382
const kind = (/** @type {Node | undefined} */ child) =>
373383
!child ? 'boundary' : child.type === 'JSXText' ? 'text' : 'node';

‎packages/tsrx-prettier-plugin/tests/tsrx.test.js‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,33 @@ const b = (
725725
);
726726
});
727727

728+
// `{" "}` with a comment inside the braces is an expression, not the space a
729+
// comment run renders. The comment has to stay with the expression.
730+
test('a comment inside {" "} stays when the expression is beside a child comment', async () => {
731+
await expectFormat(
732+
`const a = <div>/* c */ {/* inner */ " "}</div>;
733+
const b = <div>/* c */ {" " /* inner */}</div>;
734+
const c = <div>{/* inner */ " "} /* c */</div>;
735+
const d = (
736+
<div>
737+
/* c */
738+
{/* keep */ " "}
739+
</div>
740+
);
741+
`,
742+
`const a = <div> /* c */ {/* inner */ " "}</div>;
743+
const b = <div> /* c */ {" " /* inner */}</div>;
744+
const c = <div>{/* inner */ " "} /* c */ </div>;
745+
const d = (
746+
<div>
747+
/* c */
748+
{/* keep */ " "}
749+
</div>
750+
);
751+
`,
752+
);
753+
});
754+
728755
test('in a fragment, and a JSDoc-style block comment is re-indented', async () => {
729756
await expectFormat(
730757
`const a = <>

0 commit comments

Comments
 (0)