From 565731668f8d4797e163eba812f4dac77cf63648 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Mon, 21 Jan 2019 17:26:39 +0100 Subject: [PATCH] fix: do not format callbacks with arguments as React hooks (#5778) Ref: https://github.com/prettier/prettier/pull/5608#issuecomment-455860396 --- CHANGELOG.unreleased.md | 37 +++++++++++++++++++ src/language-js/printer-estree.js | 1 + .../__snapshots__/jsfmt.spec.js.snap | 15 ++++++++ tests/break-calls/break.js | 8 ++++ 4 files changed, 61 insertions(+) diff --git a/CHANGELOG.unreleased.md b/CHANGELOG.unreleased.md index 7b5a8f5be..e3d39b97e 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 6dc595828..fcfabfffb 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 ca9195dca..35c0b97e7 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 b2a98ca97..9edd24456 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); + }, + [] +); +