Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use babel-check-duplicated-nodes #8768

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -15,6 +15,7 @@
"@babel/core": "^7.1.0",
"@babel/helper-fixtures": "^7.0.0",
"@babel/polyfill": "^7.0.0",
"babel-check-duplicated-nodes": "^1.0.0",
"jest": "^22.4.2",
"jest-diff": "^22.4.0",
"lodash": "^4.17.10",
Expand Down
50 changes: 3 additions & 47 deletions packages/babel-helper-transform-fixture-test-runner/src/index.js
Expand Up @@ -15,6 +15,7 @@ import assert from "assert";
import fs from "fs";
import path from "path";
import vm from "vm";
import checkDuplicatedNodes from "babel-check-duplicated-nodes";

import diff from "jest-diff";

Expand Down Expand Up @@ -129,51 +130,6 @@ function wrapPackagesArray(type, names, optionsDir) {
});
}

function checkDuplicatedNodes(ast) {
const nodes = new WeakSet();
const parents = new WeakMap();

const setParent = (child, parent) => {
if (typeof child === "object" && child !== null) {
let p = parents.get(child);
if (!p) {
p = [];
parents.set(child, p);
}
p.unshift(parent);
}
};
const registerChildren = node => {
for (const key in node) {
if (Array.isArray(node[key])) {
node[key].forEach(child => setParent(child, node));
} else {
setParent(node[key], node);
}
}
};

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 (nodes.has(node)) {
throw new Error(
"Do not reuse nodes. Use `t.cloneNode` to copy them.\n" +
JSON.stringify(node, hidePrivateProperties, 2) +
"\nParent:\n" +
JSON.stringify(parents.get(node), hidePrivateProperties, 2),
);
}
nodes.add(node);
});
}

function run(task) {
const actual = task.actual;
const expected = task.expect;
Expand Down Expand Up @@ -222,7 +178,7 @@ function run(task) {
if (execCode) {
const execOpts = getOpts(exec);
result = babel.transform(execCode, execOpts);
checkDuplicatedNodes(result.ast);
checkDuplicatedNodes(babel, result.ast);
execCode = result.code;

try {
Expand All @@ -244,7 +200,7 @@ function run(task) {
"<CWD>",
);

checkDuplicatedNodes(result.ast);
checkDuplicatedNodes(babel, result.ast);
if (
!expected.code &&
expectedCode &&
Expand Down