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

Incorrect rest operator behavior for async arrow function #3077

Closed
alexeyraspopov opened this issue Nov 16, 2015 · 3 comments
Closed

Incorrect rest operator behavior for async arrow function #3077

alexeyraspopov opened this issue Nov 16, 2015 · 3 comments
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@alexeyraspopov
Copy link

Bug is reproducible in Babel 5.x REPL. I wasn't able to test it with Babel 6, sorry.

Code example

function composeSync(f, g) {
  return (...args) => f(g(...args));
}

function composeAsync(f, g) {
  return async (...args) => f(await g(...args));
}

In first function everything is good: the result of composeSync will be an arrow function which uses rest operator for getting all arguments.

In the composeAsync function behavior is different (maybe because of regenerator's runtime?): arguments for rest operator are taken from outer scope, from composeAsync function.

Any thoughts?

@alexeyraspopov
Copy link
Author

I hope I didn't duplicate an issue.

@jamiebuilds
Copy link
Contributor

I'm 99% sure the root of this problem is the same as #2765. (shadow functions for the informed)

@sebmck ?

@babel-bot
Copy link
Collaborator

Comment originally made by @HMUDesign on 2015-12-23T00:11:06.000Z

I can confirm that I am experiencing this in Babel 6.3.15, being used under webpack.

function fooSync() {
    return (...args) => {
        console.log(args)
    }
}

function fooAsync() {
    return async (...args) => {
        console.log(args)
    }
}

fooSync(1,2)(3,4) logs [3,4] as it should, but fooAsync(1,2)(3,4) logs the arguments that were passed into my webpack module (presumably something that my code should never be able to access).


Comment originally made by @HMUDesign on 2015-12-23T00:13:16.000Z

When viewing the transpiled code, the short arguments-to-array loop in fooSync references arguments, but the one in fooAsync is identical except that it references _arguments, which is defined much higher. Adjusting this in the final bundle to remove the _ makes the console logs correct.


Comment originally made by @UltCombo on 2016-01-29T05:39:56.000Z

This bug is still reproducible on the latest babel-core with the latest "syntax-async-functions" and "transform-async-to-generator" plugins.

It does not only affect rest params, but also default parameters.

Input code:

program.action(async (port = 9356) => {});

Output:

"use strict";

var _arguments = arguments;

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) { return step("next", value); }, function (err) { return step("throw", err); }); } } return step("next"); }); }; }

program.action(function () {
  var ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee() {
    var port = _arguments.length <= 0 || _arguments[0] === undefined ? 9356 : _arguments[0];
    return regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
          case "end":
            return _context.stop();
        }
      }
    }, _callee, undefined);
  })),
      _this = undefined;

  return function (_x) {
    return ref.apply(_this, arguments);
  };
}());

The var _arguments = arguments; is in the wrong place, as @HMUDesign commented.

@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label May 6, 2018
@lock lock bot locked as resolved and limited conversation to collaborators May 6, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue
Projects
None yet
Development

No branches or pull requests

3 participants