-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
gatherSequenceExpressions.js
92 lines (82 loc) 路 2.62 KB
/
gatherSequenceExpressions.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
// @flow
import type { Scope } from "@babel/traverse";
import getBindingIdentifiers from "../retrievers/getBindingIdentifiers";
import {
isExpression,
isExpressionStatement,
isVariableDeclaration,
isIfStatement,
isBlockStatement,
isEmptyStatement,
} from "../validators/generated";
import {
sequenceExpression,
assignmentExpression,
conditionalExpression,
} from "../builders/generated";
import cloneNode from "../clone/cloneNode";
export default function gatherSequenceExpressions(
nodes: Array<Object>,
scope: Scope,
declars: Array<Object>,
): ?Object {
const exprs = [];
let ensureLastUndefined = true;
for (const node of nodes) {
// if we encounter emptyStatement before a non-emptyStatement
// we want to disregard that
if (!isEmptyStatement(node)) {
ensureLastUndefined = false;
}
if (isExpression(node)) {
exprs.push(node);
} else if (isExpressionStatement(node)) {
exprs.push(node.expression);
} else if (isVariableDeclaration(node)) {
if (node.kind !== "var") return; // bailed
for (const declar of (node.declarations: Array<any>)) {
const bindings = getBindingIdentifiers(declar);
for (const key of Object.keys(bindings)) {
declars.push({
kind: node.kind,
id: cloneNode(bindings[key]),
});
}
if (declar.init) {
exprs.push(assignmentExpression("=", declar.id, declar.init));
}
}
ensureLastUndefined = true;
} else if (isIfStatement(node)) {
const consequent = node.consequent
? gatherSequenceExpressions([node.consequent], scope, declars)
: scope.buildUndefinedNode();
const alternate = node.alternate
? gatherSequenceExpressions([node.alternate], scope, declars)
: scope.buildUndefinedNode();
if (!consequent || !alternate) return; // bailed
exprs.push(conditionalExpression(node.test, consequent, alternate));
} else if (isBlockStatement(node)) {
const body = gatherSequenceExpressions(node.body, scope, declars);
if (!body) return; // bailed
exprs.push(body);
} else if (isEmptyStatement(node)) {
// empty statement so ensure the last item is undefined if we're last
// checks if emptyStatement is first
if (nodes.indexOf(node) === 0) {
ensureLastUndefined = true;
}
} else {
// bailed, we can't turn this statement into an expression
return;
}
}
if (ensureLastUndefined) {
exprs.push(scope.buildUndefinedNode());
}
if (exprs.length === 1) {
return exprs[0];
} else {
return sequenceExpression(exprs);
}
}