Skip to content

Support iterating generators in browsers without Symbol#13129

Merged
nicolo-ribaudo merged 5 commits intobabel:mainfrom
nicolo-ribaudo:regenerator-iterate-old
Apr 15, 2021
Merged

Support iterating generators in browsers without Symbol#13129
nicolo-ribaudo merged 5 commits intobabel:mainfrom
nicolo-ribaudo:regenerator-iterate-old

Conversation

@nicolo-ribaudo
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown

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
Copy Markdown

That was fast 🤩
You rock

@zloirock
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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 nicolo-ribaudo force-pushed the regenerator-iterate-old branch from b76c3ec to 46e7748 Compare April 15, 2021 19:54
@nicolo-ribaudo
Copy link
Copy Markdown
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