Skip to content

Commit a7c0212

Browse files
djpohlyJLHwungnicolo-ribaudo
authored
Let path.remove() remove IfStatement.alternate (#14833)
Co-authored-by: Huáng Jùnliàng <jlhwung@gmail.com> Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
1 parent 343b269 commit a7c0212

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

packages/babel-traverse/src/path/lib/removal-hooks.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ export const hooks = [
6262

6363
function (self: NodePath, parent: NodePath) {
6464
if (
65-
(parent.isIfStatement() &&
66-
(self.key === "consequent" || self.key === "alternate")) ||
65+
(parent.isIfStatement() && self.key === "consequent") ||
6766
(self.key === "body" &&
6867
(parent.isLoop() || parent.isArrowFunctionExpression()))
6968
) {

packages/babel-traverse/test/removal.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,22 @@ describe("removal", function () {
4545

4646
expect(generate(ast).code).toBe("");
4747
});
48+
49+
describe("within an IfStatement", function () {
50+
it("does not make consequent null", function () {
51+
const rootPath = getPath("if (x) foo(); else bar();");
52+
const ifPath = rootPath.get("body.0");
53+
ifPath.get("consequent").remove();
54+
55+
expect(ifPath.get("consequent").type).toBe("BlockStatement");
56+
});
57+
58+
it("completely removes alternate", function () {
59+
const rootPath = getPath("if (x) foo(); else bar();");
60+
const ifPath = rootPath.get("body.0");
61+
ifPath.get("alternate").remove();
62+
63+
expect(ifPath.get("alternate").node).toBeNull();
64+
});
65+
});
4866
});

0 commit comments

Comments
 (0)