mirror of
https://github.com/hanzoai/babel.git
synced 2026-09-24 16:56:45 +00:00
47 lines
1.3 KiB
JavaScript
47 lines
1.3 KiB
JavaScript
import { transformSync } from "@babel/core";
|
|
import { Hub } from "../lib/index.js";
|
|
|
|
describe("hub", function () {
|
|
it("default buildError should return TypeError", function () {
|
|
const hub = new Hub();
|
|
const msg = "test_msg";
|
|
expect(hub.buildError(null, msg)).toEqual(new TypeError(msg));
|
|
});
|
|
|
|
it("should be preserved across nested traversals", function () {
|
|
let origHub;
|
|
let innerHub = {};
|
|
let exprHub;
|
|
function plugin({ types: t, traverse }) {
|
|
return {
|
|
visitor: {
|
|
Identifier(path) {
|
|
if (path.node.name !== "foo") return;
|
|
origHub = path.hub;
|
|
|
|
const mem = t.memberExpression(
|
|
t.identifier("property"),
|
|
t.identifier("access"),
|
|
);
|
|
traverse(mem, {
|
|
noScope: true,
|
|
Identifier(path) {
|
|
if (path.node.name === "property") innerHub = path.hub;
|
|
},
|
|
});
|
|
const [p2] = path.insertAfter(mem);
|
|
|
|
exprHub = p2.get("expression").hub;
|
|
},
|
|
},
|
|
};
|
|
}
|
|
|
|
transformSync("foo;", { configFile: false, plugins: [plugin] });
|
|
|
|
expect(origHub).toBeInstanceOf(Object);
|
|
expect(exprHub).toBe(origHub);
|
|
expect(innerHub).toBeUndefined();
|
|
});
|
|
});
|