fix: Crash when removing without `Program` (#16191) · babel/babel@d292822

2 files changed

lines changed

Original file line numberDiff line numberDiff line change

@@ -30,8 +30,10 @@ export function _removeFromScope(this: NodePath) {

3030

}

3131
3232

export function _callRemovalHooks(this: NodePath) {

33-

for (const fn of hooks) {

34-

if (fn(this, this.parentPath)) return true;

33+

if (this.parentPath) {

34+

for (const fn of hooks) {

35+

if (fn(this, this.parentPath)) return true;

36+

}

3537

}

3638

}

3739
Original file line numberDiff line numberDiff line change

@@ -152,4 +152,17 @@ describe("removal", function () {

152152
153153

expect(rootPath.scope.hasBinding("x")).toBe(true);

154154

});

155+
156+

it("should not throw when removing without `Program`", function () {

157+

const ast = parse("['1']").program.body[0].expression;

158+
159+

traverse(ast, {

160+

noScope: true,

161+

StringLiteral(path) {

162+

path.remove();

163+

},

164+

});

165+
166+

expect(ast.elements.length).toBe(0);

167+

});

155168

});