babel / kneden

Transpile ES2017 async/await to vanilla ES6 Promise chains: a Babel plugin
ISC License
514 stars 41 forks source link

fix: don't hoist functions in objects #49

Closed dariocravero closed 8 years ago

dariocravero commented 8 years ago

Here's a fix for an edge case with kneden's function hoisting from functions defined as object values.

If you're using the es2015 preset, a code like:

async function run() {
  var o1 = {
    a: function() { }
  };
  var o2 = {
    a: function() { }
  };
}

Pre kneden would be converted to:

async function run() {
  var o1 = {
    a: function a() { }
  };
  var o2 = {
    a: function a() { }
  };
}

This is because of babel-helper-function-name. Essentially the functions get a name. The problem is that if different objects have the same keys and those keys happen to have functions as values, their name would be the same. That doesn't seem to matter at runtime though... but! If you pass it through kneden, you get this:


function run() {
  function a() {}
  function a() {}
  var o1, o2;
  return Promise.resolve().then(function () {
    o1 = {
      a: a };
    o2 = {
      a: a };
  });
}

And that's the end of the game because the last definition of a wins and you loose one function :(. Here's a repo with sample code showing the issue.

I first thought this was a babel issue, see https://github.com/babel/babel/pull/3474. The way I see it, babel's output, despite seeming technically sound, feels a bit confusing for its users.

Because I don't know if the fix will make it into babel, I'm filing this PR to prevent kneden from hoisting those types of functions. :)

marten-de-vries commented 8 years ago

Thanks! Merged: dc3ff6640aef62dd1612aa94b2dd3bc9985a5389

dariocravero commented 8 years ago

Good stuff :)

dariocravero commented 8 years ago

Could you publish a patch? Thanks! :)

marten-de-vries commented 8 years ago
+ babel-plugin-async-to-promises@1.0.5
dariocravero commented 8 years ago

Thanks! On Sun 24 Apr 2016 at 15:13, Marten de Vries notifications@github.com wrote:

  • babel-plugin-async-to-promises@1.0.5

— You are receiving this because you authored the thread. Reply to this email directly or view it on GitHub https://github.com/marten-de-vries/kneden/pull/49#issuecomment-213964462