JavaScript: arguments with object contains newline in arrow function (#6382)
* Modify to fix arguments with object contains newline in arrow function(nested) * Update CHANGELOG.unreleased.md * Update tests * Modify to refactor * Modify to refactor * Add pr number and link * Update CHANGELOG.unreleased.md * Modify to remove unnecessary null checking * Fix for ArrayPattern * Fix for RestElement * Fix for AssignmentPattern * Update tests * Fix for nested array and object * Update tests * Fix for nested array pattern * Update tests * Modify to discard unnecessary node type check * Fix for rest element * Fix for array in rest * Fix for array in assignment pattern * Update tests * Fix for CallExpression with Assignment * Fix for ArrowFunction with AssignmentPattern * Update tests * Modify to support generally pattern * Update tests * Modify to use path.each instead of path.map * Fix for multi arguments * Fix typo, Argments => Argumentsmaster
parent
a7cae47cdf
commit
7f68db8e88
|
@ -573,7 +573,7 @@ Previously, even if the line length was shorter than `printWidth`, Prettier woul
|
||||||
</template>
|
</template>
|
||||||
```
|
```
|
||||||
|
|
||||||
#### JavaScript: Empty lines in destructured arrow function parameters could break indentation and idempotence ([#6301] by [@sosukesuzuki])
|
#### JavaScript: Fix breaks indentation and idempotency when an arrow function that args include object pattern is passed to a function as parameter. ([#6301] & [#6382] by [@sosukesuzuki])
|
||||||
|
|
||||||
Previously, Prettier indented code strangely when an arrow function whose parameters included an object pattern was passed to a function call as an argument. Also, it broke idempotence. Please see [#6294](https://github.com/prettier/prettier/issues/6294) for details.
|
Previously, Prettier indented code strangely when an arrow function whose parameters included an object pattern was passed to a function call as an argument. Also, it broke idempotence. Please see [#6294](https://github.com/prettier/prettier/issues/6294) for details.
|
||||||
|
|
||||||
|
@ -1116,6 +1116,7 @@ sometimes{{nogaps}}areimportant
|
||||||
[#6640]: https://github.com/prettier/prettier/pull/6640
|
[#6640]: https://github.com/prettier/prettier/pull/6640
|
||||||
[#6646]: https://github.com/prettier/prettier/pull/6646
|
[#6646]: https://github.com/prettier/prettier/pull/6646
|
||||||
[#6673]: https://github.com/prettier/prettier/pull/6673
|
[#6673]: https://github.com/prettier/prettier/pull/6673
|
||||||
|
[#6382]: https://github.com/prettier/prettier/pull/6382
|
||||||
[@brainkim]: https://github.com/brainkim
|
[@brainkim]: https://github.com/brainkim
|
||||||
[@duailibe]: https://github.com/duailibe
|
[@duailibe]: https://github.com/duailibe
|
||||||
[@gavinjoyce]: https://github.com/gavinjoyce
|
[@gavinjoyce]: https://github.com/gavinjoyce
|
||||||
|
|
|
@ -3879,26 +3879,29 @@ function printArgumentsList(path, options, print) {
|
||||||
// b
|
// b
|
||||||
// }) => {}
|
// }) => {}
|
||||||
// );
|
// );
|
||||||
function hasEmptyLineInObjectArgInArrowFunction(arg) {
|
function shouldBreakForArrowFunctionInArguments(arg, argPath) {
|
||||||
return (
|
if (
|
||||||
arg &&
|
!arg ||
|
||||||
arg.type === "ArrowFunctionExpression" &&
|
arg.type !== "ArrowFunctionExpression" ||
|
||||||
arg.params &&
|
!arg.body ||
|
||||||
arg.params.some(
|
arg.body.type !== "BlockStatement" ||
|
||||||
param =>
|
!arg.params ||
|
||||||
param.type &&
|
arg.params.length < 1
|
||||||
param.type === "ObjectPattern" &&
|
) {
|
||||||
param.properties &&
|
return false;
|
||||||
param.properties.some(
|
}
|
||||||
(property, i, properties) =>
|
|
||||||
i < properties.length - 1 &&
|
let shouldBreak = false;
|
||||||
isNextLineEmpty(options.originalText, property, options)
|
argPath.each(paramPath => {
|
||||||
)
|
const printed = concat([print(paramPath)]);
|
||||||
)
|
shouldBreak = shouldBreak || willBreak(printed);
|
||||||
);
|
}, "params");
|
||||||
|
|
||||||
|
return shouldBreak;
|
||||||
}
|
}
|
||||||
|
|
||||||
let anyArgEmptyLine = false;
|
let anyArgEmptyLine = false;
|
||||||
|
let shouldBreakForArrowFunction = false;
|
||||||
let hasEmptyLineFollowingFirstArg = false;
|
let hasEmptyLineFollowingFirstArg = false;
|
||||||
const lastArgIndex = args.length - 1;
|
const lastArgIndex = args.length - 1;
|
||||||
const printedArguments = path.map((argPath, index) => {
|
const printedArguments = path.map((argPath, index) => {
|
||||||
|
@ -3918,7 +3921,10 @@ function printArgumentsList(path, options, print) {
|
||||||
parts.push(",", line);
|
parts.push(",", line);
|
||||||
}
|
}
|
||||||
|
|
||||||
anyArgEmptyLine = hasEmptyLineInObjectArgInArrowFunction(arg);
|
shouldBreakForArrowFunction = shouldBreakForArrowFunctionInArguments(
|
||||||
|
arg,
|
||||||
|
argPath
|
||||||
|
);
|
||||||
|
|
||||||
return concat(parts);
|
return concat(parts);
|
||||||
}, "arguments");
|
}, "arguments");
|
||||||
|
@ -3953,7 +3959,9 @@ function printArgumentsList(path, options, print) {
|
||||||
const shouldBreak =
|
const shouldBreak =
|
||||||
(shouldGroupFirst
|
(shouldGroupFirst
|
||||||
? printedArguments.slice(1).some(willBreak)
|
? printedArguments.slice(1).some(willBreak)
|
||||||
: printedArguments.slice(0, -1).some(willBreak)) || anyArgEmptyLine;
|
: printedArguments.slice(0, -1).some(willBreak)) ||
|
||||||
|
anyArgEmptyLine ||
|
||||||
|
shouldBreakForArrowFunction;
|
||||||
|
|
||||||
// We want to print the last argument with a special flag
|
// We want to print the last argument with a special flag
|
||||||
let printedExpanded;
|
let printedExpanded;
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -133,3 +133,266 @@ foo(
|
||||||
|
|
||||||
}) => a
|
}) => a
|
||||||
);
|
);
|
||||||
|
|
||||||
|
foo(
|
||||||
|
({
|
||||||
|
a: {
|
||||||
|
a,
|
||||||
|
|
||||||
|
b
|
||||||
|
}
|
||||||
|
}) => {}
|
||||||
|
);
|
||||||
|
|
||||||
|
foo(
|
||||||
|
({
|
||||||
|
a: {
|
||||||
|
b: {
|
||||||
|
c,
|
||||||
|
|
||||||
|
d
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}) => {}
|
||||||
|
);
|
||||||
|
|
||||||
|
foo(
|
||||||
|
({
|
||||||
|
a: {
|
||||||
|
b: {
|
||||||
|
c: {
|
||||||
|
d,
|
||||||
|
|
||||||
|
e
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}) => {}
|
||||||
|
);
|
||||||
|
|
||||||
|
foo(
|
||||||
|
({
|
||||||
|
a: {
|
||||||
|
a,
|
||||||
|
|
||||||
|
b
|
||||||
|
}
|
||||||
|
}) => a
|
||||||
|
);
|
||||||
|
|
||||||
|
foo(
|
||||||
|
({
|
||||||
|
a: {
|
||||||
|
b: {
|
||||||
|
c,
|
||||||
|
|
||||||
|
d
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}) => a
|
||||||
|
);
|
||||||
|
|
||||||
|
foo(
|
||||||
|
({
|
||||||
|
a: {
|
||||||
|
b: {
|
||||||
|
c: {
|
||||||
|
d,
|
||||||
|
|
||||||
|
e
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}) => a
|
||||||
|
);
|
||||||
|
|
||||||
|
foo(
|
||||||
|
([
|
||||||
|
{
|
||||||
|
a: {
|
||||||
|
b: {
|
||||||
|
c: {
|
||||||
|
d,
|
||||||
|
|
||||||
|
e
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]) => {}
|
||||||
|
);
|
||||||
|
|
||||||
|
foo(
|
||||||
|
([
|
||||||
|
...{
|
||||||
|
a: {
|
||||||
|
b: {
|
||||||
|
c: {
|
||||||
|
d,
|
||||||
|
|
||||||
|
e
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]) => {}
|
||||||
|
);
|
||||||
|
|
||||||
|
foo(
|
||||||
|
(
|
||||||
|
n = {
|
||||||
|
a: {
|
||||||
|
b: {
|
||||||
|
c: {
|
||||||
|
d,
|
||||||
|
|
||||||
|
e
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
) => {}
|
||||||
|
);
|
||||||
|
|
||||||
|
foo(
|
||||||
|
({
|
||||||
|
x: [
|
||||||
|
{
|
||||||
|
a,
|
||||||
|
|
||||||
|
b
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}) => {}
|
||||||
|
);
|
||||||
|
|
||||||
|
foo(
|
||||||
|
(
|
||||||
|
a = [
|
||||||
|
{
|
||||||
|
a,
|
||||||
|
|
||||||
|
b
|
||||||
|
}
|
||||||
|
]
|
||||||
|
) => a
|
||||||
|
);
|
||||||
|
|
||||||
|
foo(
|
||||||
|
([
|
||||||
|
[
|
||||||
|
{
|
||||||
|
a,
|
||||||
|
|
||||||
|
b
|
||||||
|
}
|
||||||
|
]
|
||||||
|
]) => {}
|
||||||
|
);
|
||||||
|
|
||||||
|
foo(
|
||||||
|
([
|
||||||
|
[
|
||||||
|
[
|
||||||
|
[
|
||||||
|
{
|
||||||
|
a,
|
||||||
|
b: {
|
||||||
|
c,
|
||||||
|
d: {
|
||||||
|
e,
|
||||||
|
|
||||||
|
f
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
]
|
||||||
|
]
|
||||||
|
]) => {}
|
||||||
|
);
|
||||||
|
|
||||||
|
foo(
|
||||||
|
(
|
||||||
|
...{
|
||||||
|
a,
|
||||||
|
|
||||||
|
b
|
||||||
|
}
|
||||||
|
) => {}
|
||||||
|
);
|
||||||
|
|
||||||
|
foo(
|
||||||
|
(
|
||||||
|
...[
|
||||||
|
{
|
||||||
|
a,
|
||||||
|
|
||||||
|
b
|
||||||
|
}
|
||||||
|
]
|
||||||
|
) => {}
|
||||||
|
);
|
||||||
|
|
||||||
|
foo(
|
||||||
|
([
|
||||||
|
...[
|
||||||
|
{
|
||||||
|
a,
|
||||||
|
|
||||||
|
b
|
||||||
|
}
|
||||||
|
]
|
||||||
|
]) => {}
|
||||||
|
);
|
||||||
|
|
||||||
|
foo(
|
||||||
|
(
|
||||||
|
a = [{
|
||||||
|
a,
|
||||||
|
|
||||||
|
b
|
||||||
|
}]
|
||||||
|
) => {}
|
||||||
|
);
|
||||||
|
|
||||||
|
foo(
|
||||||
|
(
|
||||||
|
a = (({
|
||||||
|
a,
|
||||||
|
|
||||||
|
b
|
||||||
|
}) => {})()
|
||||||
|
) => {}
|
||||||
|
);
|
||||||
|
|
||||||
|
foo(
|
||||||
|
(
|
||||||
|
a = f({
|
||||||
|
a,
|
||||||
|
|
||||||
|
b
|
||||||
|
})
|
||||||
|
) => {}
|
||||||
|
);
|
||||||
|
|
||||||
|
foo(
|
||||||
|
(
|
||||||
|
a = ({
|
||||||
|
a,
|
||||||
|
|
||||||
|
b
|
||||||
|
}) => {}
|
||||||
|
) => {}
|
||||||
|
);
|
||||||
|
|
||||||
|
foo(
|
||||||
|
(
|
||||||
|
a = 1 +
|
||||||
|
f({
|
||||||
|
a,
|
||||||
|
|
||||||
|
b
|
||||||
|
})
|
||||||
|
) => {}
|
||||||
|
);
|
||||||
|
|
Loading…
Reference in New Issue