diff --git a/changelog_unreleased/javascript/pr-9662.md b/changelog_unreleased/javascript/pr-9662.md
new file mode 100644
index 000000000..ec48a9bb3
--- /dev/null
+++ b/changelog_unreleased/javascript/pr-9662.md
@@ -0,0 +1,29 @@
+#### Flatten function expression parameters in hugged last argument (#9662 by @thorn0)
+
+
+```jsx
+// Prettier stable
+function* mySagas() {
+ yield effects.takeEvery(rexpress.actionTypes.REQUEST_START, function* ({
+ id
+ }) {
+ console.log(id);
+ yield rexpress.actions(store).writeHead(id, 400);
+ yield rexpress.actions(store).end(id, "pong");
+ console.log("pong");
+ });
+}
+
+// Prettier master
+function* mySagas() {
+ yield effects.takeEvery(
+ rexpress.actionTypes.REQUEST_START,
+ function* ({ id }) {
+ console.log(id);
+ yield rexpress.actions(store).writeHead(id, 400);
+ yield rexpress.actions(store).end(id, "pong");
+ console.log("pong");
+ }
+ );
+}
+```
diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js
index b6be2237f..d9473ed70 100644
--- a/src/language-js/printer-estree.js
+++ b/src/language-js/printer-estree.js
@@ -644,7 +644,16 @@ function printPathNoParens(path, options, print, args) {
]);
case "FunctionDeclaration":
case "FunctionExpression":
- parts.push(printFunctionDeclaration(path, print, options));
+ parts.push(
+ printFunctionDeclaration(
+ path,
+ print,
+ options,
+ args &&
+ args.expandLastArg &&
+ getCallArguments(path.getParentNode()).length > 1
+ )
+ );
if (!n.body) {
parts.push(semi);
}
@@ -3736,7 +3745,7 @@ function canPrintParamsWithoutParens(node) {
);
}
-function printFunctionDeclaration(path, print, options) {
+function printFunctionDeclaration(path, print, options, expandArg) {
const n = path.getValue();
const parts = [];
@@ -3758,7 +3767,7 @@ function printFunctionDeclaration(path, print, options) {
printFunctionTypeParameters(path, options, print),
group(
concat([
- printFunctionParameters(path, print, options),
+ printFunctionParameters(path, print, options, expandArg),
printReturnType(path, print, options),
])
),
diff --git a/tests/js/last-argument-expansion/__snapshots__/jsfmt.spec.js.snap b/tests/js/last-argument-expansion/__snapshots__/jsfmt.spec.js.snap
index c52a30092..54cf63cff 100644
--- a/tests/js/last-argument-expansion/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/js/last-argument-expansion/__snapshots__/jsfmt.spec.js.snap
@@ -186,26 +186,26 @@ a(
exports.examples = [
{
- render: withGraphQLQuery("node(1234567890){image{uri}}", function (
- container,
- data
- ) {
- return (
-
-
-
-
-
- );
- }),
+ render: withGraphQLQuery(
+ "node(1234567890){image{uri}}",
+ function (container, data) {
+ return (
+
+
+
+
+
+ );
+ }
+ ),
},
];
@@ -217,32 +217,32 @@ someReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReal
]
);
-(function webpackUniversalModuleDefinition() {})(this, function (
- __WEBPACK_EXTERNAL_MODULE_85__,
- __WEBPACK_EXTERNAL_MODULE_115__
-) {
- return /******/ (function (modules) {
- // webpackBootstrap
- /******/
- })(
- /************************************************************************/
- /******/ [
- /* 0 */
- /***/ function (module, exports, __webpack_require__) {
- /***/
- },
- /* 1 */
- /***/ function (module, exports, __webpack_require__) {
- /***/
- },
- /* 2 */
- /***/ function (module, exports, __webpack_require__) {
- /***/
- },
+(function webpackUniversalModuleDefinition() {})(
+ this,
+ function (__WEBPACK_EXTERNAL_MODULE_85__, __WEBPACK_EXTERNAL_MODULE_115__) {
+ return /******/ (function (modules) {
+ // webpackBootstrap
/******/
- ]
- );
-});
+ })(
+ /************************************************************************/
+ /******/ [
+ /* 0 */
+ /***/ function (module, exports, __webpack_require__) {
+ /***/
+ },
+ /* 1 */
+ /***/ function (module, exports, __webpack_require__) {
+ /***/
+ },
+ /* 2 */
+ /***/ function (module, exports, __webpack_require__) {
+ /***/
+ },
+ /******/
+ ]
+ );
+ }
+);
================================================================================
`;
@@ -309,6 +309,58 @@ func(
================================================================================
`;
+exports[`function-expression.js format 1`] = `
+====================================options=====================================
+parsers: ["babel", "flow"]
+printWidth: 80
+ | printWidth
+=====================================input======================================
+function* mySagas() {
+ yield effects.takeEvery(
+ rexpress.actionTypes.REQUEST_START,
+ function*({ id }) {
+ console.log(id);
+ yield rexpress.actions(store).writeHead(id, 400);
+ yield rexpress.actions(store).end(id, 'pong');
+ console.log('pong');
+ }
+ );
+}
+
+function mySagas2() {
+ return effects.takeEvery(
+ rexpress.actionTypes.REQUEST_START,
+ function({ id }) {
+ console.log(id);
+ }
+ );
+}
+
+=====================================output=====================================
+function* mySagas() {
+ yield effects.takeEvery(
+ rexpress.actionTypes.REQUEST_START,
+ function* ({ id }) {
+ console.log(id);
+ yield rexpress.actions(store).writeHead(id, 400);
+ yield rexpress.actions(store).end(id, "pong");
+ console.log("pong");
+ }
+ );
+}
+
+function mySagas2() {
+ return effects.takeEvery(
+ rexpress.actionTypes.REQUEST_START,
+ function ({ id }) {
+ console.log(id);
+ }
+ );
+}
+
+================================================================================
+`;
+
exports[`jsx.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow"]
diff --git a/tests/js/last-argument-expansion/function-expression.js b/tests/js/last-argument-expansion/function-expression.js
new file mode 100644
index 000000000..dc369091b
--- /dev/null
+++ b/tests/js/last-argument-expansion/function-expression.js
@@ -0,0 +1,20 @@
+function* mySagas() {
+ yield effects.takeEvery(
+ rexpress.actionTypes.REQUEST_START,
+ function*({ id }) {
+ console.log(id);
+ yield rexpress.actions(store).writeHead(id, 400);
+ yield rexpress.actions(store).end(id, 'pong');
+ console.log('pong');
+ }
+ );
+}
+
+function mySagas2() {
+ return effects.takeEvery(
+ rexpress.actionTypes.REQUEST_START,
+ function({ id }) {
+ console.log(id);
+ }
+ );
+}