Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set correct path.context un push/unshiftContainer #12394

Merged
merged 1 commit into from Nov 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/babel-traverse/src/context.js
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions packages/babel-traverse/src/path/modification.js
Expand Up @@ -217,7 +217,7 @@ export function unshiftContainer(listKey, nodes) {
container: this.node[listKey],
listKey,
key: 0,
});
}).setContext(this.context);

return path._containerInsertBefore(nodes);
}
Expand All @@ -237,7 +237,7 @@ export function pushContainer(listKey, nodes) {
container: container,
listKey,
key: container.length,
});
}).setContext(this.context);

return path.replaceWithMultiple(nodes);
}
Expand Down
40 changes: 40 additions & 0 deletions packages/babel-traverse/test/modification.js
Expand Up @@ -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 () {
Expand Down Expand Up @@ -78,6 +98,26 @@ describe("modification", function () {
},
});
});

it("should set the correct path.context", function () {
expect.assertions(2);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably add this to all the tests where we expect inside a visitor, just in case the visitor doesn't get called!


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 () {
Expand Down