Bogdan Savluk 0058b7fef4
Migrate Babel from Flow to TypeScript (except Babel parser) (#11578)
Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
2021-11-25 23:09:13 +01:00

35 lines
980 B
TypeScript

import explode from "@babel/helper-explode-assignable-expression";
import { assignmentExpression, sequenceExpression } from "@babel/types";
import type { Visitor } from "@babel/traverse";
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 = [];
// @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;
}