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

Fix super method call in private instance method calling overridden method #9801

Merged
merged 3 commits into from Mar 31, 2019
Merged

Fix super method call in private instance method calling overridden method #9801

merged 3 commits into from Mar 31, 2019

Conversation

MattiasBuelens
Copy link
Contributor

Q                       A
Fixed Issues? #9788
Patch: Bug Fix? Yes
Major: Breaking Change? No
Minor: New Feature? No
Tests Added + Pass? Yes
Documentation PR Link No
Any Dependency Changes? No
License MIT

Previously, we transpiled the following code:

class Sub extends Base {
  #privateMethod() {
    return super.superMethod();
  }
}

to:

var _privateMethod2 = function _privateMethod2() {
  return babelHelpers.get(babelHelpers.getPrototypeOf(this), "superMethod", this).call(this);
};

This is incorrect: the lookup happens on the wrong prototype. The prototype chain of this looks something like:

this ---> Sub.prototype ---> Base.prototype ---> Object.prototype ---> null

getPrototypeOf(this) === Sub.prototype, so we look up Sub.prototype.superMethod. This may be overridden by Sub, so it's incorrect for the super call.

With this PR, the transpiled code becomes:

var _privateMethod2 = function _privateMethod2() {
  return babelHelpers.get(babelHelpers.getPrototypeOf(Sub.prototype), "superMethod", this).call(this);
};

getPrototypeOf(Sub.prototype) === Base.prototype, so we look up Base.prototype.superMethod. This is the expected method for the super call.

@babel-bot
Copy link
Collaborator

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/10643/

1 similar comment
@babel-bot
Copy link
Collaborator

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/10643/

@nicolo-ribaudo nicolo-ribaudo added the PR: Bug Fix 🐛 A type of pull request used for our changelog categories label Mar 31, 2019
@nicolo-ribaudo nicolo-ribaudo added this to In progress in Class features via automation Mar 31, 2019
@jridgewell jridgewell merged commit 3c11a4a into babel:master Mar 31, 2019
Class features automation moved this from In progress to Done Mar 31, 2019
@MattiasBuelens MattiasBuelens deleted the fix-9788 branch April 1, 2019 16:01
@MattiasBuelens
Copy link
Contributor Author

You might want to remove the "needs triage" label on #9788, seeing that it's fixed now. 😉

@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Oct 4, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 4, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue PR: Bug Fix 🐛 A type of pull request used for our changelog categories Spec: Private Methods
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

None yet

4 participants