Set correct path.context un push/unshiftContainer (#12394)

This commit is contained in:
Nicolò Ribaudo 2020-11-25 16:51:58 +01:00 committed by GitHub
parent 0f3db5deea
commit 695abb8dfc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 2 deletions

View File

@ -42,6 +42,8 @@ export default class TraversalContext {
}
create(node, obj, key, listKey): NodePath {
// We don't need to `.setContext()` here, since `.visitQueue()` already
// calls `.pushContext`.
return NodePath.get({
parentPath: this.parentPath,
parent: node,

View File

@ -217,7 +217,7 @@ export function unshiftContainer(listKey, nodes) {
container: this.node[listKey],
listKey,
key: 0,
});
}).setContext(this.context);
return path._containerInsertBefore(nodes);
}
@ -237,7 +237,7 @@ export function pushContainer(listKey, nodes) {
container: container,
listKey,
key: container.length,
});
}).setContext(this.context);
return path.replaceWithMultiple(nodes);
}

View File

@ -49,6 +49,26 @@ describe("modification", function () {
},
});
});
it("should set the correct path.context", function () {
expect.assertions(2);
const ast = parse("[b];");
traverse(ast, {
skipKeys: ["consequent"],
ExpressionStatement(path) {
path.traverse({ Identifier() {}, skipKeys: [] });
const arr = path.get("expression");
const x = arr.pushContainer("elements", [
{ type: "Identifier", name: "x" },
])[0];
expect(x.node.name).toBe("x");
expect(x.opts.skipKeys).toEqual(["consequent"]);
},
});
});
});
describe("unshiftContainer", function () {
it("unshifts identifier into params", function () {
@ -78,6 +98,26 @@ describe("modification", function () {
},
});
});
it("should set the correct path.context", function () {
expect.assertions(2);
const ast = parse("[b];");
traverse(ast, {
skipKeys: ["consequent"],
ExpressionStatement(path) {
path.traverse({ Identifier() {}, skipKeys: [] });
const arr = path.get("expression");
const x = arr.unshiftContainer("elements", [
{ type: "Identifier", name: "x" },
])[0];
expect(x.node.name).toBe("x");
expect(x.opts.skipKeys).toEqual(["consequent"]);
},
});
});
});
describe("insertBefore", function () {