From 25ce91cac92e26f2ad8a6e9b2d3a08fe875dccbd Mon Sep 17 00:00:00 2001 From: fisker Date: Thu, 26 Mar 2020 11:03:03 +0800 Subject: [PATCH 1/3] Fix JSX ignore --- src/language-js/printer-estree.js | 34 +++++++++---------- .../__snapshots__/jsfmt.spec.js.snap | 12 ++++--- .../__snapshots__/jsfmt.spec.js.snap | 3 +- tests_config/run_spec.js | 1 - 4 files changed, 26 insertions(+), 24 deletions(-) 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", From 995e83063155570cf1d0cef89eb715c33b2a875e Mon Sep 17 00:00:00 2001 From: fisker Date: Thu, 26 Mar 2020 12:39:21 +0800 Subject: [PATCH 2/3] Add changelog --- changelog_unreleased/javascript/pr-7877.md | 31 ++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 changelog_unreleased/javascript/pr-7877.md diff --git a/changelog_unreleased/javascript/pr-7877.md b/changelog_unreleased/javascript/pr-7877.md new file mode 100644 index 000000000000..00fff9d10dc2 --- /dev/null +++ b/changelog_unreleased/javascript/pr-7877.md @@ -0,0 +1,31 @@ +#### Fix prettier-ignore inside JSX ([#7877](https://github.com/prettier/prettier/pull/7877) by [@fisker](https://github.com/fisker)) + +Optional description if it makes sense. + + +```jsx +// Input +
+{ + /* prettier-ignore */ + x ? : +} +
; + +// Prettier stable +
+ {/* prettier-ignore */ + x ? : } +
; + +// Prettier stable (Second format) +
{/* prettier-ignore */ x ? : }
; + +// Prettier master +
+ { + /* prettier-ignore */ + x ? : + } +
; +``` From 651decf926674581def29805c164e30610f867f6 Mon Sep 17 00:00:00 2001 From: Georgii Dolzhykov Date: Thu, 26 Mar 2020 20:09:19 +0200 Subject: [PATCH 3/3] Update pr-7877.md --- changelog_unreleased/javascript/pr-7877.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/changelog_unreleased/javascript/pr-7877.md b/changelog_unreleased/javascript/pr-7877.md index 00fff9d10dc2..d240341124f4 100644 --- a/changelog_unreleased/javascript/pr-7877.md +++ b/changelog_unreleased/javascript/pr-7877.md @@ -1,6 +1,4 @@ -#### Fix prettier-ignore inside JSX ([#7877](https://github.com/prettier/prettier/pull/7877) by [@fisker](https://github.com/fisker)) - -Optional description if it makes sense. +#### Fix `prettier-ignore` inside JSX ([#7877](https://github.com/prettier/prettier/pull/7877) by [@fisker](https://github.com/fisker)) ```jsx