forked from opbeat/babel
/
index.js
52 lines (40 loc) · 1.22 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
import explode from "babel-helper-explode-assignable-expression";
import * as t from "babel-types";
export default function (
exports: Object,
opts: {
build: Function;
is: Function;
},
) {
const buildAssignment = function (left, right) {
return t.assignmentExpression("=", left, right);
};
exports.ExpressionStatement = function (path, file) {
// hit the `AssignmentExpression` one below
if (path.isCompletionRecord()) return;
const expr = path.node.expression;
if (!opts.is(expr, file)) return;
const nodes = [];
const exploded = explode(expr.left, nodes, file, path.scope);
nodes.push(t.ifStatement(
opts.build(exploded.uid, file),
t.expressionStatement(buildAssignment(exploded.ref, expr.right))
));
return nodes;
};
exports.AssignmentExpression = function (path, file) {
const node = path.node;
if (!opts.is(node, file)) return;
const nodes = [];
const exploded = explode(node.left, nodes, file, path.scope);
nodes.push(t.logicalExpression(
"&&",
opts.build(exploded.uid, file),
buildAssignment(exploded.ref, node.right)
));
// todo: duplicate expression node
nodes.push(exploded.ref);
return nodes;
};
}