You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When destructuring into an exported variable, transform-es2015-modules-commonjs fails to generate code to keep the local variable and the exports value in sync.
"use strict";Object.defineProperty(exports,"__esModule",{value: true});exports.f1=f1;exports.f2=f2;exports.f3=f3;letx=exports.x=0;functionf1(){exports.x=x=1;}functionf2(){[x]=[2];exports.x=x;//this is one possible solution}functionf3(){({ x }={x: 3});exports.x=x;}
Current Generated Code
"use strict";Object.defineProperty(exports,"__esModule",{value: true});exports.f1=f1;exports.f2=f2;exports.f3=f3;letx=exports.x=0;functionf1(){exports.x=x=1;}functionf2(){[x]=[2];}functionf3(){({ x }={x: 3});}
Possible Solution
Add a line to keep the local variable and the export value in sync when destructuring into export variables.
Hey @jonhartnett! We really appreciate you taking the time to report an issue. The collaborators
on this project attempt to help as many people as possible, but we're a limited number of volunteers,
so it's possible this won't be addressed swiftly.
If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack
community that typically always has someone willing to help. You can sign-up here
for an invite.
As far as I know module bindings should be "live", any updates should be visible from outside the module. Furthermore, the line in f3 should behave as nothing more than syntactic sugar for x = ({x: 3}).x
You can see from the differing output if you make that change, that this is indeed a bug and not "unexpected" expected behavior.
Activity
babel-bot commentedon May 24, 2017
Hey @jonhartnett! We really appreciate you taking the time to report an issue. The collaborators
on this project attempt to help as many people as possible, but we're a limited number of volunteers,
so it's possible this won't be addressed swiftly.
If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack
community that typically always has someone willing to help. You can sign-up here
for an invite.
joshwnj commentedon Jun 1, 2017
@jonhartnett this is interesting. Do you know whether this is even expected behaviour of javascript?
I'm not 100% up to scratch with how the bindings work, but it may be because in this case
xis value rather than a reference.For example, if we're dealing with an object reference instead of a value:
calling
f2()does update bothyandexports.y.If anyone can weigh in on whether this is expected behaviour according to the module spec, that would be very helpful :)
jonhartnett commentedon Jun 1, 2017
As far as I know module bindings should be "live", any updates should be visible from outside the module. Furthermore, the line in f3 should behave as nothing more than syntactic sugar for
x = ({x: 3}).xYou can see from the differing output if you make that change, that this is indeed a bug and not "unexpected" expected behavior.
Fix destructured exports
joshwnj commentedon Jun 1, 2017
@jonhartnett ahh you're right - I was thinking from the point of view of the commonjs module but I see what you mean now.
I think I've got something working for this, just pushing a PR for review now.
joshwnj commentedon Jun 1, 2017
@jonhartnett when you have a moment please try #5811 and see if that works for you.
jonhartnett commentedon Jun 2, 2017
@joshwnj That works perfectly
Fix 5768 (#5811)