Skip to content

Commit

Permalink
Add new line after comments (#178)
Browse files Browse the repository at this point in the history
* Add new line after comments

* chore: add changeset
  • Loading branch information
antonyfaris authored Apr 12, 2022
1 parent 08ec9f1 commit da0fe36
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/violet-books-try.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'prettier-plugin-astro': patch
---

Add new line after comments
1 change: 1 addition & 0 deletions src/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,4 +308,5 @@ export type {
DoctypeNode,
CommentNode,
FragmentNode,
TagLikeNode,
} from '@astrojs/compiler/types';
9 changes: 8 additions & 1 deletion src/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ import {
trimTextNodeLeft,
trimTextNodeRight,
removeDuplicates,
getNextNode,
isTagLikeNode,
} from './utils';

// function printTopLevelParts(node: RootNode, path: AstPath, opts: ParserOptions, print: printFn): Doc {
Expand Down Expand Up @@ -448,7 +450,12 @@ function print(path: AstPath, opts: ParserOptions, print: printFn): Doc {
// '}',
// ];
case 'comment':
return ['<!--', getUnencodedText(node), '-->'];
const nextNode = getNextNode(path);
let trailingLine: _doc.builders.Concat | string = '';
if (nextNode && isTagLikeNode(nextNode)) {
trailingLine = hardline;
}
return ['<!--', getUnencodedText(node), '-->', trailingLine];
// case 'CodeSpan':
// return getUnencodedText(node);
// case 'CodeFence': {
Expand Down
44 changes: 43 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
InlineElementNode,
// MustacheTagNode,
NodeWithChildren,
TagLikeNode,
// NodeWithText,
// TextNode,
} from './nodes';
Expand Down Expand Up @@ -725,7 +726,7 @@ export function getMarkdownName(script: string): Set<string> {

// TODO: USE THE COMPILER
/** True if the node is of type text */
export function isTextNode(node: Node): node is TextNode {
export function isTextNode(node: anyNode): node is TextNode {
return node.type === 'text';
}

Expand Down Expand Up @@ -769,3 +770,44 @@ export function removeDuplicates(root: RootNode) {
);
});
}

/** True if the node is TagLikeNode:
*
* ElementNode | ComponentNode | CustomElementNode | FragmentNode */
export function isTagLikeNode(node: anyNode): node is TagLikeNode {
return (
node.type === 'element' ||
node.type === 'component' ||
node.type === 'custom-element' ||
node.type === 'fragment'
);
}

/**
* Returns siblings, that is, the children of the parent.
*/
export function getSiblings(path: AstPath): anyNode[] {
const parent = path.getParentNode();
if (!parent) return [];

return getChildren(parent);
}

export function getNextNode(path: AstPath): anyNode | null {
const node = path.getNode();
if (node) {
const siblings = getSiblings(path);
if (node.position?.start === siblings[siblings.length - 1].position?.start)
return null;
for (let i = 0; i < siblings.length; i++) {
const sibling = siblings[i];
if (
sibling.position?.start === node.position?.start &&
i !== siblings.length - 1
) {
return siblings[i + 1];
}
}
}
return null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
hello world this is a comment
<html>
{1 + 3 }
-->
--><div>lorem</div>
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
<html>
{1 + 3 }
-->
<div>lorem</div>
2 changes: 2 additions & 0 deletions test/tests/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ test(
);

test('Can format a basic astro only text', files, 'basic/simple-text');

test('Can format html comments', files, 'basic/html-comment');
4 changes: 0 additions & 4 deletions test/tests/other.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ test(
'other/attribute-with-embedded-expr'
);

test('does not alter html comments', files, 'other/html-comment', {
mode: 'unaltered',
});

test(
'Can format an Astro file with a JSX expression and an HTML Comment',
files,
Expand Down

0 comments on commit da0fe36

Please sign in to comment.