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
This commit is contained in:
Dmitry Rybin authored and Christopher Chedeau committed 2017-05-12 07:48:03 -07:00
1 parent 5ca2117d23
commit 1d9478a61f
4 files changed
+36 -7

No files matched your search

+13 -5
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
@@ -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 || {};
`;
@@ -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;
+3
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;