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

Proper handling of super() call semantics #186

Open
2 tasks
nene opened this issue Sep 29, 2016 · 2 comments
Open
2 tasks

Proper handling of super() call semantics #186

nene opened this issue Sep 29, 2016 · 2 comments

Comments

@nene
Copy link
Collaborator

nene commented Sep 29, 2016

Lebab will currently produce invalid ES2015 code for super():

  • super() must be called before any references to this. The following code will fail:

    constructor() {
      this.age = 10; // ReferenceError
      super();
    }
  • super() must always be called from subclass constructor. The following code will fail:

    class Foo extends Bar {
      constructor() {
        this.age = 10; // super() not called
      }
    }

I basically see two solutions:

  • refuse transforming code like this, display a details error message about what's wrong, so user can correct it and then try running Lebab again.
  • do transform the code, but produce an additional warning.

I'm leaning towards the first approach as that's what Lebab has been doing in all other transforms so far.

@nene nene mentioned this issue Sep 29, 2016
6 tasks
@apexearth
Copy link
Contributor

I was looking at this a few weeks ago and it seemed that if any pathway of the code referenced this prior to executing super(), the reference error would occur. I believe that is the extent of the problem.
Do you see any more complexity in the problem beyond that?

I also agree with your first approach.

@nene
Copy link
Collaborator Author

nene commented Sep 30, 2016

No, I think that's it. I was wondering whether one could define a function internally using this that you could call before super() - but I think it's not possible. Well... you could call functions that use this before super(), but the this would be referencing something else than the current object.

We also need to watch out for false-positives. The following code is valid:

constructor() {
    var foo = function() {
        return this;
    };
    super();
    foo.call(this);
}

But this is not:

constructor() {
    var foo = () => {
        return this;
    };
    super();
    foo();
}

Which I think simply comes down to that we should not look for this inside normal functions - only inside arrow-functions.

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

2 participants