-
Notifications
You must be signed in to change notification settings - Fork 1
/
expandIfShortcut.js
39 lines (37 loc) · 1.18 KB
/
expandIfShortcut.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
const estraverse = require('estraverse')
const leftTypes = ['BinaryExpression', 'Identifier', 'AssignmentExpression', 'MemberExpression']
const rightTypes = ['AssignmentExpression', 'CallExpression', 'SequenceExpression', 'ConditionalExpression']
module.exports = parsed => {
return estraverse.replace(parsed, {
enter: node => {
if (
node.type === 'ExpressionStatement' &&
node.expression.type === 'LogicalExpression' &&
['&&', '||'].includes(node.expression.operator) // &&
// leftTypes.includes(node.expression.left.type) &&
// rightTypes.includes(node.expression.right.type)
) {
return {
type: 'IfStatement',
test: node.expression.operator === '&&'
? node.expression.left
: {
type: 'UnaryExpression',
operator: '!',
argument: node.expression.left,
prefix: true
},
consequent: {
type: 'BlockStatement',
body: [
{
type: 'ExpressionStatement',
expression: node.expression.right
}
]
}
}
}
}
})
}