Skip to content

Commit 62876ae

Browse files
committed
fix(tsrx-prettier-plugin): keep a {" "} in a comment run
A comment reprints the space beside it, but a {" "} is not a space or tab touching the comment, so formatting dropped it and joined the neighbors. The run now records which side that expression is on and prints the space there. A {" "} that still has a comment prints itself, so that comment is not skipped with the space.
1 parent 7cad78b commit 62876ae

3 files changed

Lines changed: 78 additions & 16 deletions

File tree

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

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -364,9 +364,11 @@ function printJsxChildren(path, options, print, whitespace, isFacebookTranslatio
364364
return;
365365
}
366366
// TSRX: the comment beside a `{" "}` prints it, and whitespace in a
367-
// comment's run keeps what the comment printed after itself.
367+
// comment's run keeps what the comment printed after itself. An
368+
// expression that still has comments (`{" " /* note */}`) prints itself:
369+
// skipping it would drop those comments, and its space with them.
368370
if (
369-
node.tsrxCommentSpace ||
371+
(node.tsrxCommentSpace && !node.expression?.comments?.length && !node.comments?.length) ||
370372
(node.tsrxInCommentRun && node.type === 'JSXText' && !/\n/u.test(getRaw(node)))
371373
) {
372374
return;
@@ -482,21 +484,21 @@ function printJsxChildren(path, options, print, whitespace, isFacebookTranslatio
482484
? ' '
483485
: '';
484486
}
485-
if (
486-
run.space &&
487-
!run.lineBreak &&
488-
run.lineComment &&
489-
!(run.before === 'text' && run.after === 'text')
490-
) {
487+
// A `{" "}` renders a space without being a space or tab next to the
488+
// comment, so `spaceBefore` / `spaceAfter` miss it.
489+
const explicitBefore = outerBefore && run.explicitBefore;
490+
const explicitAfter = outerAfter && run.explicitAfter;
491+
if (run.space && !run.lineBreak && run.lineComment) {
491492
// A line comment puts a line break in the run, so the space it renders
492-
// is written out, before the comments.
493+
// is written out, before the comments, between two words too. A plain
494+
// space there would make `//` text (`hello// c`).
493495
if (outerBefore) before = rawWhitespace;
494496
if (outerAfter && !lineAfter) after = comment.spaceAfter ? ' ' : '';
495497
} else if (run.space && !run.lineBreak) {
496498
if (run.before === 'text' && run.after === 'text') {
497499
// Between two words, a line break renders the space too.
498-
if (outerBefore && comment.spaceBefore) before = line;
499-
if (outerAfter && comment.spaceAfter) after = line;
500+
if (outerBefore && (comment.spaceBefore || explicitBefore)) before = line;
501+
if (outerAfter && (comment.spaceAfter || explicitAfter)) after = line;
500502
} else if (run.after === 'boundary' || run.before === 'boundary') {
501503
// The start or end of the children has a line break when the element
502504
// breaks, so the space goes there, where it prints as `{" "}`.
@@ -508,9 +510,13 @@ function printJsxChildren(path, options, print, whitespace, isFacebookTranslatio
508510
}
509511
} else {
510512
// The space stays on its side; a line break there prints `{" "}`.
511-
const side = outerAfter && comment.spaceAfter ? 'after' : 'before';
513+
const side = outerAfter && (comment.spaceAfter || explicitAfter) ? 'after' : 'before';
512514
if (side === 'after') after = whitespace;
513-
else if (outerBefore && comment.spaceBefore) before = whitespace;
515+
else if (outerBefore && (comment.spaceBefore || explicitBefore)) before = whitespace;
516+
// The other side's `{" "}` is its own space.
517+
if (side === 'after' && explicitBefore && !comment.spaceBefore) before = whitespace;
518+
if (side === 'before' && explicitAfter && !comment.spaceAfter && !lineAfter)
519+
after = whitespace;
514520
}
515521
} else if (run.lineBreak) {
516522
// A space beside the start or end of the children renders nothing here.

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,11 +349,18 @@ class Adapter {
349349
describeCommentRuns(children, start, end) {
350350
const isBlank = (/** @type {Node | undefined} */ child) =>
351351
child?.type === 'JSXText' && /^[ \t\r\n]*$/u.test(child.value);
352+
// A comment inside `{" " /* note */}` belongs to that expression. It is not
353+
// a gap the comment run reprints: printing the expression keeps the space
354+
// and the comment (`isJsxWhitespaceExpression` leaves it as an expression).
355+
const hasInnerComment = (/** @type {Node} */ child) =>
356+
this.comments.some((comment) => comment.start > child.start && comment.end < child.end);
352357
// `{" "}` renders a space whatever is around it.
353358
const isSpace = (/** @type {Node | undefined} */ child) =>
354-
child?.type === 'JSXExpressionContainer' &&
359+
!!child &&
360+
child.type === 'JSXExpressionContainer' &&
355361
child.expression.type === 'Literal' &&
356-
child.expression.value === ' ';
362+
child.expression.value === ' ' &&
363+
!hasInnerComment(child);
357364
const isGap = (/** @type {Node | undefined} */ child) => isBlank(child) || isSpace(child);
358365
const kind = (/** @type {Node | undefined} */ child) =>
359366
!child ? 'boundary' : child.type === 'JSXText' ? 'text' : 'node';
@@ -375,13 +382,19 @@ class Adapter {
375382
const before = children[previous];
376383
const after = children[following];
377384
// The whitespace and `{" "}`s beside the comments are the run, which the
378-
// comments print.
385+
// comments print. A `{" "}` is not whitespace beside the comment, so
386+
// record which outer side it is on: `spaceBefore` / `spaceAfter` only
387+
// see spaces and tabs touching the comment.
379388
let explicitSpace = false;
389+
let explicitBefore = false;
390+
let explicitAfter = false;
380391
for (let index = previous + 1; index < following; index++) {
381392
children[index].tsrxInCommentRun = true;
382393
if (isSpace(children[index])) {
383394
children[index].tsrxCommentSpace = true;
384395
explicitSpace = true;
396+
if (index < first) explicitBefore = true;
397+
else if (index > last) explicitAfter = true;
385398
}
386399
}
387400
const runStart = !before
@@ -415,6 +428,9 @@ class Adapter {
415428
lineBreak: lineBreak && !explicitSpace,
416429
// Whether a line comment in the group puts a line break in the run.
417430
lineComment: group.some((comment) => comment.commentType === 'Line'),
431+
// A `{" "}` before the first comment or after the last one.
432+
explicitBefore,
433+
explicitAfter,
418434
};
419435
for (const [index, comment] of group.entries()) {
420436
// Between two comments, whatever the source had there (whitespace or

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,46 @@ const b = (
707707
);
708708
});
709709

710+
// A `{" "}` is the space in the run, but it is not a space or tab touching
711+
// the comment, and a comment inside it is that expression's.
712+
test('a {" "} beside a comment keeps its space, and a comment inside it', async () => {
713+
await expectFormat(
714+
`export function App() @{
715+
<>
716+
<div>hello{" "}/* c */world</div>
717+
<div>hello/* c */{" "}world</div>
718+
<div><b />{" "}/* c */<i /></div>
719+
<div><b />/* c */{" "}<i /></div>
720+
<div>hello{" "}// c
721+
world</div>
722+
<div>hello{" " /* note */}/* c */world</div>
723+
<div>hello/* c */{" " /* note */}world</div>
724+
<div>{/* note */ " "}/* c */x</div>
725+
</>
726+
}`,
727+
`export function App() @{
728+
<>
729+
<div>hello /* c */world</div>
730+
<div>hello/* c */ world</div>
731+
<div>
732+
<b /> /* c */<i />
733+
</div>
734+
<div>
735+
<b />/* c */ <i />
736+
</div>
737+
<div>
738+
hello{" "}// c
739+
world
740+
</div>
741+
<div>hello{" " /* note */}/* c */world</div>
742+
<div>hello/* c */{" " /* note */}world</div>
743+
<div>{/* note */ " "}/* c */x</div>
744+
</>
745+
}
746+
`,
747+
);
748+
});
749+
710750
test('in a fragment, and a JSDoc-style block comment is re-indented', async () => {
711751
await expectFormat(
712752
`const a = <>

0 commit comments

Comments
 (0)