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

Issue with re-assigning exported local variable after declaration. #130

Closed
rwjblue opened this issue Mar 28, 2015 · 5 comments
Closed

Issue with re-assigning exported local variable after declaration. #130

rwjblue opened this issue Mar 28, 2015 · 5 comments

Comments

@rwjblue
Copy link
Contributor

rwjblue commented Mar 28, 2015

Input:

var foo = function() { }

if (false) {
  foo = function () { };  
}

export {
 foo 
}

Output:

define(['exports'], function (exports) {

  'use strict';

  var foo = function() { }

  if (false) {
    exports.foo = foo = function () { };  
  }

});

This causes some issues in Ember, and we have locked Esperanto down to 0.6.17 temporarily (in emberjs/ember.js#10751).

@Rich-Harris
Copy link
Contributor

Ah, yep, I think I see the problem. Thinking out loud: it's trying to avoid doing more exports.foo = ... than is necessary - this line is being run, because isTopLevelNode is true but shouldn't be. The problematic line is here - it's determining whether it's a top level node by seeing if it's in the top level scope, which obviously isn't the case since it's in a block. Should be an easy fix - thanks for raising it

@eventualbuddha
Copy link
Contributor

I believe the expected output should include two exports.foo assignments, one inside the if, the other outside it.

@rwjblue
Copy link
Contributor Author

rwjblue commented Mar 28, 2015

@eventualbuddha - OK, I definitely defer that to y'all. My main issue is that there is no export.foo in the current output (because it is inside the if). I'll just remove the expected output....

@Rich-Harris
Copy link
Contributor

Have opened #131, which fixes this but changes the approach slightly. Previously, esperanto would keep track of any assignments to exported bindings, so that it didn't need the final exports.foo = foo. A simpler approach in my view is to always have exports.foo = foo at the end, and not bother with changing the foo = ... into exports.foo = foo = ... inside the if block.

The one exception to this is when foo is assigned to inside a function body, since that could happen late. So for example this becomes this.

Does that sound right to everyone?

Rich-Harris added a commit that referenced this issue Mar 29, 2015
ensure bindings are always exported (#130)
@Rich-Harris
Copy link
Contributor

Released in 0.6.23

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

No branches or pull requests

3 participants