Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix prettier-ignore inside JSX #7877

Merged
merged 3 commits into from
Mar 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 29 additions & 0 deletions changelog_unreleased/javascript/pr-7877.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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