lebab icon indicating copy to clipboard operation
lebab copied to clipboard

new function to class constructor

Open pherenjito opened this issue 8 years ago • 2 comments

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?

pherenjito avatar Feb 13 '18 14:02 pherenjito

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 avatar Feb 13 '18 15:02 nene

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.

nene avatar Feb 13 '18 15:02 nene