Skip to content

Commit 06e93e3

Browse files
committed
fix(prettier): keep comments before a dynamic tag name
willPrintOwnComments claims every commented tag name, but only static names were printed, so a comment between < and {Tag} was skipped.
1 parent 2fca807 commit 06e93e3

2 files changed

Lines changed: 47 additions & 5 deletions

File tree

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,11 @@ function printNode(path, options, print, args) {
423423
function printTsrx(path, options, print) {
424424
const { node, parent } = path;
425425

426+
// `willPrintOwnComments` claims every commented tag name, including a
427+
// dynamic `<{Tag}>` whose name is a `JSXExpressionContainer`. Print it
428+
// here; otherwise Prettier skips the comment.
429+
if (isCommentedTagName(path)) return printTagNameComments(path, options, print);
430+
426431
switch (node.tsrxType ?? node.type) {
427432
case 'BlockStatement':
428433
return node.tsrxCodeBlock
@@ -452,11 +457,6 @@ function printTsrx(path, options, print) {
452457
return node.commentType === 'Line' ? [printed, breakParent] : printed;
453458
}
454459

455-
case 'JSXIdentifier':
456-
case 'JSXMemberExpression':
457-
case 'JSXNamespacedName':
458-
return isCommentedTagName(path) ? printTagNameComments(path, options, print) : null;
459-
460460
case 'JSXAttribute':
461461
return node.shorthand ? ['{', print(['value', 'expression']), '}'] : null;
462462

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,48 @@ function App() @{
521521
);
522522
});
523523

524+
// A dynamic name is a `JSXExpressionContainer`. Its leading comment is still
525+
// a tag-name comment, so it keeps the same layout as a static name.
526+
test('a comment before a dynamic tag name is kept', async () => {
527+
await expectFormat(
528+
`const d = <
529+
// note
530+
{Tag} a="1">x</{Tag}>;
531+
const e = <div>
532+
<// note
533+
{Foo.Bar} />
534+
</div>;
535+
const f = <{Tag}>x</
536+
// note
537+
{Tag}>;
538+
const g = <
539+
/* note */
540+
{Tag} />;`,
541+
`const d = (
542+
<
543+
// note
544+
{Tag}
545+
a="1"
546+
>
547+
x
548+
</{Tag}>
549+
);
550+
const e = (
551+
<div>
552+
<// note
553+
{Foo.Bar}
554+
/>
555+
</div>
556+
);
557+
const f = <{Tag}>x</
558+
// note
559+
{Tag}
560+
>;
561+
const g = </* note */ {Tag} />;
562+
`,
563+
);
564+
});
565+
524566
// Prettier prints this on its second format; its first keeps the line breaks.
525567
test('a block comment on its own line prints straight after `<` or `</`', async () => {
526568
await expectFormat(

0 commit comments

Comments
 (0)