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 class inheritance in IE <=10 (T3041) #3527

Merged
merged 4 commits into from Aug 23, 2016
Merged

Fix class inheritance in IE <=10 (T3041) #3527

merged 4 commits into from Aug 23, 2016

Conversation

danez
Copy link
Member

@danez danez commented Jun 15, 2016

Problem summary

Internet Explorer 10 and below do not support __proto__ at all, they don't have Object.setPrototypeOf(), but they have Object.getPrototypeOf().

When babel is doing the inheritance it is trying to use Object.setPrototypeOf() if this function is available. As mentioned this function isn't available (even with core-js, as also core-js can't polyfill this). In this case the helper falls back to just set __proto__ on the object. For IE <=10 this is just some custom property that gets set on the target, with no special meaning.

In the constructor after the inherit helper finished its work, babel uses Object.getPrototypeOf() to get the prototype. This is supported by IE, but as babel can't set the prototype on the target, this will just return the base Object-prototype and not the wanted superclass.

Proposed solution

The solution simply changes the retrieval of the prototype by first trying to get __proto__ and if not available falling back to Object.getPrototypeOf(). This fixes the behavior in IE as babel reads the custom prop __proto__ that the inherit helper was setting. In all other browsers this should not have any effect, as __proto__ should be the same as Object.getPrototypeOf().

Does anyone see a problem with this solution? Anything that I maybe have missed?

P.S.: I did not yet change any of the tests, as I wanted to get feedback first on the solution.

@danez danez changed the title Fix class inheritance in IE9 & IE10 (T3041) Fix class inheritance in IE <=10 (T3041) Jun 15, 2016
@loganfsmyth
Copy link
Member

Interesting idea. Right now IE9/10 would be expected to use loose-mode to avoid this issue, but you are right this may be another approach.

@danez
Copy link
Member Author

danez commented Jun 15, 2016

The problem with loose-mode for us is that we are not always in control how projects transpile their code. In the concrete example of babylon (which we use in the browser for our internal styleguide) we can't control the transpiling configuration it uses (currently it uses babel6 non-loose since v6.8).
But with this solution the next release would be working in IE<=10 even if babylon doesn't switch to loose mode.

[
this.isStatic ? this.getObjectRef() : t.memberExpression(this.getObjectRef(), t.identifier("prototype"))
]
t.logicalExpression(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe worth making a simple helper function for this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I extracted the logic into a helper within babel-helper-replace-supers

I would have liked to share this method also with the transform-class package, but not sure where to put the helper function then.

@codecov-io
Copy link

codecov-io commented Jun 15, 2016

Current coverage is 87.44%

Merging #3527 into master will decrease coverage by 0.04%

@@             master      #3527   diff @@
==========================================
  Files           194        194          
  Lines          8949       9175   +226   
  Methods        1007       1024    +17   
  Messages          0          0          
  Branches       2004       2062    +58   
==========================================
+ Hits           7829       8023   +194   
- Misses         1120       1152    +32   
  Partials          0          0          

Powered by Codecov. Last updated by 40ec299...7ae67fe

@danez danez added the PR: Bug Fix 🐛 A type of pull request used for our changelog categories label Jul 7, 2016
@ljharb
Copy link
Member

ljharb commented Jul 16, 2016

Any progress on this? Both loose mode and "broken on IE 9 and 10" aren't fun places to be :-(

danez and others added 4 commits July 19, 2016 10:15
Internet Explorer 9&10 do not support __proto__ at all, don't have
Object.setPrototypeOf(), but have Object.getPrototypeOf().

Because of this setting the prototype is not possible, which makes the
babelHelpers.inherits() function to set __proto__ although not supported.

Afterwards Object.getPrototypeOf() is used, but this one is not
respecting the "custom" property __proto__ that we set.

The solution is to check for __proto__ first and afterwards fallback to
Object.getPrototypeOf().
@danez danez added this to the Next Patch milestone Aug 9, 2016
@hzoo hzoo merged commit 10cd651 into babel:master Aug 23, 2016
@danez danez deleted the fix-super-call-ie branch August 23, 2016 19:14
hannesj added a commit to hannesj/react that referenced this pull request Sep 12, 2016
nulltask added a commit to uniba/front-end-boilerplate that referenced this pull request Sep 15, 2016
Haroenv added a commit to algolia/instantsearch that referenced this pull request Jan 26, 2018
We use Babel 6.26.0, which has been patched for this already; see babel/babel#3041 and babel/babel#3527
bobylito pushed a commit to algolia/instantsearch that referenced this pull request Feb 6, 2018
We use Babel 6.26.0, which has been patched for this already; see babel/babel#3041 and babel/babel#3527
@xc2
Copy link

xc2 commented Apr 27, 2018

Hello,

Class inheritance does not work again in ^7.0.0-beta since __proto__ is not supported in IE9/10.

Any plans for IE9/10 supporting?

@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Oct 5, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 5, 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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants