forked from babel/babel
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.ts
35 lines (31 loc) 路 1.02 KB
/
index.ts
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
import explode from "@babel/helper-explode-assignable-expression";
import { assignmentExpression, sequenceExpression } from "@babel/types";
import type { Visitor } from "@babel/traverse";
import type * as t from "@babel/types";
export default function (opts: { build: Function; operator: string }) {
const { build, operator } = opts;
const visitor: Visitor = {
AssignmentExpression(path) {
const { node, scope } = path;
if (node.operator !== operator + "=") return;
const nodes: t.AssignmentExpression[] = [];
// @ts-expect-error todo(flow->ts)
const exploded = explode(node.left, nodes, this, scope);
nodes.push(
assignmentExpression(
"=",
exploded.ref,
build(exploded.uid, node.right),
),
);
path.replaceWith(sequenceExpression(nodes));
},
BinaryExpression(path) {
const { node } = path;
if (node.operator === operator) {
path.replaceWith(build(node.left, node.right));
}
},
};
return visitor;
}