diff --git a/src/printer.js b/src/printer.js index 323d536bd..980be4bff 100644 --- a/src/printer.js +++ b/src/printer.js @@ -65,21 +65,27 @@ function getPrintFunction(options) { function hasIgnoreComment(path) { const node = path.getValue(); + return hasNodeIgnoreComment(node) || hasJsxIgnoreComment(path); +} + +function hasNodeIgnoreComment(node) { return ( node && - ((node.comments && - node.comments.length > 0 && - node.comments.some( - comment => comment.value.trim() === "prettier-ignore" - )) || - hasJsxIgnoreComment(path)) + node.comments && + node.comments.length > 0 && + node.comments.some(comment => comment.value.trim() === "prettier-ignore") ); } function hasJsxIgnoreComment(path) { const node = path.getValue(); const parent = path.getParentNode(); - if (!parent || node.type !== "JSXElement" || parent.type !== "JSXElement") { + if ( + !parent || + !node || + node.type !== "JSXElement" || + parent.type !== "JSXElement" + ) { return false; } @@ -4385,7 +4391,7 @@ function hasTrailingComment(node) { function hasLeadingOwnLineComment(text, node) { if (node.type === "JSXElement") { - return false; + return hasNodeIgnoreComment(node); } const res = diff --git a/tests/jsx_ignore/__snapshots__/jsfmt.spec.js.snap b/tests/jsx_ignore/__snapshots__/jsfmt.spec.js.snap index 030f89813..d34f784d8 100644 --- a/tests/jsx_ignore/__snapshots__/jsfmt.spec.js.snap +++ b/tests/jsx_ignore/__snapshots__/jsfmt.spec.js.snap @@ -48,6 +48,14 @@ push( :) , ); + +function f() { + return ( + // prettier-ignore + /* $FlowFixMe(>=0.53.0) */ + + ); +} ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // this should remain as-is
@@ -93,4 +101,12 @@ push( ); +function f() { + return ( + // prettier-ignore + /* $FlowFixMe(>=0.53.0) */ + + ); +} + `; diff --git a/tests/jsx_ignore/jsx_ignore.js b/tests/jsx_ignore/jsx_ignore.js index b3033124b..c19f8db1b 100644 --- a/tests/jsx_ignore/jsx_ignore.js +++ b/tests/jsx_ignore/jsx_ignore.js @@ -45,3 +45,11 @@ push( :) , ); + +function f() { + return ( + // prettier-ignore + /* $FlowFixMe(>=0.53.0) */ + + ); +}