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

Super class getter invoked with incorrect context in loose mode #11064

Open
robwalch opened this issue Jan 29, 2020 · 2 comments
Open

Super class getter invoked with incorrect context in loose mode #11064

robwalch opened this issue Jan 29, 2020 · 2 comments
Labels

Comments

@robwalch
Copy link

@robwalch robwalch commented Jan 29, 2020

Given REPL:

class Base {
  constructor (val) {
    this.val = val;
  }
  get test() {
    return this.val;
  }
}

class Obj extends Base {
  constructor (val) {
    super(val)
  }
  // Override getter and use super
  get test() {
    return super.test;
  }
}

const obj = new Obj(true);
console.log(obj.test);
console.assert(obj.test, 'Ivoking the base class getter super.test should return the instance.val of true ');

The sub-class getter is output as:

    get: function get() {
      return _Base.prototype.test;
    }

When this is invoked, the context of the getter is the prototype rather than the instance so obj.test returns undefined rather than true.

Expected behavior/code
It would be nice if there was a simple solution to this for loose mode. Unfortunately I cannot think of one.

I looked at how a new instance of the base class and Reflect is used without loose mode to invoke the getter... Wow! As cool as that is, I'm going to avoid calling super.getter altogether!!

What might have been helpful in this case is a warning from babel about loose mode and super class getter invocation.

@babel-bot

This comment has been minimized.

Copy link
Collaborator

@babel-bot babel-bot commented Jan 29, 2020

Hey @robwalch! 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."

@robwalch robwalch mentioned this issue Jan 29, 2020
0 of 2 tasks complete
@nicolo-ribaudo

This comment has been minimized.

Copy link
Member

@nicolo-ribaudo nicolo-ribaudo commented Jan 31, 2020

What might have been helpful in this case is a warning from babel about loose mode and super class getter invocation.

The problem is that Babel doesn't analyze the base class (usually it's defined in a separate file), so we don't know if super.foo references a getter, a property or a method 🤔

I think that we should just document this problem.

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

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.