Group last argument if it's an empty object with a comment (#2758)

master
Lucas Duailibe 2017-09-05 16:22:25 -03:00 committed by Christopher Chedeau
parent 4d68faf195
commit 15989df6e1
4 changed files with 94 additions and 58 deletions

View File

@ -2844,8 +2844,10 @@ function printMethod(path, options, print) {
function couldGroupArg(arg) {
return (
(arg.type === "ObjectExpression" && arg.properties.length > 0) ||
(arg.type === "ArrayExpression" && arg.elements.length > 0) ||
(arg.type === "ObjectExpression" &&
(arg.properties.length > 0 || arg.comments)) ||
(arg.type === "ArrayExpression" &&
(arg.elements.length > 0 || arg.comments)) ||
arg.type === "TSTypeAssertionExpression" ||
arg.type === "TSAsExpression" ||
arg.type === "FunctionExpression" ||
@ -2863,7 +2865,8 @@ function shouldGroupLastArg(args) {
const lastArg = util.getLast(args);
const penultimateArg = util.getPenultimate(args);
return (
(!lastArg.comments || !lastArg.comments.length) &&
!hasLeadingComment(lastArg) &&
!hasTrailingComment(lastArg) &&
couldGroupArg(lastArg) &&
// If the last two arguments are of the same type,
// disable last element expansion.

View File

@ -184,11 +184,9 @@ expect(() => {}).toTriggerReadyStateChanges([
[1 /*first comment */, 2 /* second comment */, 3];
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
expect(() => {}).toTriggerReadyStateChanges(
[
// Nothing.
]
);
expect(() => {}).toTriggerReadyStateChanges([
// Nothing.
]);
[1 /*first comment */, 2 /* second comment */, 3];

View File

@ -277,6 +277,61 @@ someReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReal
`;
exports[`empty-object.js 1`] = `
func(first, second, third, fourth, fifth, aReallyLongArgumentsListToForceItToBreak, {
// comment
})
func({
// comment
})
func(
{} // comment
)
func(
{}
// comment
)
func(
// comment
{}
)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
func(
first,
second,
third,
fourth,
fifth,
aReallyLongArgumentsListToForceItToBreak,
{
// comment
}
);
func({
// comment
});
func(
{} // comment
);
func(
{}
// comment
);
func(
// comment
{}
);
`;
exports[`jsx.js 1`] = `
const els = items.map(item => (
<div className="whatever">
@ -411,38 +466,12 @@ func(
yes,
[]
);
func(
one,
two,
three,
four,
five,
six,
seven,
eig,
is,
this,
too,
long,
yes,
[
// Comments
]
);
func(
five,
six,
seven,
eig,
is,
this,
too,
long,
yes,
[
// Comments
]
);
func(one, two, three, four, five, six, seven, eig, is, this, too, long, yes, [
// Comments
]);
func(five, six, seven, eig, is, this, too, long, yes, [
// Comments
]);
func(one, two, three, four, five, six, seven, eig, is, this, too, long, no, {});
func(
@ -461,24 +490,9 @@ func(
yes,
{}
);
func(
one,
two,
three,
four,
five,
six,
seven,
eig,
is,
this,
too,
long,
yes,
{
// Comments
}
);
func(one, two, three, four, five, six, seven, eig, is, this, too, long, yes, {
// Comments
});
foo(
(

View File

@ -0,0 +1,21 @@
func(first, second, third, fourth, fifth, aReallyLongArgumentsListToForceItToBreak, {
// comment
})
func({
// comment
})
func(
{} // comment
)
func(
{}
// comment
)
func(
// comment
{}
)