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: empty destructuring with var should be converted to const #351

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

pionxzh
Copy link
Contributor

@pionxzh pionxzh commented Oct 1, 2023

closes #350

@nene
Copy link
Collaborator

nene commented Oct 1, 2023

Thanks for bringing up this interesting corner-case. Does Babel actually generate code containing such empty destructuring cases or is it just an odd corner-case that the tests of Babel check?

@pionxzh
Copy link
Contributor Author

pionxzh commented Oct 1, 2023

It's more like corner cases that Babel is ensuring they are doing it right because they are... well... valid. You usually won't get this unless you do write one.

// Mix of empty destructuring and other variable declarations will be handled
// by other parts of the transform.
if (node.kind === 'var' && node.declarations.every(isEmptyDestructuring)) {
node.kind = 'const';
Copy link
Collaborator

Choose a reason for hiding this comment

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

Modifying the node.kind field here is definitely the easy way to fix this, but it doesn't align with how the rest of this transform is organized. Until now, there have been two distinct phases:

  • first gather all the data of all the var-declarations to be modified.
  • then go and convert the vars to let/const.

This change breaks this pattern. Now some var-declarations get modified during this first loop of gathering data, which breaks expectations of somebody reading this code in the future.

I'd rather leave these var-s unchanged than do it this way.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

At first, I was trying to do it in the transformation phase, but nothing about empty restructuring will be collected in the "gathering data" stage. It's empty. 😢 That's why I ended up putting it here.

Copy link
Collaborator

@nene nene Oct 1, 2023

Choose a reason for hiding this comment

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

Yeah, it would need to involve some more reorganization. Currently this conversion loop goes through all variables, and takes the var-declarations of them, but as there are no variables here such an empty declaration will never be reached.

It's definitely not an easy change to make. There's a currently a heavily beaked-in assumption that all variable declarations actually declare at least a single variable.

One possible middle-ground solution would be to separately gather these empty declaration nodes and then separately loop through them in the modification-phase. This way at least the modification and gathering phases will be kept separate, avoiding confusion in that area.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea. I will try to separate them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

var to let/const with empty destructuring
2 participants