-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathresolveRedundantLogicalExpressions.js
52 lines (51 loc) · 1.5 KB
/
resolveRedundantLogicalExpressions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**
* Remove redundant logical expressions which will always resolve in the same way.
* E.g.
* if (false && ...) do_a(); else do_b(); ==> do_b();
* if (... || true) do_c(); else do_d(); ==> do_c();
* @param {Arborist} arb
* @param {Function} candidateFilter (optional) a filter to apply on the candidates list
* @return {Arborist}
*/
function resolveRedundantLogicalExpressions(arb, candidateFilter = () => true) {
const relevantNodes = [
...(arb.ast[0].typeMap.IfStatement || []),
];
for (let i = 0; i < relevantNodes.length; i++) {
const n = relevantNodes[i];
if (n.test.type === 'LogicalExpression' &&
candidateFilter(n)) {
if (n.test.operator === '&&') {
if (n.test.left.type === 'Literal') {
if (n.test.left.value) {
arb.markNode(n.test, n.test.right);
} else {
arb.markNode(n.test, n.test.left);
}
} else if (n.test.right.type === 'Literal') {
if (n.test.right.value) {
arb.markNode(n.test, n.test.left);
} else {
arb.markNode(n.test, n.test.right);
}
}
} else if (n.test.operator === '||') {
if (n.test.left.type === 'Literal') {
if (n.test.left.value) {
arb.markNode(n.test, n.test.left);
} else {
arb.markNode(n.test, n.test.right);
}
} else if (n.test.right.type === 'Literal') {
if (n.test.right.value) {
arb.markNode(n.test, n.test.right);
} else {
arb.markNode(n.test, n.test.left);
}
}
}
}
}
return arb;
}
export default resolveRedundantLogicalExpressions;