diff --git a/changelog_unreleased/javascript/pr-7877.md b/changelog_unreleased/javascript/pr-7877.md new file mode 100644 index 000000000000..d240341124f4 --- /dev/null +++ b/changelog_unreleased/javascript/pr-7877.md @@ -0,0 +1,29 @@ +#### Fix `prettier-ignore` inside JSX ([#7877](https://github.com/prettier/prettier/pull/7877) by [@fisker](https://github.com/fisker)) + + +```jsx +// Input +
+{ + /* prettier-ignore */ + x ? : +} +
; + +// Prettier stable +
+ {/* prettier-ignore */ + x ? : } +
; + +// Prettier stable (Second format) +
{/* prettier-ignore */ x ? : }
; + +// Prettier master +
+ { + /* prettier-ignore */ + x ? : + } +
; +``` diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index 7c7440d16230..583c21e39ed0 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -2170,26 +2170,24 @@ function printPathNoParens(path, options, print, args) { case "JSXExpressionContainer": { const parent = path.getParentNode(0); - const preventInline = - parent.type === "JSXAttribute" && - n.expression.comments && - n.expression.comments.length > 0; + const hasComments = + n.expression.comments && n.expression.comments.length > 0; const shouldInline = - !preventInline && - (n.expression.type === "ArrayExpression" || - n.expression.type === "ObjectExpression" || - n.expression.type === "ArrowFunctionExpression" || - n.expression.type === "CallExpression" || - n.expression.type === "OptionalCallExpression" || - n.expression.type === "FunctionExpression" || - n.expression.type === "JSXEmptyExpression" || - n.expression.type === "TemplateLiteral" || - n.expression.type === "TaggedTemplateExpression" || - n.expression.type === "DoExpression" || - (isJSXNode(parent) && - (n.expression.type === "ConditionalExpression" || - isBinaryish(n.expression)))); + n.expression.type === "JSXEmptyExpression" || + (!hasComments && + (n.expression.type === "ArrayExpression" || + n.expression.type === "ObjectExpression" || + n.expression.type === "ArrowFunctionExpression" || + n.expression.type === "CallExpression" || + n.expression.type === "OptionalCallExpression" || + n.expression.type === "FunctionExpression" || + n.expression.type === "TemplateLiteral" || + n.expression.type === "TaggedTemplateExpression" || + n.expression.type === "DoExpression" || + (isJSXNode(parent) && + (n.expression.type === "ConditionalExpression" || + isBinaryish(n.expression))))); if (shouldInline) { return group( diff --git a/tests/jsx_ignore/__snapshots__/jsfmt.spec.js.snap b/tests/jsx_ignore/__snapshots__/jsfmt.spec.js.snap index 0c728eedec05..25ee2059d5e3 100644 --- a/tests/jsx_ignore/__snapshots__/jsfmt.spec.js.snap +++ b/tests/jsx_ignore/__snapshots__/jsfmt.spec.js.snap @@ -91,14 +91,18 @@ f( // this should remain as-is
- {/* prettier-ignore */ - foo ( )} + { + /* prettier-ignore */ + foo ( ) + }
; // this should remain as-is
- {/* prettier-ignore */ - x ? : } + { + /* prettier-ignore */ + x ? : + }
; push( diff --git a/tests/line_suffix_boundary/__snapshots__/jsfmt.spec.js.snap b/tests/line_suffix_boundary/__snapshots__/jsfmt.spec.js.snap index 475d1d817e90..61cab002633c 100644 --- a/tests/line_suffix_boundary/__snapshots__/jsfmt.spec.js.snap +++ b/tests/line_suffix_boundary/__snapshots__/jsfmt.spec.js.snap @@ -56,7 +56,8 @@ ExampleStory.getFragment('story')} \`;
- {ExampleStory.getFragment("story") // $FlowFixMe found when converting React.createClass to ES6 + { + ExampleStory.getFragment("story") // $FlowFixMe found when converting React.createClass to ES6 }
; diff --git a/tests_config/run_spec.js b/tests_config/run_spec.js index 050aafe2da4d..4c9767ee7e7b 100644 --- a/tests_config/run_spec.js +++ b/tests_config/run_spec.js @@ -35,7 +35,6 @@ const unstableTests = new Map( (options) => options.printWidth === 1, ], "js_empty/semicolon.js", - "jsx_ignore/jsx_ignore.js", "markdown_footnoteDefinition/multiline.md", "markdown_spec/example-234.md", "markdown_spec/example-235.md",