-
Notifications
You must be signed in to change notification settings - Fork 1
/
commaSeparatedStatements.js
46 lines (44 loc) · 1.22 KB
/
commaSeparatedStatements.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
const estraverse = require('estraverse')
module.exports = parsed => {
return estraverse.replace(parsed, {
enter: node => {
if (
node.type === 'BlockStatement' &&
node.body.length === 1 &&
node.body[0].type === 'ExpressionStatement' &&
node.body[0].expression.type === 'SequenceExpression'
) {
node.body = node.body[0].expression.expressions.map(expression => ({
type: 'ExpressionStatement',
expression
}))
return node
}
if (
node.type === 'BlockStatement' &&
node.body.length > 0
) {
const lastStmt = node.body[node.body.length-1]
if (
lastStmt.type === 'ReturnStatement' &&
lastStmt.argument &&
lastStmt.argument.type === 'SequenceExpression'
) {
node.body.pop()
const exprs = lastStmt.argument.expressions
const last = exprs.pop()
for (const expression of exprs) {
node.body.push({
type: 'ExpressionStatement',
expression
})
}
node.body.push({
type: 'ReturnStatement',
argument: last
})
}
}
}
})
}