Use babel-check-duplicated-nodes (#8768)
This commit is contained in:
parent
af694ebae1
commit
889cdb8da5
@ -15,6 +15,7 @@
|
|||||||
"@babel/core": "^7.1.0",
|
"@babel/core": "^7.1.0",
|
||||||
"@babel/helper-fixtures": "^7.0.0",
|
"@babel/helper-fixtures": "^7.0.0",
|
||||||
"@babel/polyfill": "^7.0.0",
|
"@babel/polyfill": "^7.0.0",
|
||||||
|
"babel-check-duplicated-nodes": "^1.0.0",
|
||||||
"jest": "^22.4.2",
|
"jest": "^22.4.2",
|
||||||
"jest-diff": "^22.4.0",
|
"jest-diff": "^22.4.0",
|
||||||
"lodash": "^4.17.10",
|
"lodash": "^4.17.10",
|
||||||
|
|||||||
@ -15,6 +15,7 @@ import assert from "assert";
|
|||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import vm from "vm";
|
import vm from "vm";
|
||||||
|
import checkDuplicatedNodes from "babel-check-duplicated-nodes";
|
||||||
|
|
||||||
import diff from "jest-diff";
|
import diff from "jest-diff";
|
||||||
|
|
||||||
@ -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) {
|
function run(task) {
|
||||||
const actual = task.actual;
|
const actual = task.actual;
|
||||||
const expected = task.expect;
|
const expected = task.expect;
|
||||||
@ -222,7 +178,7 @@ function run(task) {
|
|||||||
if (execCode) {
|
if (execCode) {
|
||||||
const execOpts = getOpts(exec);
|
const execOpts = getOpts(exec);
|
||||||
result = babel.transform(execCode, execOpts);
|
result = babel.transform(execCode, execOpts);
|
||||||
checkDuplicatedNodes(result.ast);
|
checkDuplicatedNodes(babel, result.ast);
|
||||||
execCode = result.code;
|
execCode = result.code;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -244,7 +200,7 @@ function run(task) {
|
|||||||
"<CWD>",
|
"<CWD>",
|
||||||
);
|
);
|
||||||
|
|
||||||
checkDuplicatedNodes(result.ast);
|
checkDuplicatedNodes(babel, result.ast);
|
||||||
if (
|
if (
|
||||||
!expected.code &&
|
!expected.code &&
|
||||||
expectedCode &&
|
expectedCode &&
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user