Do not swallow prettier-ignore comments (#2664)

master
Christopher Chedeau 2017-08-24 18:57:01 -07:00 committed by GitHub
parent f7bd74ed98
commit 0ee74a8d25
3 changed files with 38 additions and 16 deletions

View File

@ -63,6 +63,19 @@ function getPrintFunction(options) {
}
}
function hasIgnoreComment(path) {
const node = path.getValue();
return (
node &&
((node.comments &&
node.comments.length > 0 &&
node.comments.some(
comment => comment.value.trim() === "prettier-ignore"
)) ||
hasJsxIgnoreComment(path))
);
}
function hasJsxIgnoreComment(path) {
const node = path.getValue();
const parent = path.getParentNode();
@ -99,15 +112,7 @@ function genericPrint(path, options, printPath, args) {
const node = path.getValue();
// Escape hatch
if (
node &&
((node.comments &&
node.comments.length > 0 &&
node.comments.some(
comment => comment.value.trim() === "prettier-ignore"
)) ||
hasJsxIgnoreComment(path))
) {
if (hasIgnoreComment(path)) {
return options.originalText.slice(util.locStart(node), util.locEnd(node));
}
@ -4741,13 +4746,14 @@ function printAstToDoc(ast, options, addAlignmentSize) {
// UnionTypeAnnotation has to align the child without the comments
let res;
if (
(node && node.type === "JSXElement") ||
(parent &&
(parent.type === "UnionTypeAnnotation" ||
parent.type === "TSUnionType" ||
((parent.type === "ClassDeclaration" ||
parent.type === "ClassExpression") &&
parent.superClass === node)))
((node && node.type === "JSXElement") ||
(parent &&
(parent.type === "UnionTypeAnnotation" ||
parent.type === "TSUnionType" ||
((parent.type === "ClassDeclaration" ||
parent.type === "ClassExpression") &&
parent.superClass === node)))) &&
!hasIgnoreComment(path)
) {
res = genericPrint(path, options, printGenerically, args);
} else {

View File

@ -43,6 +43,11 @@ f(
}
</div>;
push(
// prettier-ignore
<td> :)
</td>,
);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// this should remain as-is
<div>
@ -82,4 +87,10 @@ f(
x ? <Y/> : <Z/>}
</div>;
push(
// prettier-ignore
<td> :)
</td>
);
`;

View File

@ -40,3 +40,8 @@ f(
}
</div>;
push(
// prettier-ignore
<td> :)
</td>,
);