diff --git a/packages/babel-traverse/src/path/lib/removal-hooks.ts b/packages/babel-traverse/src/path/lib/removal-hooks.ts index 7872299b4f0c..105fb9cfea2f 100644 --- a/packages/babel-traverse/src/path/lib/removal-hooks.ts +++ b/packages/babel-traverse/src/path/lib/removal-hooks.ts @@ -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())) ) { diff --git a/packages/babel-traverse/test/removal.js b/packages/babel-traverse/test/removal.js index 210304c736e9..e1b313e452c1 100644 --- a/packages/babel-traverse/test/removal.js +++ b/packages/babel-traverse/test/removal.js @@ -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").type).toBe("BlockStatement"); + }); + + 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(); + }); + }); });