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
base: master
Are you sure you want to change the base?
Conversation
0301a2e
to
0562a52
Compare
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? |
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'; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
closes #350