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

Static method arrow function does not name anonymous function #7425

Open
reggi opened this issue Feb 24, 2018 · 8 comments
Open

Static method arrow function does not name anonymous function #7425

reggi opened this issue Feb 24, 2018 · 8 comments

Comments

@reggi
Copy link

reggi commented Feb 24, 2018

I'd expect M.blue.name to be blue, but for some reason arrow functions don't have a name. Is this a bug or intentional?

class M {
static green () { return 'green' }
static blue = () => 'green'
}
module.exports = M
var M = function () {
  function M() {
    _classCallCheck(this, M);
  }

  _createClass(M, null, [{
    key: 'green',
    value: function green() {
      return 'green';
    }
  }]);

  return M;
}();

M.blue = function () {
    'green'
};

Guess I'd love it to be like this instead:

M.blue = function blue () {
    'green'
};
@babel-bot
Copy link
Collaborator

Hey @reggi! We really appreciate you taking the time to report an issue. The collaborators
on this project attempt to help as many people as possible, but we're a limited number of volunteers,
so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack
community that typically always has someone willing to help. You can sign-up here
for an invite.

@thymikee
Copy link
Contributor

thymikee commented Feb 27, 2018

Can you provide a repro? What are the plugins/presets you're using?
I can only verify that with

"plugins": [
  "@babel/plugin-proposal-class-properties"
]

produces this output

class M {
  static green() {
    return 'green';
  }

}

Object.defineProperty(M, "blue", {
  configurable: true,
  enumerable: true,
  writable: true,
  value: () => 'green'
});
module.exports = M;

and M.blue.name === 'value', so it looks like a bug.

Babel & plugin version: 7.0.0.beta.40

@Andarist
Copy link
Member

Andarist commented Feb 27, 2018

@thymikee I guess he is using loose mode which doesn't use defineProperty for static methods, just a simple assignments.

Also defineProperty usage is related to what you can see - value inferred as name of this function because it's used as value (:<) of the descriptor's property called value

And it definitely could be polished, I'm not sure if function naming is centralized any where (this would mean that it's probably a bug) or is it scattered around the codebase and dependant on some helper usage (this would mean that we should detect the call site - where those functions are inserted and use helper on the inserted node)

@thymikee
Copy link
Contributor

Oh, that's unfortunate. Looks like that arrow should be eventually replaced with a named function in both cases, right?

@Andarist
Copy link
Member

Ain't sure if class properties (static or not) have inferred names - I think spec says they can, but I'm not the best ecma spec reader :/

@thymikee
Copy link
Contributor

I'm not an expert either, but looking at https://tc39.github.io/proposal-class-public-fields/#initialize-public-instance-fields I could tell the spec says .name should be derived from the field name:

screen shot 2018-02-27 at 23 30 08

@Andarist
Copy link
Member

Yeah, I suspect this is right reasoning. I guess the only way to 'fix' it is to transform directly to named functions, because at later point any other transform won't have enough info to infer the name correctly (just like in the value example).

@vivek12345
Copy link
Contributor

Does this still need help? Can I pick this up?

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

5 participants