Skip to content

Commit b67ab6e

Browse files
committed
fix(prettier): keep a line comment after < inside the opening tag
A line comment leading a tag name printed against `<` (`<// note`), which comments out the rest of that line. Break after `<`, as fragments already do.
1 parent d0d2352 commit b67ab6e

3 files changed

Lines changed: 42 additions & 3 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tsrx/prettier-plugin': patch
3+
---
4+
5+
A line comment between `<` and a tag name stays on its own line inside the opening tag. It used to print against the `<` (`<// note`), which comments out the rest of that line.

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12494,14 +12494,20 @@ function printJSXElement(node, path, options, print) {
1249412494
typeArgsDoc = path.call(print, 'openingElement', 'typeArguments');
1249512495
}
1249612496
// A comment before the tag name, as in `</* note */ div>`, prints before
12497-
// it, and one after it, as in `<div // note`, prints after it
12497+
// it, and one after it, as in `<div // note`, prints after it.
12498+
// A line comment must not sit against `<` (`<// note` comments out the rest
12499+
// of that line). Break after `<`, as `printJSXOpeningFragment` does.
1249812500
const nameNode = /** @type {AST.Node & AST.NodeWithMaybeComments} */ (openingElement.name);
12499-
const openingTagName = finishTsrxNode(
12501+
const nameLeadingComments = nameNode.leadingComments ?? [];
12502+
let openingTagName = finishTsrxNode(
1250012503
nameNode,
12501-
printLeadingComments(nameNode, nameNode.leadingComments ?? [], options),
12504+
printLeadingComments(nameNode, nameLeadingComments, options),
1250212505
tagName,
1250312506
options,
1250412507
);
12508+
if (nameLeadingComments[0]?.type === 'Line') {
12509+
openingTagName = indent([hardline, openingTagName]);
12510+
}
1250512511
const nameHasComments =
1250612512
hasComment(nameNode) ||
1250712513
Boolean(

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14784,6 +14784,34 @@ item
1478414784
expect(await format(source)).toBeWithNewline(expected);
1478514785
});
1478614786

14787+
// A line comment glued to `<` (`<// note`) comments out the rest of that
14788+
// line. Keep it on its own line inside the tag, as a fragment does.
14789+
it.each([
14790+
[
14791+
'const a = <\n // note\n div></div>;',
14792+
'const a = (\n <\n // note\n div\n ></div>\n);',
14793+
],
14794+
['const a = <\n // note\n div />;', 'const a = (\n <\n // note\n div\n />\n);'],
14795+
[
14796+
'const a = <\n // note\n div a="1"></div>;',
14797+
'const a = (\n <\n // note\n div\n a="1"\n ></div>\n);',
14798+
],
14799+
[
14800+
'function App() {\n return (\n <\n // note\n div\n >\n test\n </div>\n );\n}',
14801+
'function App() {\n return (\n <\n // note\n div\n >\n test\n </div>\n );\n}',
14802+
],
14803+
[
14804+
'export function App() @{\n <\n // note\n div\n >\n x\n </div>\n}',
14805+
'export function App() @{\n <\n // note\n div\n >\n x\n </div>\n}',
14806+
],
14807+
[
14808+
'const a = <\n // note\n div<T> />;',
14809+
'const a = (\n <\n // note\n div<T>\n />\n);',
14810+
],
14811+
])('keeps a line comment after the `<` of %j inside the tag', async (source, expected) => {
14812+
expect(await format(source)).toBeWithNewline(expected);
14813+
});
14814+
1478714815
it('joins an operator on the next line to the element it continues, like Prettier', async () => {
1478814816
const source = '<div />\n+ 1;\n\nfunction f() {\n <div />\n > 5;\n}';
1478914817
expect(await format(source)).toBeWithNewline(

0 commit comments

Comments
 (0)