From a7c0212dacabb9ad6d58ca7e4b6fe28c1f6c80a8 Mon Sep 17 00:00:00 2001 From: "Devin J. Pohly" Date: Tue, 16 Aug 2022 08:08:09 -0500 Subject: [PATCH] Let `path.remove()` remove `IfStatement.alternate` (#14833) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Huáng Jùnliàng Co-authored-by: Nicolò Ribaudo --- .../src/path/lib/removal-hooks.ts | 3 +-- packages/babel-traverse/test/removal.js | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) 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(); + }); + }); });