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

[decorators] Set method names at compile time instead of at runtime #9244

Merged
merged 1 commit into from Jan 9, 2019

Conversation

nicolo-ribaudo
Copy link
Member

Q                       A
Fixed Issues? Fixes #9234
Patch: Bug Fix? yes
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

In gh-8743 I used Object.defineProperty in the decorators helper
to set the correct methods name: it was needed because methods were
renamed to value. Apparently some browsers set Function#name as
non-configurable: gh-9234, so it throws.

This commit removes that fix from the helper and changes the
transform so that it gives the correct name at compile time. It
doesn't work with computed keys, but it should be ok because we
already don't handle method names in classes and object methods.

Output for methods, classes and decorated classes

Objects:

In:

var key = "k";
var obj = {
  fn() {},
  [key]() {}
}

Out:

var key = "k";

var obj = _defineProperty({
  fn: function fn() {}
}, key, function () {});

Test:

obj.fn.name; // "fn"
obj.k.name; // ""

Classes:

In:

var key = "k";
class Cl {
  fn() {}
  [key]() {}
}

Out:

var key = "k";

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

  _createClass(Cl, [{
    key: "fn",
    value: function fn() {}
  }, {
    key: key,
    value: function () {}
  }]);

  return Cl;
}();

Test:

new Cl().fn.name; // "fn"
new Cl().k.name; // "value"

Decorated classes:

In:

var key = "k";

@(_ => _)
class DecCl {
  fn() {}
  [key]() {}
}

Out:

var key = "k";

let DecCl = _decorate([_ => _], function (_initialize) {
  class DecCl {
    constructor() {
      _initialize(this);
    }

  }

  return {
    F: DecCl,
    d: [{
      kind: "method",
      key: "fn",
      value: function fn() {}
    }, {
      kind: "method",
      key: key,
      value: function () {}
    }]
  };
});

Test:

new Cl().fn.name; // "fn"
new Cl().k.name; // "value"

In babelgh-8743 I used `Object.defineProperty` in the decorators helper
to set the correct methods name: it was needed because methods were
renamed to `value`. Apparently some browsers set `Function#name` as
non-configurable: babelgh-9234, so it throws.

This commit removes that fix from the helper and changes the
transform so that it gives the correct name at compile time. It
doesn't work with computed keys, but it should be ok because we
already don't handle method names in classes and object methods.
@babel-bot
Copy link
Collaborator

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/9662/

@nicolo-ribaudo nicolo-ribaudo added PR: Bug Fix 🐛 A type of pull request used for our changelog categories Spec: Decorators labels Dec 27, 2018
@nicolo-ribaudo nicolo-ribaudo added this to In progress in Class features via automation Dec 27, 2018
@nicolo-ribaudo nicolo-ribaudo merged commit 8e051ca into babel:master Jan 9, 2019
Class features automation moved this from In progress to Done Jan 9, 2019
@nicolo-ribaudo nicolo-ribaudo deleted the deco-method-name branch January 9, 2019 23:45
@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Oct 4, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 4, 2019
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 PR: Bug Fix 🐛 A type of pull request used for our changelog categories Spec: Decorators
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

Decorators throws TypeError in some mobile phones.
4 participants