Set correct path.context un push/unshiftContainer (#12394)
This commit is contained in:
parent
0f3db5deea
commit
695abb8dfc
@ -42,6 +42,8 @@ export default class TraversalContext {
|
|||||||
}
|
}
|
||||||
|
|
||||||
create(node, obj, key, listKey): NodePath {
|
create(node, obj, key, listKey): NodePath {
|
||||||
|
// We don't need to `.setContext()` here, since `.visitQueue()` already
|
||||||
|
// calls `.pushContext`.
|
||||||
return NodePath.get({
|
return NodePath.get({
|
||||||
parentPath: this.parentPath,
|
parentPath: this.parentPath,
|
||||||
parent: node,
|
parent: node,
|
||||||
|
|||||||
@ -217,7 +217,7 @@ export function unshiftContainer(listKey, nodes) {
|
|||||||
container: this.node[listKey],
|
container: this.node[listKey],
|
||||||
listKey,
|
listKey,
|
||||||
key: 0,
|
key: 0,
|
||||||
});
|
}).setContext(this.context);
|
||||||
|
|
||||||
return path._containerInsertBefore(nodes);
|
return path._containerInsertBefore(nodes);
|
||||||
}
|
}
|
||||||
@ -237,7 +237,7 @@ export function pushContainer(listKey, nodes) {
|
|||||||
container: container,
|
container: container,
|
||||||
listKey,
|
listKey,
|
||||||
key: container.length,
|
key: container.length,
|
||||||
});
|
}).setContext(this.context);
|
||||||
|
|
||||||
return path.replaceWithMultiple(nodes);
|
return path.replaceWithMultiple(nodes);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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 () {
|
describe("unshiftContainer", function () {
|
||||||
it("unshifts identifier into params", 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 () {
|
describe("insertBefore", function () {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user