Skip to content

Commit

Permalink
feat(removecalls): handle nested conditional expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
merceyz committed May 27, 2019
1 parent 07dcd92 commit 2110589
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
34 changes: 27 additions & 7 deletions src/visitors/removeUnnecessaryCalls.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,33 @@ const visitor = {
}

function isSafeConditional(node) {
return (
t.isConditionalExpression(node) &&
t.isStringLiteral(node.consequent) &&
t.isStringLiteral(node.alternate) &&
node.consequent.value.length > 0 &&
node.alternate.value.length > 0
);
if (!t.isConditionalExpression(node)) {
return false;
}

const { consequent, alternate } = node;

if (
t.isStringLiteral(consequent) &&
t.isStringLiteral(alternate) &&
consequent.value.length > 0 &&
alternate.value.length > 0
) {
return true;
}

if (
(t.isStringLiteral(consequent) &&
consequent.value.length > 0 &&
isSafeConditional(alternate)) ||
(t.isStringLiteral(alternate) &&
alternate.value.length > 0 &&
isSafeConditional(consequent))
) {
return true;
}

return false;
}
},
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const x = clsx({
foo1: true,
foo2: foo,
foo3: foo,
foo4: !foo && !bar,
foo5: !foo && bar,
foo6: true,
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const x = 'foo1 foo6 ' + (foo ? 'foo2 foo3' : bar ? 'foo5' : 'foo4');

0 comments on commit 2110589

Please sign in to comment.