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

Support iterating generators in browsers without Symbol #13129

Merged
merged 5 commits into from Apr 15, 2021

Conversation

nicolo-ribaudo
Copy link
Member

@nicolo-ribaudo nicolo-ribaudo commented Apr 9, 2021

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

In browsers without Symbol support and without a Symbol polyfill, regenerator uses @@iterator/@@asyncIterator to mark (async) generators as iterable. (ref)

Since from a user point of view regenerator is effectively part of Babel (because we use it by default in @babel/preset-env), we should make sure that it works well with the other plugins.

This PR adds checks for @@iterator/@@asyncIterator in the spread and for-of helpers, so that they work with compiled generators.

@nicolo-ribaudo nicolo-ribaudo added PR: Bug Fix 🐛 A type of pull request used for our changelog categories pkg: generator and removed pkg: generator labels Apr 9, 2021
@babel-bot
Copy link
Collaborator

babel-bot commented Apr 9, 2021

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

@codesandbox-ci
Copy link

codesandbox-ci bot commented Apr 9, 2021

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit a664ac3:

Sandbox Source
babel-repl-custom-plugin Configuration
babel-plugin-multi-config Configuration

@matthieusieben
Copy link

That was fast 🤩
You rock

@zloirock
Copy link
Member

zloirock commented Apr 9, 2021

It's a crutch and solves only a part of the problem. As you could see, #13128 use polyfilling with corejs: { version: 3 }, useBuiltIns: 'usage'. The problem is that polyfills are injected only to the userland code, not regenerator/runtime. But regenerator uses other features that should be polyfilled too.

The actual core-js features work with "@@iterator" string like with Symbol.iterator, but not with "@@asyncIterator" - in case of #13128, Babel async generators will not be able to work with core-js features like iterators helpers. More other, I planned to drop "@@iterator" fallback from core-js@4.

The proper solution here - if you load a polyfill with useBuiltIns: 'usage' and required to inject regenerator/runtime, inject all polyfills required for regenerator/runtime.

@nicolo-ribaudo
Copy link
Member Author

nicolo-ribaudo commented Apr 9, 2021

The goal of this PR is to allow using generators without a polyfill, similarly to how array spread or array for-of works without polyfills.

More other, I planned to drop "@@iterator" fallback from core-js@4.

Wouldn't that make it incompatible with some Firefox versions, that implemented the iterator protocol before implementing symbol support?

@zloirock
Copy link
Member

zloirock commented Apr 9, 2021

The goal of this PR is to allow using generators without a polyfill, similarly to how array spread or array for-of works without polyfills.

However, #13128 uses polyfills.

Wouldn't that make it incompatible with some Firefox versions, that implemented the iterator protocol before implementing symbol support?

Nobody bothers to implement iterators from scratch. It will not work with for-of syntax in critically old FF versions, but anyway it should be transpiled. About collections iterators - for them, it's just a key that can be redefined.

@nicolo-ribaudo
Copy link
Member Author

I'm merging this since:

  • It solves the case without polyfills (and Babel helpers should be compatible with each other even without loading a polyfill)
  • It currently solves the case with a polyfill in most cases

As @zloirock noted the fix isn't complete, and we'll need to polyfill symbols when using generators.

@nicolo-ribaudo nicolo-ribaudo merged commit 808d437 into babel:main Apr 15, 2021
@nicolo-ribaudo nicolo-ribaudo deleted the regenerator-iterate-old branch April 15, 2021 21:47
@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 Jul 16, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 16, 2021
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.

for-of with generator fails in ie11
6 participants