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

Let path.remove() remove IfStatement.alternate #14833

Merged
merged 3 commits into from Aug 16, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 1 addition & 2 deletions packages/babel-traverse/src/path/lib/removal-hooks.ts
Expand Up @@ -62,8 +62,7 @@ export const hooks = [

function (self: NodePath, parent: NodePath) {
if (
(parent.isIfStatement() &&
(self.key === "consequent" || self.key === "alternate")) ||
(parent.isIfStatement() && self.key === "consequent") ||
(self.key === "body" &&
(parent.isLoop() || parent.isArrowFunctionExpression()))
) {
Expand Down
18 changes: 18 additions & 0 deletions packages/babel-traverse/test/removal.js
Expand Up @@ -45,4 +45,22 @@ describe("removal", function () {

expect(generate(ast).code).toBe("");
});

describe("within an IfStatement", function () {
it("does not make consequent null", function () {
const rootPath = getPath("if (x) foo(); else bar();");
const ifPath = rootPath.get("body.0");
ifPath.get("consequent").remove();

expect(ifPath.get("consequent").node).not.toBeNull();
nicolo-ribaudo marked this conversation as resolved.
Show resolved Hide resolved
});

it("completely removes alternate", function () {
const rootPath = getPath("if (x) foo(); else bar();");
const ifPath = rootPath.get("body.0");
ifPath.get("alternate").remove();

expect(ifPath.get("alternate").node).toBeNull();
});
});
});