Skip to content

Allow extending untranspiled classes - #8656

Merged
nicolo-ribaudo merged 1 commit into
babel:masterfrom
nicolo-ribaudo:extend-native-class-2
Mar 16, 2020
Merged

nicolo-ribaudo merged 1 commit into
babel:masterfrom
nicolo-ribaudo:extend-native-class-2

Conversation

@nicolo-ribaudo

@nicolo-ribaudo nicolo-ribaudo commented Sep 9, 2018 •

Copy link
Copy Markdown
Member
Q                       A
Fixed Issues? Fixes #7022
Patch: Bug Fix?
Major: Breaking Change? Yes?[1]
Minor: New Feature? y
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

Prev: #7081

This PR allows transpiled classes to extends untranspiled classes. This can happen, for example, when a dependency is transpiled using Babel and extends an user-provided class.
Example: Oblosys/react-lifecycle-visualizer#4

The new helper only allows extending native classes when Reflect.construct is natively supported. Some envs (e.g. Node 4) have partial support for classes but don't provide Reflect.construct: in this case users need to ensure that every class (also in their dependencies) is transpiled, like it was before.

[1] If someone sets .constructor to something else, the new helper won't work. e.g.

class A {}
class B extends A {}

B.prototype.constructor = A;

console.log(new B() instanceof A); // true
console.log(new B() instanceof B); // false, should be true

A workaround to avoid the breaking change could be to only use the Reflect-based version of the helper only if Function.toString.call(SuperClass).slice(0, 5) === "class", but in those cases this.constructor still can't be modified.

@nicolo-ribaudo nicolo-ribaudo added PR: New Feature 🚀 A type of pull request used for our changelog categories Spec: Classes labels Sep 9, 2018
@babel-bot

Copy link
Copy Markdown
Collaborator

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

Comment thread packages/babel-helpers/src/helpers.js Resolved
@aminsol

aminsol commented Jun 28, 2019

Copy link
Copy Markdown

Any update? I have posted a question on StackOverflow which I think is related to this:
StackOverflow Question

minrk added a commit to jupyter-book/thebe that referenced this pull request Aug 6, 2019
babel can't transpile classes that extend native classes

babel/babel#8656
minrk added a commit to jupyter-book/thebe that referenced this pull request Aug 6, 2019
babel can't transpile classes that extend native classes

babel/babel#8656
minrk added a commit to jupyter-book/thebe that referenced this pull request Aug 6, 2019
babel can't transpile classes that extend native classes

babel/babel#8656
@1Jesper1

Copy link
Copy Markdown

Merge please.

@vzaidman

Copy link
Copy Markdown

Hey!
Can I help in merging this one somehow?

@ntulele

ntulele commented Dec 31, 2019

Copy link
Copy Markdown

+1 for getting this merged - thanks!

@nicolo-ribaudo nicolo-ribaudo added this to the v7.9.0 milestone Jan 1, 2020
@vzaidman

vzaidman commented Jan 1, 2020

Copy link
Copy Markdown

great!

@JLHwung
JLHwung self-requested a review January 2, 2020 23:24
Comment thread packages/babel-helpers/src/helpers.js Resolved
@github-actions github-actions Bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Jun 16, 2020
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Jun 16, 2020
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: New Feature 🚀 A type of pull request used for our changelog categories PR: Ready For Review Spec: Classes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

How to fix this:Error in mounted hook: "TypeError: Class constructor Base cannot be invoked without 'new'"

8 participants