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

new function to class constructor #266

Open
pherenjito opened this issue Feb 13, 2018 · 2 comments
Open

new function to class constructor #266

pherenjito opened this issue Feb 13, 2018 · 2 comments
Labels

Comments

@pherenjito
Copy link

pherenjito commented Feb 13, 2018

b = new function() {
  this.c = 'a'; 
}

converts to

b = new () => {
  this.c = 'a'; 
}

Would not this one

b = new class() {
  constructor() {
    this.c = 'a';
  }
}

be better?

@nene
Copy link
Collaborator

nene commented Feb 13, 2018

Hmm... this shouldn't happen. Are you perhaps using some older version of Lebab?

This should not be converted to arrow-function at all, as it will break the use of this.

You can try this with in the latest Lebab in here: https://uniibu.github.io/lebab-ce/

Regarding conversion to class... I'm not really sure. This new function(){} is a pretty rare pattern. Hard to tell if the user really had a class in mind or not. I've seen people unfamiliar with JavaScript writing new function(){} thinking it'll create a new function.

I'd expect your code doesn't contain lots of these new function calls... so it's only a tiny task for you to convert them manually.

@nene
Copy link
Collaborator

nene commented Feb 13, 2018

There is a case when you don't use this:

b = new function() {
  return "hello";
}

that'll be indeed converted to arrow-function:

b = new () => "hello"

The result is kinda silly as the new will have no effect in here. But neither did it in the original code. So that's more in the area of garbage-in, garbage-out. Probably not worth a fix.

Hmm... that will actually result in syntax error as you can't use new with arrow functions. So maybe it would be worth of a fix... though perhaps the error will help out the user in spotting this seriously flawed piece of code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants