Skip to content

Commit

Permalink
Remove regenerator hacks in checkDuplicatedNodes (#8220)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andarist authored and nicolo-ribaudo committed Jun 24, 2018
1 parent 12c75cc commit e166275
Showing 1 changed file with 1 addition and 145 deletions.
146 changes: 1 addition & 145 deletions packages/babel-helper-transform-fixture-test-runner/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,6 @@ function wrapPackagesArray(type, names, optionsDir) {
}

function checkDuplicatedNodes(ast) {
// TODO Remove all these function when regenerator doesn't
// insert duplicated nodes

const nodes = new WeakSet();
const parents = new WeakMap();

Expand All @@ -152,156 +149,15 @@ function checkDuplicatedNodes(ast) {
}
};

const parentIs = (node, test) => {
return (parents.get(node) || []).some(parent => test(parent));
};
const isByRegenerator = node => {
if (!node) {
return false;
} else if (node.type === "Identifier") {
if (/^_(?:context|value|callee|marked)\d*$/.test(node.name)) {
return true;
} else if (
/^t\d+$/.test(node.name) &&
parentIs(
node,
parent =>
parent.type === "MemberExpression" &&
isByRegenerator(parent.object),
)
) {
// _context.t* // <-- t*
return true;
} else if (
parentIs(
node,
parent =>
parent.type === "VariableDeclarator" &&
parentIs(
parent,
parent =>
parent.type === "VariableDeclaration" &&
parentIs(
parent,
parent =>
parent.type === "BlockStatement" &&
parentIs(
parent,
parent =>
parent.type === "FunctionExpression" &&
isByRegenerator(parent.id),
),
),
),
)
) {
// regeneratorRuntime.mark(function _callee3() {
// var bar, _bar2; // <-- Those identifiers
return true;
} else if (
parentIs(
node,
parent =>
parent.type === "VariableDeclarator" &&
parentIs(
parent,
parent =>
parent.type === "VariableDeclaration" &&
parentIs(
parent,
parent =>
parent.type === "BlockStatement" &&
parent.body.length === 2 &&
parent.body[1].type === "ReturnStatement" &&
parent.body[1].argument.type === "CallExpression" &&
parent.body[1].argument.callee.type ===
"MemberExpression" &&
parent.body[1].argument.callee.property.type ===
"Identifier" &&
parent.body[1].argument.callee.property.name === "wrap",
),
),
)
) {
// function foo() {
// var _len, // <-- Those identifiers
// items,
// _key,
// _args = arguments;
// return regeneratorRuntime.wrap(function foo$(_context) {
return true;
} else if (
parentIs(
node,
parent =>
parent.type === "CallExpression" &&
parent.arguments.length === 3 &&
parent.arguments[1] === node &&
parent.callee.type === "MemberExpression" &&
parent.callee.property.type === "Identifier" &&
parent.callee.property.name === "wrap",
)
) {
// regeneratorRuntime.wrap(function foo$(_context) {
// ...
// }, foo, this); // <- foo
return true;
} else if (
parentIs(
node,
parent =>
parent.type === "CallExpression" &&
parent.callee.type === "MemberExpression" &&
parent.callee.property.type === "Identifier" &&
parent.callee.property.name === "mark",
)
) {
// regeneratorRuntime.mark(foo); // foo
return true;
}
} else if (node.type === "MemberExpression") {
// _context.next
return isByRegenerator(node.object);
} else if (node.type === "CallExpression") {
return isByRegenerator(node.callee);
} else if (node.type === "AssignmentExpression") {
// _context.next = 4;
return isByRegenerator(node.left);
} else if (node.type === "NumericLiteral") {
if (
parentIs(
node,
parent =>
parent.type === "AssignmentExpression" &&
isByRegenerator(parent.left),
)
) {
// _context.next = 4; // <-- The 4
return true;
} else if (
parentIs(
node,
parent =>
parent.type === "CallExpression" &&
parent.callee.type === "MemberExpression" &&
isByRegenerator(parent.callee.object),
)
) {
// return _context.abrupt("break", 11); // <-- The 11
return true;
}
}
return false;
};
const hidePrivateProperties = (key, val) => {
// Hides properties like _shadowedFunctionLiteral,
// which makes the AST circular
if (key[0] === "_") return "[Private]";
return val;
};

babel.types.traverseFast(ast, node => {
registerChildren(node);
if (isByRegenerator(node)) return;
if (nodes.has(node)) {
throw new Error(
"Do not reuse nodes. Use `t.cloneNode` to copy them.\n" +
Expand Down

0 comments on commit e166275

Please sign in to comment.