/
index.js
103 lines (86 loc) · 2.36 KB
/
index.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
"use strict";
const flipSeen = Symbol("flipSeen");
module.exports = function(t) {
return {
hasSeen(node) {
return !!node[flipSeen];
},
// Takes an expressions and determines if it has
// more nodes that could benifit from flipping than not.
shouldFlip(topNode, savings = 0) {
visit(topNode);
return savings > 0;
function visit(node) {
if (t.isUnaryExpression(node, { operator: "!" })) {
savings++;
return;
}
if (t.isLogicalExpression(node)) {
visit(node.left);
visit(node.right);
return;
}
if (
!(
t.isBinaryExpression(node) &&
t.EQUALITY_BINARY_OPERATORS.indexOf(node.operator) > -1
)
) {
// Binary expressions wouldn't hurut because we know how to flip them
savings--;
}
}
},
flip(node, resultNotUsed) {
let lastNodeDesc;
const ret = visit(node);
ret[flipSeen] = true;
if (resultNotUsed && lastNodeDesc) {
const { parent, key } = lastNodeDesc;
if (
parent &&
key &&
t.isUnaryExpression(parent[key], { operator: "!" })
) {
parent[key] = parent[key].argument;
}
}
return ret;
function visit(node, parent, key) {
lastNodeDesc = { parent, key };
if (t.isUnaryExpression(node, { operator: "!" })) {
return node.argument;
}
if (t.isLogicalExpression(node)) {
node.operator = node.operator === "&&" ? "||" : "&&";
node.left = visit(node.left, node, "left");
node.right = visit(node.right, node, "right");
return node;
}
if (t.isBinaryExpression(node)) {
let operator;
switch (node.operator) {
case "!==":
operator = "===";
break;
case "===":
operator = "!==";
break;
case "!=":
operator = "==";
break;
case "==":
operator = "!=";
break;
}
if (operator) {
node.operator = operator;
return node;
}
// Falls through to unary expression
}
return t.unaryExpression("!", node, true);
}
}
};
};