Initialize NodePath context when using getSibling (#12387)
This commit is contained in:
parent
2b13863680
commit
7d2a14b856
@ -127,7 +127,7 @@ export function getSibling(key: string): NodePath {
|
||||
container: this.container,
|
||||
listKey: this.listKey,
|
||||
key: key,
|
||||
});
|
||||
}).setContext(this.context);
|
||||
}
|
||||
|
||||
export function getPrevSibling(): NodePath {
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
import traverse from "../lib";
|
||||
import { parse } from "@babel/parser";
|
||||
import * as t from "@babel/types";
|
||||
|
||||
describe("path/family", function () {
|
||||
describe("getBindingIdentifiers", function () {
|
||||
@ -81,6 +82,32 @@ describe("path/family", function () {
|
||||
expect(sibling.getAllNextSiblings()).toHaveLength(2);
|
||||
expect(lastSibling.getAllPrevSiblings()).toHaveLength(2);
|
||||
});
|
||||
|
||||
it("should initialize path.scope when needed", function () {
|
||||
const ast = parse("if (0) {}");
|
||||
|
||||
let testHasScope = false;
|
||||
let consequentHasScope = false;
|
||||
|
||||
traverse(ast, {
|
||||
IfStatement(path) {
|
||||
// @babel/traverse pre-traverses the whole tree to populate the initial
|
||||
// scope. Thus, it pre-caches paths for all the original nodes.
|
||||
// We need to introduce two new nodes to avoid using the cached paths
|
||||
// that already have the path.scope property.
|
||||
path.set("test", t.identifier("a"));
|
||||
path.set("consequent", t.expressionStatement(t.identifier("b")));
|
||||
|
||||
const testPath = path.get("test");
|
||||
|
||||
testHasScope = !!testPath.scope;
|
||||
consequentHasScope = !!testPath.getSibling("consequent").scope;
|
||||
},
|
||||
});
|
||||
|
||||
expect(testHasScope).toBe(true);
|
||||
expect(consequentHasScope).toBe(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user