From ca4806066e89fc31b0ae7ff526cd56d2f6e636c4 Mon Sep 17 00:00:00 2001 From: Erika <3019731+Princesseuh@users.noreply.github.com> Date: Thu, 27 Oct 2022 17:37:16 -0300 Subject: [PATCH] Add support for prettier-ignore comments (#297) * 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 Co-authored-by: Nate Moore --- .changeset/four-parrots-run.md | 5 +++++ package.json | 2 +- src/index.ts | 4 ++-- src/printer/index.ts | 33 +++++++++++++++++++++++++++++++-- src/printer/utils.ts | 6 +++++- test/tests/other.test.ts | 7 +++++-- yarn.lock | 8 ++++---- 7 files changed, 53 insertions(+), 12 deletions(-) create mode 100644 .changeset/four-parrots-run.md diff --git a/.changeset/four-parrots-run.md b/.changeset/four-parrots-run.md new file mode 100644 index 0000000..faed81a --- /dev/null +++ b/.changeset/four-parrots-run.md @@ -0,0 +1,5 @@ +--- +'prettier-plugin-astro': patch +--- + +Add support for prettier-ignore comments diff --git a/package.json b/package.json index 8343a32..172c6db 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/src/index.ts b/src/index.ts index 1692dfa..fddfb0c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -29,8 +29,8 @@ export const parsers: Record = { 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, }, }; diff --git a/src/printer/index.ts b/src/printer/index.ts index dd3b2eb..f08cd8a 100644 --- a/src/printer/index.ts +++ b/src/printer/index.ts @@ -1,5 +1,6 @@ import { Doc } from 'prettier'; import { selfClosingTags } from './elements'; +import { TextNode } from './nodes'; import { AstPath, canOmitSoftlineBeforeClosingTag, @@ -9,6 +10,7 @@ import { getUnencodedText, hasSetDirectives, isEmptyTextNode, + isIgnoreDirective, isInlineElement, isPreTagContent, isTagLikeNode, @@ -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 { @@ -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; } @@ -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)) { diff --git a/src/printer/utils.ts b/src/printer/utils.ts index 720329e..1c52939 100644 --- a/src/printer/utils.ts +++ b/src/printer/utils.ts @@ -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 */ @@ -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() === ''; }; diff --git a/test/tests/other.test.ts b/test/tests/other.test.ts index 2690218..d5ebd73 100644 --- a/test/tests/other.test.ts +++ b/test/tests/other.test.ts @@ -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', diff --git a/yarn.lock b/yarn.lock index 01d41d0..97719d4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"