Inline last arg function arguments (#847)

```js
SuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperLongCall((err, result) => {
  // comment
});
```

currently breaks into

```js
SuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperLongCall((
  err,
  result,
) => {
  // comment
});
```

which looks bad, instead this PR makes it break

```js
SuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperLongCall(
  (err, result) => {
    // comment
  }
);
```

which look better
master
Christopher Chedeau 2017-03-02 19:29:53 -08:00 committed by James Long
parent 4b237d331e
commit 211e18bed2
3 changed files with 49 additions and 14 deletions

View File

@ -1906,20 +1906,10 @@ function printMethod(path, options, print) {
return concat(parts);
}
function printArgumentsList(path, options, print) {
var printed = path.map(print, "arguments");
if (printed.length === 0) {
return "()";
}
const args = path.getValue().arguments;
function shouldGroupLastArg(args) {
const lastArg = util.getLast(args);
const penultimateArg = util.getPenultimate(args);
// This is just an optimization; I think we could return the
// conditional group for all function calls, but it's more expensive
// so only do it for specific forms.
const groupLastArg = (!lastArg.comments || !lastArg.comments.length) &&
const penultimateArg = util.getPenultimate(args)
return (!lastArg.comments || !lastArg.comments.length) &&
(lastArg.type === "ObjectExpression" ||
lastArg.type === "ArrayExpression" ||
lastArg.type === "FunctionExpression" ||
@ -1933,8 +1923,20 @@ function printArgumentsList(path, options, print) {
// If the last two arguments are of the same type,
// disable last element expansion.
(!penultimateArg || penultimateArg.type !== lastArg.type);
}
if (groupLastArg) {
function printArgumentsList(path, options, print) {
var printed = path.map(print, "arguments");
if (printed.length === 0) {
return "()";
}
const args = path.getValue().arguments;
// This is just an optimization; I think we could return the
// conditional group for all function calls, but it's more expensive
// so only do it for specific forms.
if (shouldGroupLastArg(args)) {
const shouldBreak = printed.slice(0, -1).some(willBreak);
return concat([
printed.some(willBreak) ? breakParent : "",
@ -2018,6 +2020,23 @@ function printFunctionParams(path, print, options) {
lastParam.type === "RestElement") &&
!fun.rest;
// If the parent is a call with the last argument expansion and this is the
// params of the last argument, we dont want the arguments to break and instead
// want the whole expression to be on a new line.
//
// Good: Bad:
// verylongcall( verylongcall((
// (a, b) => { a,
// } b,
// }) ) => {
// })
const parent = path.getParentNode();
if ((parent.type === "CallExpression" || parent.type === "NewExpression") &&
util.getLast(parent.arguments) === path.getValue() &&
shouldGroupLastArg(parent.arguments)) {
return concat(["(", join(", ", printed), ")"]);
}
return concat([
"(",
indent(

View File

@ -149,3 +149,16 @@ export const log = y => {
};
"
`;
exports[`overflow.js 1`] = `
"SuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperLongCall((err, result) => {
// comment
});
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperLongCall(
(err, result) => {
// comment
}
);
"
`;

View File

@ -0,0 +1,3 @@
SuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperLongCall((err, result) => {
// comment
});