Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Crash when removing without Program #16191

Merged
merged 1 commit into from Dec 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
});
});