Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix prettier-ignore in union types #7798

Merged
merged 5 commits into from Mar 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 32 additions & 0 deletions changelog_unreleased/typescript/pr-7798.md
@@ -0,0 +1,32 @@
#### Fix `prettier-ignore` in union types ([#7798](https://github.com/prettier/prettier/pull/7798) by [@thorn0](https://github.com/thorn0))

<!-- prettier-ignore -->
```ts
// Input
export type a =
// foo
| foo1&foo2
// prettier-ignore
| bar1&bar2
// baz
| baz1&baz2;

// Prettier stable
export type a =
// foo
| foo1&foo2
// prettier-ignore
// prettier-ignore
| (bar1 & bar2)
// baz
| (baz1 & baz2);

// Prettier master
export type a =
// foo
| (foo1 & foo2)
// prettier-ignore
| bar1&bar2
// baz
| (baz1 & baz2);
```
1 change: 1 addition & 0 deletions cspell.json
Expand Up @@ -381,6 +381,7 @@
"unaries",
"uncheck",
"uncook",
"unignore",
"uniqby",
"unist",
"unmount",
Expand Down
14 changes: 11 additions & 3 deletions src/common/util.js
Expand Up @@ -749,12 +749,19 @@ function hasIgnoreComment(path) {
function hasNodeIgnoreComment(node) {
return (
node &&
node.comments &&
node.comments.length > 0 &&
node.comments.some((comment) => comment.value.trim() === "prettier-ignore")
((node.comments &&
node.comments.length > 0 &&
node.comments.some(
(comment) => isNodeIgnoreComment(comment) && !comment.unignore
)) ||
node.prettierIgnore)
);
}

function isNodeIgnoreComment(comment) {
return comment.value.trim() === "prettier-ignore";
}

function addCommentHelper(node, comment) {
const comments = node.comments || (node.comments = []);
comments.push(comment);
Expand Down Expand Up @@ -850,6 +857,7 @@ module.exports = {
printNumber,
hasIgnoreComment,
hasNodeIgnoreComment,
isNodeIgnoreComment,
makeString,
addLeadingComment,
addDanglingComment,
Expand Down
22 changes: 20 additions & 2 deletions src/language-js/comments.js
Expand Up @@ -722,9 +722,27 @@ function handleUnionTypeComments(
(enclosingNode.type === "UnionTypeAnnotation" ||
enclosingNode.type === "TSUnionType")
) {
addTrailingComment(precedingNode, comment);
return true;
if (privateUtil.isNodeIgnoreComment(comment)) {
followingNode.prettierIgnore = true;
comment.unignore = true;
}
if (precedingNode) {
addTrailingComment(precedingNode, comment);
return true;
}
return false;
}

if (
followingNode &&
(followingNode.type === "UnionTypeAnnotation" ||
followingNode.type === "TSUnionType") &&
privateUtil.isNodeIgnoreComment(comment)
) {
followingNode.types[0].prettierIgnore = true;
comment.unignore = true;
}

return false;
}

Expand Down
4 changes: 2 additions & 2 deletions src/language-js/postprocess.js
Expand Up @@ -60,13 +60,13 @@ function postprocess(ast, options) {
}
// remove redundant TypeScript nodes
case "TSParenthesizedType": {
return node.typeAnnotation;
return { ...node.typeAnnotation, ...composeLoc(node) };
}
case "TSUnionType":
case "TSIntersectionType":
if (node.types.length === 1) {
// override loc, so that comments are attached properly
return { ...node.types[0], loc: node.loc, range: node.range };
return { ...node.types[0], ...composeLoc(node) };
}
break;
case "TSTypeParameter":
Expand Down
4 changes: 3 additions & 1 deletion src/language-js/printer-estree.js
Expand Up @@ -6089,7 +6089,9 @@ function willPrintOwnComments(path /*, options */) {
((parent.type === "ClassDeclaration" ||
parent.type === "ClassExpression") &&
parent.superClass === node)))) &&
!hasIgnoreComment(path)
(!hasIgnoreComment(path) ||
parent.type === "UnionTypeAnnotation" ||
parent.type === "TSUnionType")
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/core.js
Expand Up @@ -34,7 +34,7 @@ function ensureAllCommentsPrinted(astComments) {
}

for (let i = 0; i < astComments.length; ++i) {
if (astComments[i].value.trim() === "prettier-ignore") {
if (privateUtil.isNodeIgnoreComment(astComments[i])) {
// If there's a prettier-ignore, we're not printing that sub-tree so we
// don't know if the comments was printed or not.
return;
Expand Down
128 changes: 128 additions & 0 deletions tests/typescript_union/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -94,6 +94,134 @@ type T8 = number | ((arg: any) => void);
================================================================================
`;

exports[`prettier-ignore.ts 1`] = `
====================================options=====================================
parsers: ["typescript"]
printWidth: 80
| printWidth
=====================================input======================================
export type a =
// foo
| foo1&foo2
// bar
| bar1&bar2
// prettier-ignore
| qux1&qux2;

export type a =
// foo
| foo1&foo2
// bar
| bar1&bar2
// prettier-ignore
| qux1&qux2
// baz
| baz1&baz2;

export type a =
// prettier-ignore
| foo1&foo2
// bar
| bar1&bar2
// qux
| qux1&qux2;

=====================================output=====================================
export type a =
// foo
| (foo1 & foo2)
// bar
| (bar1 & bar2)
// prettier-ignore
| qux1&qux2;

export type a =
// foo
| (foo1 & foo2)
// bar
| (bar1 & bar2)
// prettier-ignore
| qux1&qux2
// baz
| (baz1 & baz2);

export type a =
// prettier-ignore
| foo1&foo2
// bar
| (bar1 & bar2)
// qux
| (qux1 & qux2);

================================================================================
`;

exports[`prettier-ignore-nested-unions.ts 1`] = `
====================================options=====================================
parsers: ["typescript"]
printWidth: 80
| printWidth
=====================================input======================================
export type a =
// foo
| foo1&foo2
// bar
| bar1&bar2
// prettier-ignore
| (
| aaaaaaaaaaaaa&1
// b
| bbbbbbbbbbbbb&2
)
// baz
| baz1&baz2;

export type b =
// foo
| foo1&foo2
// bar
| bar1&bar2
| (
// prettier-ignore
| aaaaaaaaaaaaa&1
// b
| bbbbbbbbbbbbb&2
)
// baz
| baz1&baz2;

=====================================output=====================================
export type a =
// foo
| (foo1 & foo2)
// bar
| (bar1 & bar2)
// prettier-ignore
| (
| aaaaaaaaaaaaa&1
// b
| bbbbbbbbbbbbb&2
)
// baz
| (baz1 & baz2);

export type b =
// foo
| (foo1 & foo2)
// bar
| (bar1 & bar2)
| (
| // prettier-ignore
aaaaaaaaaaaaa&1
// b
| (bbbbbbbbbbbbb & 2)
)
// baz
| (baz1 & baz2);

================================================================================
`;

exports[`union-parens.ts 1`] = `
====================================options=====================================
parsers: ["typescript"]
Expand Down
27 changes: 27 additions & 0 deletions tests/typescript_union/prettier-ignore-nested-unions.ts
@@ -0,0 +1,27 @@
export type a =
// foo
| foo1&foo2
// bar
| bar1&bar2
// prettier-ignore
| (
| aaaaaaaaaaaaa&1
// b
| bbbbbbbbbbbbb&2
)
// baz
| baz1&baz2;

export type b =
// foo
| foo1&foo2
// bar
| bar1&bar2
| (
// prettier-ignore
| aaaaaaaaaaaaa&1
// b
| bbbbbbbbbbbbb&2
)
// baz
| baz1&baz2;
25 changes: 25 additions & 0 deletions tests/typescript_union/prettier-ignore.ts
@@ -0,0 +1,25 @@
export type a =
// foo
| foo1&foo2
// bar
| bar1&bar2
// prettier-ignore
| qux1&qux2;

export type a =
// foo
| foo1&foo2
// bar
| bar1&bar2
// prettier-ignore
| qux1&qux2
// baz
| baz1&baz2;

export type a =
// prettier-ignore
| foo1&foo2
// bar
| bar1&bar2
// qux
| qux1&qux2;