Skip to content

Commit

Permalink
fix: Crash when removing without Program (#16191)
Browse files Browse the repository at this point in the history
fix
  • Loading branch information
liuxingbaoyu committed Dec 24, 2023
1 parent 3dd529d commit d292822
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
6 changes: 4 additions & 2 deletions packages/babel-traverse/src/path/removal.ts
Expand Up @@ -30,8 +30,10 @@ export function _removeFromScope(this: NodePath) {
}

export function _callRemovalHooks(this: NodePath) {
for (const fn of hooks) {
if (fn(this, this.parentPath)) return true;
if (this.parentPath) {
for (const fn of hooks) {
if (fn(this, this.parentPath)) return true;
}
}
}

Expand Down
13 changes: 13 additions & 0 deletions packages/babel-traverse/test/removal.js
Expand Up @@ -152,4 +152,17 @@ describe("removal", function () {

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

it("should not throw when removing without `Program`", function () {
const ast = parse("['1']").program.body[0].expression;

traverse(ast, {
noScope: true,
StringLiteral(path) {
path.remove();
},
});

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

0 comments on commit d292822

Please sign in to comment.