Skip to content

Commit

Permalink
Let path.remove() remove IfStatement.alternate (#14833)
Browse files Browse the repository at this point in the history
Co-authored-by: Huáng Jùnliàng <jlhwung@gmail.com>
Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
  • Loading branch information
3 people committed Aug 16, 2022
1 parent 343b269 commit a7c0212
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
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").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();
});
});
});

0 comments on commit a7c0212

Please sign in to comment.