Skip to content

Commit

Permalink
Add support for prettier-ignore comments (#297)
Browse files Browse the repository at this point in the history
* Add support for prettier-ignore comments

* chore: update compiler

* chore: whoops, use yarn

* chore: update compiler

* feat: fix prettier-ignore in markup

* chore: remove position hacks

* Remove cast to any

* Add changeset

Co-authored-by: Nate Moore <[email protected]>
Co-authored-by: Nate Moore <[email protected]>
  • Loading branch information
3 people authored Oct 27, 2022
1 parent 485fb91 commit ca48060
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/four-parrots-run.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'prettier-plugin-astro': patch
---

Add support for prettier-ignore comments
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"release": "yarn build && changeset publish"
},
"dependencies": {
"@astrojs/compiler": "0.27.2",
"@astrojs/compiler": "^0.29.3",
"prettier": "^2.7.1",
"sass-formatter": "^0.7.5",
"synckit": "^0.8.4"
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ export const parsers: Record<string, Parser> = {
astro: {
parse: (source) => parse(source),
astFormat: 'astro',
locStart: (node) => node.start,
locEnd: (node) => node.end,
locStart: (node) => node.position.start.offset,
locEnd: (node) => node.position.end.offset,
},
};

Expand Down
33 changes: 31 additions & 2 deletions src/printer/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Doc } from 'prettier';
import { selfClosingTags } from './elements';
import { TextNode } from './nodes';
import {
AstPath,
canOmitSoftlineBeforeClosingTag,
Expand All @@ -9,6 +10,7 @@ import {
getUnencodedText,
hasSetDirectives,
isEmptyTextNode,
isIgnoreDirective,
isInlineElement,
isPreTagContent,
isTagLikeNode,
Expand All @@ -26,14 +28,26 @@ import {
trimTextNodeLeft,
trimTextNodeRight,
} from './utils';
import { TextNode } from './nodes';

import _doc from 'prettier/doc';
const {
builders: { breakParent, dedent, fill, group, indent, join, line, softline, hardline },
builders: {
breakParent,
dedent,
fill,
group,
indent,
join,
line,
softline,
hardline,
literalline,
},
utils: { stripTrailingHardline },
} = _doc;

let ignoreNext = false;

// https://prettier.io/docs/en/plugins.html#print
// eslint-disable-next-line @typescript-eslint/no-shadow
export function print(path: AstPath, opts: ParserOptions, print: printFn): Doc {
Expand All @@ -44,6 +58,17 @@ export function print(path: AstPath, opts: ParserOptions, print: printFn): Doc {
return '';
}

if (ignoreNext && !isEmptyTextNode(node)) {
ignoreNext = false;
return [
opts.originalText
.slice(opts.locStart(node), opts.locEnd(node))
.split('\n')
.map((lineContent, i) => (i == 0 ? [lineContent] : [literalline, lineContent]))
.flat(),
];
}

if (typeof node === 'string') {
return node;
}
Expand Down Expand Up @@ -298,6 +323,10 @@ export function print(path: AstPath, opts: ParserOptions, print: printFn): Doc {
}

case 'comment':
if (isIgnoreDirective(node)) {
ignoreNext = true;
}

const nextNode = getNextNode(path);
let trailingLine: _doc.builders.Concat | string = '';
if (nextNode && isTagLikeNode(nextNode)) {
Expand Down
6 changes: 5 additions & 1 deletion src/printer/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ export function isBlockElement(node: anyNode, opts: ParserOptions): boolean {
);
}

export function isIgnoreDirective(node: Node): boolean {
return node.type === 'comment' && node.value.trim() === 'prettier-ignore';
}

/**
* Returns the content of the node
*/
Expand Down Expand Up @@ -78,7 +82,7 @@ export function isNodeWithChildren(node: anyNode): node is anyNode & ParentLikeN
return node && 'children' in node && Array.isArray(node.children);
}

export const isEmptyTextNode = (node: Node): boolean => {
export const isEmptyTextNode = (node: anyNode): boolean => {
return !!node && node.type === 'text' && getUnencodedText(node).trim() === '';
};

Expand Down
7 changes: 5 additions & 2 deletions test/tests/other.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,11 @@ test('Autocloses open tags.', files, 'other/autocloses-open-tags');

test('Can format an Astro file with a script tag inside it', files, 'other/with-script');

// // Supports various prettier ignore comments
// test('Can format an Astro file with a HTML style prettier ignore comment: https://prettier.io/docs/en/ignore.html', Prettier, 'prettier-ignore-html');
test(
'Can format an Astro file with a HTML style prettier ignore comment: https://prettier.io/docs/en/ignore.html',
files,
'other/prettier-ignore-html'
);

test(
'Can format an Astro file with a JS style prettier ignore comment: https://prettier.io/docs/en/ignore.html',
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
# yarn lockfile v1


"@astrojs/compiler@0.27.2":
version "0.27.2"
resolved "https://registry.yarnpkg.com/@astrojs/compiler/-/compiler-0.27.2.tgz#3190cbf84e19abaf227d3f7cde03e72a0af6a966"
integrity sha512-VG4X87cUkcmT40HqEwShQzUgl0VSnVTszobbmnhAOkHzdoWMxhwAm61A2o5fEsv6eEK8M0lW/fGwkSofYM5GcQ==
"@astrojs/compiler@^0.29.3":
version "0.29.3"
resolved "https://registry.yarnpkg.com/@astrojs/compiler/-/compiler-0.29.3.tgz#55c6b6645a4b2e259b10d7e21e890ed6eb2b02bd"
integrity sha512-hpdX96EVuoEqK7M9tM+SldbCPTrGJ2Dd49NQ8IilxsSGrs0IGsN18zdJra3U3Egau8bO1ncSUOwl3QZWUq/bMg==

"@babel/code-frame@^7.0.0":
version "7.16.0"
Expand Down

0 comments on commit ca48060

Please sign in to comment.