Fix reused nodes - part 2 (#7149)

This commit is contained in:
Nicolò Ribaudo
2018-01-11 19:31:48 +01:00
parent 912bcc186d
commit 252ea5a966
20 changed files with 261 additions and 170 deletions

View File

@@ -7,23 +7,26 @@ export default function() {
t.isIdentifier(node.meta, { name: "function" }) &&
t.isIdentifier(node.property, { name: "sent" });
const hasBeenReplaced = (node, sentId) =>
t.isAssignmentExpression(node) &&
t.isIdentifier(node.left, { name: sentId });
const yieldVisitor = {
Function(path) {
path.skip();
},
YieldExpression(path) {
const replaced = t.isAssignmentExpression(path.parent, {
left: this.sentId,
});
if (!replaced) {
path.replaceWith(t.assignmentExpression("=", this.sentId, path.node));
if (!hasBeenReplaced(path.parent, this.sentId)) {
path.replaceWith(
t.assignmentExpression("=", t.identifier(this.sentId), path.node),
);
}
},
MetaProperty(path) {
if (isFunctionSent(path.node)) {
path.replaceWith(this.sentId);
path.replaceWith(t.identifier(this.sentId));
}
},
};
@@ -41,12 +44,12 @@ export default function() {
throw new Error("Parent generator function not found");
}
const sentId = path.scope.generateUidIdentifier("function.sent");
const sentId = path.scope.generateUid("function.sent");
fnPath.traverse(yieldVisitor, { sentId });
fnPath.node.body.body.unshift(
t.variableDeclaration("let", [
t.variableDeclarator(sentId, t.yieldExpression()),
t.variableDeclarator(t.identifier(sentId), t.yieldExpression()),
]),
);