-
Notifications
You must be signed in to change notification settings - Fork 1
/
staticIf.js
40 lines (39 loc) · 1.07 KB
/
staticIf.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
const estraverse = require('estraverse')
module.exports = parsed => {
return estraverse.replace(parsed, {
enter: node => {
if (
node.type === 'IfStatement' &&
node.test.type === 'BinaryExpression' &&
node.test.left.type === 'Literal' &&
node.test.right.type === 'Literal'
) {
const left = node.test.left.value
const right = node.test.right.value
let result = null
switch (node.test.operator) {
case '==':
result = left == right
? node.consequent
: node.alternate
break
case '===':
result = left === right
? node.consequent
: node.alternate
break
case '!=':
result = left != right
? node.consequent
: node.alternate
break
case '!==':
result = left !== right
? node.consequent
: node.alternate
}
return result || estraverse.VisitorOption.Remove
}
}
})
}