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 referencing this in async method default arguments #28

Closed
jimmydief opened this issue Apr 11, 2019 · 2 comments
Closed

Issue with referencing this in async method default arguments #28

jimmydief opened this issue Apr 11, 2019 · 2 comments

Comments

@jimmydief
Copy link
Contributor

Thanks for all of the work on this awesome plugin! I came across an instance where the plugin, when combined with @babel/preset-env, generates some invalid code.

Original code:

class A {
	async a(x = this.x())  {}
}

The issue is that the line that applies the default arg references _this but appears outside of the scope where _this is defined.

Generated code:

"use strict";

function _instanceof(left, right) { if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) { return right[Symbol.hasInstance](left); } else { return left instanceof right; } }

function _classCallCheck(instance, Constructor) { if (!_instanceof(instance, Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

var A =
/*#__PURE__*/
function () {
  function A() {
    _classCallCheck(this, A);
  }

  _createClass(A, [{
    key: "a",
    value: function () {
      var x = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _this.x();
      return function () {
        var _this = this;
      }.apply(this);
    }
  }]);

  return A;
}();

Heres' my Babel config:

{
	presets: ['env'],
	plugins: ['transform-async-to-promises']
}

Here it is on the Babel REPL.

And some version info:
node: 8.11.1
@babel/core: 7.3.3
@babel/preset-env: 7.3.1
babel-plugin-transform-async-to-promises: 0.8.5

@rpetrich
Copy link
Owner

@jimmydief Thanks for sending this test case. It exposed the lack of proper support for default arguments. This should be remedied in 0.8.10

@jimmydief
Copy link
Contributor Author

Thanks again for the quick fix!

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

2 participants