Skip to content

Commit

Permalink
Test
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Jun 29, 2023
1 parent b05da49 commit af1a93e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/babel-traverse/package.json
Expand Up @@ -28,6 +28,7 @@
"globals": "condition:BABEL_8_BREAKING ? ^13.5.0 : ^11.1.0"
},
"devDependencies": {
"@babel/core": "workspace:^",
"@babel/helper-plugin-test-runner": "workspace:^"
},
"engines": {
Expand Down
3 changes: 2 additions & 1 deletion packages/babel-traverse/src/cache.ts
Expand Up @@ -3,10 +3,11 @@ import type NodePath from "./path";
import type Scope from "./scope";
import type { HubInterface } from "./hub";

export let pathsCache: WeakMap<
let pathsCache: WeakMap<
HubInterface | typeof nullHub,
WeakMap<Node, Map<Node, NodePath>>
> = new WeakMap();
export { pathsCache as path };
export let scope: WeakMap<Node, Scope> = new WeakMap();

export function clear() {
Expand Down
40 changes: 38 additions & 2 deletions packages/babel-traverse/test/hub.js
@@ -1,10 +1,46 @@
import assert from "assert";
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";
assert.deepEqual(hub.buildError(null, msg), new TypeError(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();
});
});
1 change: 1 addition & 0 deletions yarn.lock
Expand Up @@ -3941,6 +3941,7 @@ __metadata:
resolution: "@babel/traverse@workspace:packages/babel-traverse"
dependencies:
"@babel/code-frame": "workspace:^"
"@babel/core": "workspace:^"
"@babel/generator": "workspace:^"
"@babel/helper-environment-visitor": "workspace:^"
"@babel/helper-function-name": "workspace:^"
Expand Down

0 comments on commit af1a93e

Please sign in to comment.