Skip to content

Commit

Permalink
Fix prettier-ignore inside JSX (#7877)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Mar 26, 2020
1 parent 8eba848 commit b0ffc0f
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 24 deletions.
29 changes: 29 additions & 0 deletions 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))

<!-- prettier-ignore -->
```jsx
// Input
<div>
{
/* prettier-ignore */
x ? <Y/> : <Z/>
}
</div>;

// Prettier stable
<div>
{/* prettier-ignore */
x ? <Y/> : <Z/>}
</div>;

// Prettier stable (Second format)
<div>{/* prettier-ignore */ x ? <Y/> : <Z/>}</div>;

// Prettier master
<div>
{
/* prettier-ignore */
x ? <Y/> : <Z/>
}
</div>;
```
34 changes: 16 additions & 18 deletions src/language-js/printer-estree.js
Expand Up @@ -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(
Expand Down
12 changes: 8 additions & 4 deletions tests/jsx_ignore/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -91,14 +91,18 @@ f(
// this should remain as-is
<div>
{/* prettier-ignore */
foo ( )}
{
/* prettier-ignore */
foo ( )
}
</div>;
// this should remain as-is
<div>
{/* prettier-ignore */
x ? <Y/> : <Z/>}
{
/* prettier-ignore */
x ? <Y/> : <Z/>
}
</div>;
push(
Expand Down
3 changes: 2 additions & 1 deletion tests/line_suffix_boundary/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -56,7 +56,8 @@ ExampleStory.getFragment('story')}
\`;
<div>
{ExampleStory.getFragment("story") // $FlowFixMe found when converting React.createClass to ES6
{
ExampleStory.getFragment("story") // $FlowFixMe found when converting React.createClass to ES6
}
</div>;
Expand Down
1 change: 0 additions & 1 deletion tests_config/run_spec.js
Expand Up @@ -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",
Expand Down

0 comments on commit b0ffc0f

Please sign in to comment.