Fix boolean for empty objects (#1590)

* Add test for empty object in logical expression

* Add check for empty object and array in shouldInlineLogicalExpression

* Review fixes, add additional case with function call
master
Dmitry Rybin 2017-05-12 17:48:03 +03:00 committed by Christopher Chedeau
parent 5ca2117d23
commit 1d9478a61f
4 changed files with 36 additions and 7 deletions

View File

@ -3694,11 +3694,19 @@ function isBinaryish(node) {
}
function shouldInlineLogicalExpression(node) {
return (
node.type === "LogicalExpression" &&
(node.right.type === "ObjectExpression" ||
node.right.type === "ArrayExpression")
);
if (node.type !== "LogicalExpression") {
return false;
}
if (node.right.type === "ObjectExpression" && node.right.properties.length !== 0) {
return true;
}
if (node.right.type === "ArrayExpression" && node.right.elements.length !== 0) {
return true;
}
return false;
}
// For binary expressions to be consistent, we need to group

View File

@ -4,8 +4,10 @@ exports[`empty.js 1`] = `
const a = someVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeLong.Expression || [];
const a = someVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeLong.Expression || {};
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
const a = someVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeLong.Expression || [];
const a = someVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeLong.Expression || {};
const a =
someVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeLong.Expression || [];
const a =
someVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeLong.Expression || {};
`;

View File

@ -202,6 +202,9 @@ const x = longVariable + longVariable * longVariable * longVariable / longVariab
const x = longVariable && longVariable && longVariable && longVariable && longVariable && longVariable;
const x = longVariable && longVariable || longVariable && longVariable || longVariable && longVariable;
const x = firstItemWithAVeryLongNameThatKeepsGoing || firstItemWithAVeryLongNameThatKeepsGoing || {};
const x = firstItemWithAVeryLongNameThatKeepsGoing || firstItemWithAVeryLongNameThatKeepsGoing || [];
const x = call(firstItemWithAVeryLongNameThatKeepsGoing, firstItemWithAVeryLongNameThatKeepsGoing) || [];
const x = longVariable * longint && longVariable >> 0 && longVariable + longVariable;
@ -242,6 +245,19 @@ const x =
(longVariable && longVariable) ||
(longVariable && longVariable) ||
(longVariable && longVariable);
const x =
firstItemWithAVeryLongNameThatKeepsGoing ||
firstItemWithAVeryLongNameThatKeepsGoing ||
{};
const x =
firstItemWithAVeryLongNameThatKeepsGoing ||
firstItemWithAVeryLongNameThatKeepsGoing ||
[];
const x =
call(
firstItemWithAVeryLongNameThatKeepsGoing,
firstItemWithAVeryLongNameThatKeepsGoing
) || [];
const x =
longVariable * longint && longVariable >> 0 && longVariable + longVariable;

View File

@ -8,6 +8,9 @@ const x = longVariable + longVariable * longVariable * longVariable / longVariab
const x = longVariable && longVariable && longVariable && longVariable && longVariable && longVariable;
const x = longVariable && longVariable || longVariable && longVariable || longVariable && longVariable;
const x = firstItemWithAVeryLongNameThatKeepsGoing || firstItemWithAVeryLongNameThatKeepsGoing || {};
const x = firstItemWithAVeryLongNameThatKeepsGoing || firstItemWithAVeryLongNameThatKeepsGoing || [];
const x = call(firstItemWithAVeryLongNameThatKeepsGoing, firstItemWithAVeryLongNameThatKeepsGoing) || [];
const x = longVariable * longint && longVariable >> 0 && longVariable + longVariable;