Skip to content

Commit

Permalink
fix: respect newlines in parameters (prettier#5836)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi authored and duailibe committed Apr 4, 2019
1 parent 9cb50ad commit dcaed91
Show file tree
Hide file tree
Showing 7 changed files with 610 additions and 13 deletions.
58 changes: 58 additions & 0 deletions CHANGELOG.unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,64 @@ Examples:
-->

- JavaScript: Respect newlines between parameters ([#5260] by [@evilebottnawi])

<!-- prettier-ignore -->
```js
// Input
function foo(
one,

two,
three,
four,


five,
six,
seven,
eight,
nine,
ten,

eleven

) {}

// Output (Prettier stable)
function foo(
one,
two,
three,
four,
five,
six,
seven,
eight,
nine,
ten,
eleven
) {}

// Output (Prettier master)
function foo(
one,

two,
three,
four,

five,
six,
seven,
eight,
nine,
ten,

eleven
) {}
```

- JavaScript: Fix multiline dynamic import comments ([#6025] by [@noahsug])

<!-- prettier-ignore -->
Expand Down
51 changes: 38 additions & 13 deletions src/language-js/printer-estree.js
Original file line number Diff line number Diff line change
Expand Up @@ -4104,15 +4104,45 @@ function printFunctionTypeParameters(path, options, print) {

function printFunctionParams(path, print, options, expandArg, printTypeParams) {
const fun = path.getValue();
const parent = path.getParentNode();
const paramsField = fun.parameters ? "parameters" : "params";
const isParametersInTestCall = isTestCall(parent);
const shouldHugParameters = shouldHugArguments(fun);
const shouldExpandParameters =
expandArg && !(fun[paramsField] && fun[paramsField].some(n => n.comments));

const typeParams = printTypeParams
? printFunctionTypeParameters(path, options, print)
: "";

let printed = [];
if (fun[paramsField]) {
printed = path.map(print, paramsField);
const lastArgIndex = fun[paramsField].length - 1;

printed = path.map((childPath, index) => {
const parts = [];
const param = childPath.getValue();

parts.push(print(childPath));

if (index === lastArgIndex) {
if (fun.rest) {
parts.push(",", line);
}
} else if (
isParametersInTestCall ||
shouldHugParameters ||
shouldExpandParameters
) {
parts.push(", ");
} else if (isNextLineEmpty(options.originalText, param, options)) {
parts.push(",", hardline, hardline);
} else {
parts.push(",", line);
}

return concat(parts);
}, paramsField);
}

if (fun.rest) {
Expand Down Expand Up @@ -4150,15 +4180,12 @@ function printFunctionParams(path, print, options, expandArg, printTypeParams) {
// } b,
// }) ) => {
// })
if (
expandArg &&
!(fun[paramsField] && fun[paramsField].some(n => n.comments))
) {
if (shouldExpandParameters) {
return group(
concat([
removeLines(typeParams),
"(",
join(", ", printed.map(removeLines)),
concat(printed.map(removeLines)),
")"
])
);
Expand All @@ -4171,15 +4198,13 @@ function printFunctionParams(path, print, options, expandArg, printTypeParams) {
// b,
// c
// }) {}
if (shouldHugArguments(fun)) {
return concat([typeParams, "(", join(", ", printed), ")"]);
if (shouldHugParameters) {
return concat([typeParams, "(", concat(printed), ")"]);
}

const parent = path.getParentNode();

// don't break in specs, eg; `it("should maintain parens around done even when long", (done) => {})`
if (isTestCall(parent)) {
return concat([typeParams, "(", join(", ", printed), ")"]);
if (isParametersInTestCall) {
return concat([typeParams, "(", concat(printed), ")"]);
}

const isFlowShorthandWithOneArg =
Expand Down Expand Up @@ -4211,7 +4236,7 @@ function printFunctionParams(path, print, options, expandArg, printTypeParams) {
return concat([
typeParams,
"(",
indent(concat([softline, join(concat([",", line]), printed)])),
indent(concat([softline, concat(printed)])),
ifBreak(
canHaveTrailingComma && shouldPrintComma(options, "all") ? "," : ""
),
Expand Down
Loading

0 comments on commit dcaed91

Please sign in to comment.