fix: do not format callbacks with arguments as React hooks (#5778)

Ref: https://github.com/prettier/prettier/pull/5608#issuecomment-455860396
This commit is contained in:
Simen Bekkhus authored and Ika committed 2019-01-22 00:26:39 +08:00
1 parent 106fc36c81
commit 565731668f
4 files changed
+61

No files matched your search

+37
View File
@@ -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
View File
@@ -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)
@@ -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(
@@ -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);
},
[]
);
================================================================================
`;
+8
View File
@@ -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);
},
[]
);