Skip to content

Commit fdf485e

Browse files
committed
feat(prettier-ignore): handle prettier-ignore comment
Gives an escape hatch. Example usage: ````xquery (: prettier-ignore :) let $matrix := [ 1, 0, 0 0, -1, 0 1, 0, -1 ] return $matrix ```
1 parent b075741 commit fdf485e

File tree

3 files changed

+97
-4
lines changed

3 files changed

+97
-4
lines changed

README.md

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
# Prettier for XQuery
2-
[![NPM version](https://badge.fury.io/js/prettier-plugin-xquery.svg)](http://badge.fury.io/js/prettier-plugin-xquery) [![Tests](https://github.com/DrRataplan/prettier-plugin-xquery/actions/workflows/test.yml/badge.svg)](https://github.com/DrRataplan/prettier-plugin-xquery/actions/workflows/test.yml) [![Coverage Status](https://coveralls.io/repos/github/DrRataplan/prettier-plugin-xquery/badge.svg)](https://coveralls.io/github/DrRataplan/prettier-plugin-xquery)
2+
[![NPM
3+
version](https://badge.fury.io/js/prettier-plugin-xquery.svg)](http://badge.fury.io/js/prettier-plugin-xquery)
4+
[![Tests](https://github.com/DrRataplan/prettier-plugin-xquery/actions/workflows/test.yml/badge.svg)](https://github.com/DrRataplan/prettier-plugin-xquery/actions/workflows/test.yml)
5+
[![Coverage
6+
Status](https://coveralls.io/repos/github/DrRataplan/prettier-plugin-xquery/badge.svg)](https://coveralls.io/github/DrRataplan/prettier-plugin-xquery)
37

4-
`prettier-plugin-xquery` is a [prettier](https://prettier.io/) plugin for XQuery. `prettier` is an opinionated code formatter that supports multiple languages and integrates with most editors. The idea is to eliminate discussions of style in code review and allow developers to get back to thinking about code design instead.
8+
`prettier-plugin-xquery` is a [prettier](https://prettier.io/) plugin for XQuery. `prettier` is an
9+
opinionated code formatter that supports multiple languages and integrates with most editors. The
10+
idea is to eliminate discussions of style in code review and allow developers to get back to
11+
thinking about code design instead.
512

613
## Demo
714
A demo page lives at [xquery.elliat.nl](https://xquery.elliat.nl).
815

916
## Getting started
1017

11-
To run `prettier` with the XQuery plugin, you're going to need [`node`](https://nodejs.org/en/download/).
18+
To run `prettier` with the XQuery plugin, you're going to need
19+
[`node`](https://nodejs.org/en/download/).
1220

1321
If you're using the `npm` CLI, then add the plugin by:
1422

@@ -68,6 +76,31 @@ Or, they can be passed to `prettier` as arguments:
6876
prettier --plugin=prettier-plugin-xquery --tab-width 4 --write '**/*.xq*'
6977
```
7078

79+
## Ignoring
80+
81+
A `prettier-ignore` comment marks code as ignored for formatting. Like this:
82+
83+
```xquery
84+
module namespace el = "http://www.elliat.nl";
85+
86+
declare function el:reverse (
87+
$a as xs:integer,
88+
$b as xs:integer,
89+
$c as xs:integer,
90+
$d as xs:integer
91+
) {
92+
($d, $c, $b, $a)
93+
};
94+
95+
(: prettier-ignore :)
96+
declare function el:swap (
97+
$a as xs:integer, $b as xs:integer,
98+
$c as xs:integer, $b as xs:integer
99+
) {
100+
($b, $a, $d, $c)
101+
};
102+
```
103+
71104
## Contributing
72105

73106
Bug reports and pull requests are welcome on GitHub at

src/main.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,21 @@ const xqueryParser: Parser<Node> = {
131131
},
132132
};
133133

134+
const hasPrettierIgnore = (path: AstPath<Node>): boolean => {
135+
const node = path.node as NonCommentNode;
136+
137+
return node.hasComments() && node.comments!.some((c) => c.value.includes("prettier-ignore"));
138+
};
139+
134140
const xqueryPrinter: Printer<Node> = {
135-
getVisitorKeys(node, nonTraversableKeys) {
141+
getVisitorKeys(_node, _nonTraversableKeys) {
136142
// Only traverse into children, not childrenByName
137143
return ["children"];
138144
},
139145
willPrintOwnComments(path: AstPath<Node>) {
146+
if (hasPrettierIgnore(path)) {
147+
return false;
148+
}
140149
if (path.node.name === "IntermediateClause") {
141150
return true;
142151
}
@@ -193,6 +202,7 @@ const xqueryPrinter: Printer<Node> = {
193202
const node = path.node as CommentNode;
194203
return printComment(node);
195204
},
205+
hasPrettierIgnore,
196206
print(path: AstPath<Node>, options, print: Print, _args) {
197207
if (path.node instanceof LeafNode) {
198208
switch (path.node.name) {

test/comments.spec.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,4 +274,54 @@ xxx:yyy()
274274
});
275275
}
276276
});
277+
278+
describe("prettier-ignore", () => {
279+
it("handles the prettier-ignore comment to ignore the root node", async () => {
280+
const script = `
281+
(: prettier-ignore :)
282+
let $tree := ( 1,
283+
2,3,
284+
4,5,6,
285+
7,8,9,0
286+
)
287+
return sum($tree)
288+
`;
289+
const result = await prettier.format(script, {
290+
parser: "xquery",
291+
plugins: [xqueryPlugin],
292+
});
293+
294+
assert.equal(result.trim(), script.trim());
295+
});
296+
297+
it("handles the prettier-ignore comment to ignore just a part", async () => {
298+
const script = `
299+
let $normal := ( 1,
300+
2,3,
301+
4,5,6,
302+
7,8,9,0)
303+
(: prettier-ignore :)
304+
let $tree := ( 1,
305+
2,3,
306+
4,5,6,
307+
7,8,9,0
308+
)
309+
return sum($tree)
310+
`;
311+
const expectedOutput = `let $normal := (1, 2, 3, 4, 5, 6, 7, 8, 9, 0)
312+
(: prettier-ignore :)
313+
let $tree := ( 1,
314+
2,3,
315+
4,5,6,
316+
7,8,9,0
317+
)
318+
return sum($tree)`;
319+
const result = await prettier.format(script, {
320+
parser: "xquery",
321+
plugins: [xqueryPlugin],
322+
});
323+
324+
assert.equal(result.trim(), expectedOutput.trim());
325+
});
326+
});
277327
});

0 commit comments

Comments
 (0)