Skip to content

Commit

Permalink
fix: do not format callbacks with arguments as React hooks (#5778)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB authored and ikatyang committed Jan 21, 2019
1 parent 106fc36 commit 5657316
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
37 changes: 37 additions & 0 deletions CHANGELOG.unreleased.md
Expand Up @@ -41,3 +41,40 @@ Examples:
```
-->

- JavaScript: Do not format functions with arguments as react hooks ([#5778] by [@SimenB])

The formatting added in Prettier 1.16 would format any function receiving an
arrow function and an array literal to match React Hook's documentation.
Prettier will now format this the same as before that change if the arrow
function receives any arguments.

<!-- prettier-ignore -->
```js
// Input
["red", "white", "blue", "black", "hotpink", "rebeccapurple"].reduce(
(allColors, color) => {
return allColors.concat(color);
},
[]
);

// Output (Prettier stable)
["red", "white", "blue", "black", "hotpink", "rebeccapurple"].reduce((
allColors,
color
) => {
return allColors.concat(color);
}, []);

// Output (Prettier master)
["red", "white", "blue", "black", "hotpink", "rebeccapurple"].reduce(
(allColors, color) => {
return allColors.concat(color);
},
[]
);
```

[@simenb]: https://github.com/SimenB
[#5778]: https://github.com/prettier/prettier/pull/5778
1 change: 1 addition & 0 deletions src/language-js/printer-estree.js
Expand Up @@ -3904,6 +3904,7 @@ function printArgumentsList(path, options, print) {
if (
args.length === 2 &&
args[0].type === "ArrowFunctionExpression" &&
args[0].params.length === 0 &&
args[0].body.type === "BlockStatement" &&
args[1].type === "ArrayExpression" &&
!args.find(arg => arg.leadingComments || arg.trailingComments)
Expand Down
15 changes: 15 additions & 0 deletions tests/break-calls/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -42,6 +42,14 @@ const mapChargeItems = fp.flow(
expect(new LongLongLongLongLongRange([0, 0], [0, 0])).toEqualAtomLongLongLongLongRange(new LongLongLongRange([0, 0], [0, 0]));
["red", "white", "blue", "black", "hotpink", "rebeccapurple"].reduce(
(allColors, color) => {
return allColors.concat(color);
},
[]
);
=====================================output=====================================
h(
f(
Expand Down Expand Up @@ -84,6 +92,13 @@ expect(
new LongLongLongLongLongRange([0, 0], [0, 0])
).toEqualAtomLongLongLongLongRange(new LongLongLongRange([0, 0], [0, 0]));
["red", "white", "blue", "black", "hotpink", "rebeccapurple"].reduce(
(allColors, color) => {
return allColors.concat(color);
},
[]
);
================================================================================
`;

Expand Down
8 changes: 8 additions & 0 deletions tests/break-calls/break.js
Expand Up @@ -33,3 +33,11 @@ const mapChargeItems = fp.flow(
);

expect(new LongLongLongLongLongRange([0, 0], [0, 0])).toEqualAtomLongLongLongLongRange(new LongLongLongRange([0, 0], [0, 0]));

["red", "white", "blue", "black", "hotpink", "rebeccapurple"].reduce(
(allColors, color) => {
return allColors.concat(color);
},
[]
);

0 comments on commit 5657316

Please sign in to comment.