Skip to content

transform-async-to-generator wrongly mark function as pure when called by apply()/call() #17227

@compulim

Description

@compulim

💻

  • Would you like to work on a fix?

How are you using Babel?

@babel/cli

Input code

(async function () {
  console.log('XXXXXXX This is side effect.')
})();

(async function () {
  console.log('YYYYYYY This is side effect but marked as pure.')
}).apply();

(async function () {
  console.log('ZZZZZZZ This is side effect but marked as pure.')
}).call();

Configuration file name

No response

Configuration

In Babel Playground, check "force all transforms."

Current and expected behavior

Diff for readability

Red is current. Green is expected.

  function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); }
  function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; }
_asyncToGenerator(function* () {
    console.log('XXXXXXX This is side effect.');
  })();
- /*#__PURE__*/_asyncToGenerator(function* () {
+ _asyncToGenerator(function* () {
    console.log('YYYYYYY This is side effect but marked as pure.');
  }).apply();
- /*#__PURE__*/_asyncToGenerator(function* () {
+ _asyncToGenerator(function* () {
    console.log('ZZZZZZZ This is side effect but marked as pure.');
  }).call();

Environment

  • Babel version(s): 7.27.0

Possible solution

No response

Additional context

After Babel wrongly marked the function call as pure, Terser will remove the function as it thinks it's dead code.

Terser output

Via try.terser.org.

function n(n,o,t,e,i,r,c){try{var u=n[r](c),f=u.value}catch(n){return void t(n)}u.done?o(f):Promise.resolve(f).then(e,i)}function o(o){return function(){var t=this,e=arguments;return new Promise((function(i,r){var c=o.apply(t,e);function u(o){n(c,i,r,u,f,"next",o)}function f(o){n(c,i,r,u,f,"throw",o)}u(void 0)}))}}o((function*(){console.log("XXXXXXX This is side effect.")}))();

And prettifying Terser result.

function n(n, o, t, e, i, r, c) {
  try {
    var u = n[r](c),
      f = u.value;
  } catch (n) {
    return void t(n);
  }
  u.done ? o(f) : Promise.resolve(f).then(e, i);
}
function o(o) {
  return function () {
    var t = this,
      e = arguments;
    return new Promise(function (i, r) {
      var c = o.apply(t, e);
      function u(o) {
        n(c, i, r, u, f, "next", o);
      }
      function f(o) {
        n(c, i, r, u, f, "throw", o);
      }
      u(void 0);
    });
  };
}
o(function* () {
  console.log("XXXXXXX This is side effect.");
})();

Metadata

Metadata

Assignees

Labels

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions