From da0fe3636a1c3703dbad288b76c1fede1af6aef5 Mon Sep 17 00:00:00 2001 From: Antony Faris Date: Tue, 12 Apr 2022 11:31:40 -0500 Subject: [PATCH] Add new line after comments (#178) * Add new line after comments * chore: add changeset --- .changeset/violet-books-try.md | 5 +++ src/nodes.ts | 1 + src/printer.ts | 9 +++- src/utils.ts | 44 ++++++++++++++++++- .../{other => basic}/html-comment/input.astro | 2 +- .../html-comment/output.astro | 1 + test/tests/basic.test.ts | 2 + test/tests/other.test.ts | 4 -- 8 files changed, 61 insertions(+), 7 deletions(-) create mode 100644 .changeset/violet-books-try.md rename test/fixtures/{other => basic}/html-comment/input.astro (77%) rename test/fixtures/{other => basic}/html-comment/output.astro (80%) diff --git a/.changeset/violet-books-try.md b/.changeset/violet-books-try.md new file mode 100644 index 0000000..9770a07 --- /dev/null +++ b/.changeset/violet-books-try.md @@ -0,0 +1,5 @@ +--- +'prettier-plugin-astro': patch +--- + +Add new line after comments diff --git a/src/nodes.ts b/src/nodes.ts index cede043..b48094d 100644 --- a/src/nodes.ts +++ b/src/nodes.ts @@ -308,4 +308,5 @@ export type { DoctypeNode, CommentNode, FragmentNode, + TagLikeNode, } from '@astrojs/compiler/types'; diff --git a/src/printer.ts b/src/printer.ts index 43a8d0a..d4cc24d 100644 --- a/src/printer.ts +++ b/src/printer.ts @@ -75,6 +75,8 @@ import { trimTextNodeLeft, trimTextNodeRight, removeDuplicates, + getNextNode, + isTagLikeNode, } from './utils'; // function printTopLevelParts(node: RootNode, path: AstPath, opts: ParserOptions, print: printFn): Doc { @@ -448,7 +450,12 @@ function print(path: AstPath, opts: ParserOptions, print: printFn): Doc { // '}', // ]; case 'comment': - return ['']; + const nextNode = getNextNode(path); + let trailingLine: _doc.builders.Concat | string = ''; + if (nextNode && isTagLikeNode(nextNode)) { + trailingLine = hardline; + } + return ['', trailingLine]; // case 'CodeSpan': // return getUnencodedText(node); // case 'CodeFence': { diff --git a/src/utils.ts b/src/utils.ts index 8624263..c89e835 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -26,6 +26,7 @@ import { InlineElementNode, // MustacheTagNode, NodeWithChildren, + TagLikeNode, // NodeWithText, // TextNode, } from './nodes'; @@ -725,7 +726,7 @@ export function getMarkdownName(script: string): Set { // 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'; } @@ -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; +} diff --git a/test/fixtures/other/html-comment/input.astro b/test/fixtures/basic/html-comment/input.astro similarity index 77% rename from test/fixtures/other/html-comment/input.astro rename to test/fixtures/basic/html-comment/input.astro index 256eec7..82eac9b 100644 --- a/test/fixtures/other/html-comment/input.astro +++ b/test/fixtures/basic/html-comment/input.astro @@ -2,4 +2,4 @@ hello world this is a comment {1 + 3 } ---> +-->
lorem
\ No newline at end of file diff --git a/test/fixtures/other/html-comment/output.astro b/test/fixtures/basic/html-comment/output.astro similarity index 80% rename from test/fixtures/other/html-comment/output.astro rename to test/fixtures/basic/html-comment/output.astro index 256eec7..51f17ca 100644 --- a/test/fixtures/other/html-comment/output.astro +++ b/test/fixtures/basic/html-comment/output.astro @@ -3,3 +3,4 @@ {1 + 3 } --> +
lorem
diff --git a/test/tests/basic.test.ts b/test/tests/basic.test.ts index b303d0b..be6d734 100644 --- a/test/tests/basic.test.ts +++ b/test/tests/basic.test.ts @@ -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'); diff --git a/test/tests/other.test.ts b/test/tests/other.test.ts index 0f841ae..16034b8 100644 --- a/test/tests/other.test.ts +++ b/test/tests/other.test.ts @@ -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,