diff --git a/CHANGELOG.unreleased.md b/CHANGELOG.unreleased.md index 7b5a8f5be608..e3d39b97eab2 100644 --- a/CHANGELOG.unreleased.md +++ b/CHANGELOG.unreleased.md @@ -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. + + + ```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 diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index 6dc595828516..fcfabfffbc93 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -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) diff --git a/tests/break-calls/__snapshots__/jsfmt.spec.js.snap b/tests/break-calls/__snapshots__/jsfmt.spec.js.snap index ca9195dcaeef..35c0b97e78e7 100644 --- a/tests/break-calls/__snapshots__/jsfmt.spec.js.snap +++ b/tests/break-calls/__snapshots__/jsfmt.spec.js.snap @@ -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); + }, + [] +); + ================================================================================ `; diff --git a/tests/break-calls/break.js b/tests/break-calls/break.js index b2a98ca97316..9edd24456ec9 100644 --- a/tests/break-calls/break.js +++ b/tests/break-calls/break.js @@ -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); + }, + [] +); +