codemix/babel-plugin-closure-elimination

Async function in concise object method throws Duplicate declaration

Kingles opened this issue · 3 comments

Given

function alpha() {
  return {
    bravo() {
      async function charlie() {}
    },
  };
}
Module build failed: TypeError: /Users/jmaligie/code/my-project/usecase.js: Duplicate declaration "charlie" (This is an error on an internal node. Probably an internal error)

This error does not occur for the following code, which uses an arrow function:

function alpha() {
  return {
    bravo: () => {
      async function charlie() {}
    },
  };
}
Gvozd commented

You use transform-async-to-generator, right?
It's included in preset-es2017, or maybe in some another presets

I add tests for this case at branch issued#25
But at now I have no idea how to fix this problem.
I'll try to look tomorrow, but I do not promise anything.

yes, using preset-env, which is using the transform-async-to-generator plugin

Gvozd commented

Try 1.1.15
Crash fixed
Now plugin generate this code for your example

"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = alpha;

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

function* _ref2() {}

function _ref3() {
  var _ref = _asyncToGenerator(_ref2);

  return function charlie() {
    return _ref.apply(this, arguments);
  };
}

function alpha() {
  return {
    bravo() {
      let charlie = _ref3();
    }
  };
}

charlie eliminated, but bravo property function not eliminated
If you need to support for property function you may create issue, but it will be fixed at low priority