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

Convert field initializers in constructor to class fields #288

Open
PrasannaBrabourame opened this issue Mar 26, 2019 · 5 comments
Open

Comments

@PrasannaBrabourame
Copy link

PrasannaBrabourame commented Mar 26, 2019

It is possible to get latest JS conversion?
For Eg : without constructor and 'this' keyword

class User {
  name = "Anonymous";
  sayHi() {
    alert(`Hello, ${this.name}!`);
  }
}
@nene
Copy link
Collaborator

nene commented Mar 26, 2019

Sorry, but I don't understand your question.

Try to formulate your question as:

  • which input do you give to Lebal?
  • what does Lebab output?
  • which output would you expect instead?

@uniibu
Copy link
Contributor

uniibu commented Mar 26, 2019

@nene I think what he meant withlatest JS conversion is like for example with Classes, it is not required to use a constructor and if left out, it will use a default constructor as stated here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/constructor#Default_constructors

However private and public fields like the one he used name = "Anonymous" is still at stage 3 and support are still limited. https://github.com/tc39/proposal-class-fields

@PrasannaBrabourame
Copy link
Author

@nene I am expecting the output which was stated by @uniibu

@nene
Copy link
Collaborator

nene commented Mar 27, 2019

OK. So as I understand it, given input like:

function User() {
  this.name = "Anonymous";
}
User.prototype.sayHi = function() {
  alert(`Hello, ${this.name}!`);
}

Lebab class transform currently produces:

class User {
  constructor() {
    this.name = "Anonymous";
  }

  sayHi() {
    alert(`Hello, ${this.name}!`);
  }
}

But you would instead expect to see:

class User {
  name = "Anonymous";
  sayHi() {
    alert(`Hello, ${this.name}!`);
  }
}

As @uniibu pointed out, class fields are currently a stage-3 proposal. I think it would be a nice feature to implement regardless, as the class fields feature is kinda widely used.

Unfortunately the Espree parser which Lebab uses doesn't support this syntax yet. So first we'd have to switch to another parser like Babylon. I don't think I personally have much motivation to do this. But of course pull requests are always welcome.

@frank-dspeed
Copy link

@nene espree is now acron based so it supports plugins so the syntax is supported

@nene nene changed the title It is possible to get latest JS conversion? Convert field initializers in constructor to class fields Mar 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants