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: traverse.visitors.merge unable to handle enter,exit #15593

Closed
wants to merge 1 commit into from

Conversation

liuxingbaoyu
Copy link
Member

@liuxingbaoyu liuxingbaoyu commented Apr 28, 2023

Q                       A
Fixed Issues? Fixes #15587
Patch: Bug Fix?
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass?
Documentation PR Link
Any Dependency Changes?
License MIT

I'm not sure if this will be a breaking change, although it's a bugfix, since the state parameter didn't support enter and exit at all before then.

@liuxingbaoyu liuxingbaoyu added PR: Bug Fix 🐛 A type of pull request used for our changelog categories pkg: traverse labels Apr 28, 2023
@babel-bot
Copy link
Collaborator

babel-bot commented Apr 28, 2023

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/54580/

key: string,
value: unknown,
): VisitorHandler | void {
function assertVisitorHandler(key: string, value: unknown) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a return assertion here? Like asserts value is VisitorHandler.

value: unknown,
): VisitorHandler | void {
function assertVisitorHandler(key: string, value: unknown) {
if (key === "_exploded" || key === "_verified") return;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We explicitly disallow catch-all enter and exit in Babel plugins:

if (obj.enter || obj.exit) {
throw new Error(
`${msg(
loc,
)} cannot contain catch-all "enter" or "exit" handlers. Please target individual nodes.`,
);
}

If key is _exploded, then value is a visitor processed by explode. In this case, I think we should move the catch-all check before the visitor shape check, so we don't have to handle _exploded and _verified in assertVisitorHandler.

@@ -222,9 +224,43 @@ export function merge(
for (const type of Object.keys(visitor) as (keyof Visitor)[]) {
Copy link
Contributor

@JLHwung JLHwung Apr 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If visitor has enter or exit, then it is a catch all visitor. In this case we don't have to go through its properties, instead we can call wrapWithStateOrWrapper on .enter and .exit and push it to the rootVisitor. In this approach we can set _exploded and _verified directly.

import _traverse, { visitors } from "../lib/index.js";
const traverse = _traverse.default || _traverse;

describe("visitors", () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: should be visitors.merge, or add a new nested describe block.

packages/babel-traverse/test/visitors.js Outdated Show resolved Hide resolved
packages/babel-traverse/test/visitors.js Outdated Show resolved Hide resolved
@liuxingbaoyu
Copy link
Member Author

image
Unfortunately, I don't know how to fix it.🤦‍♂️

@liuxingbaoyu
Copy link
Member Author

Superseded by #15702

@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Oct 6, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 6, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: traverse PR: Bug Fix 🐛 A type of pull request used for our changelog categories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: @babel/traverse enter/exit visitors get incorrectly merged
3 participants