Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support iterating generators in browsers without
Symbol
(#13129)
- Loading branch information
1 parent
5d55055
commit 808d437
Showing
15 changed files
with
124 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
packages/babel-plugin-transform-regenerator/test/fixtures/integration/array-rest/exec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
let iterable = function* fn() { | ||
yield 0; | ||
yield 1; | ||
yield 2; | ||
}(); | ||
|
||
let iteratorMethod = iterable[Symbol.iterator]; | ||
iterable[Symbol.iterator] = undefined; | ||
|
||
expect(() => { | ||
let [_, ...res] = iterable; | ||
}).toThrow(/non-iterable/); | ||
|
||
// In older envs, regenerator uses @@iterator. | ||
// In those browser Babel must delegate to Array.from, which | ||
// needs to be polyfilled by the user and support @@iterator. | ||
iterable["@@iterator"] = iteratorMethod; | ||
Array.from = (it) => { | ||
var i = it["@@iterator"](), arr = [], r; | ||
while (!(r = it.next()).done) arr.push(r.value); | ||
return arr; | ||
}; | ||
|
||
let [_, ...res] = iterable; | ||
expect(res).toEqual([1, 2]); |
3 changes: 3 additions & 0 deletions
3
...ages/babel-plugin-transform-regenerator/test/fixtures/integration/array-rest/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"plugins": ["transform-regenerator", "transform-destructuring"] | ||
} |
25 changes: 25 additions & 0 deletions
25
packages/babel-plugin-transform-regenerator/test/fixtures/integration/array-spread/exec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
let iterable = function* fn() { | ||
yield 1; | ||
yield 2; | ||
}(); | ||
|
||
let iteratorMethod = iterable[Symbol.iterator]; | ||
iterable[Symbol.iterator] = undefined; | ||
|
||
expect(() => { | ||
[...iterable]; | ||
}).toThrow(/non-iterable/); | ||
|
||
// In older envs, regenerator uses @@iterator. | ||
// In those browser Babel must delegate to Array.from, which | ||
// needs to be polyfilled by the user and support @@iterator. | ||
iterable["@@iterator"] = iteratorMethod; | ||
Array.from = (it) => { | ||
var i = it["@@iterator"](), arr = [], r; | ||
while (!(r = it.next()).done) arr.push(r.value); | ||
return arr; | ||
}; | ||
|
||
let res = [...iterable]; | ||
|
||
expect(res).toEqual([1, 2]); |
3 changes: 3 additions & 0 deletions
3
...es/babel-plugin-transform-regenerator/test/fixtures/integration/array-spread/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"plugins": ["transform-regenerator", "transform-spread"] | ||
} |
19 changes: 19 additions & 0 deletions
19
packages/babel-plugin-transform-regenerator/test/fixtures/integration/for-of/exec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
let iterable = function* fn() { | ||
yield 1; | ||
yield 2; | ||
}(); | ||
|
||
let iteratorMethod = iterable[Symbol.iterator]; | ||
iterable[Symbol.iterator] = undefined; | ||
|
||
expect(() => { | ||
for (let x of iterable); | ||
}).toThrow(/non-iterable/); | ||
|
||
// In older envs, regenerator uses @@iterator. | ||
iterable["@@iterator"] = iteratorMethod; | ||
|
||
let res = []; | ||
for (let x of iterable) res.push(x); | ||
|
||
expect(res).toEqual([1, 2]); |
3 changes: 3 additions & 0 deletions
3
packages/babel-plugin-transform-regenerator/test/fixtures/integration/for-of/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"plugins": ["transform-regenerator", "transform-for-of"] | ||
} |
2 changes: 1 addition & 1 deletion
2
packages/babel-plugin-transform-spread/test/fixtures/spread/non-iterable/exec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
packages/babel-runtime-corejs2/helpers/esm/iterableToArray.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import _Symbol from "@babel/runtime-corejs2/core-js/symbol"; | ||
import _isIterable from "@babel/runtime-corejs2/core-js/is-iterable"; | ||
import _Symbol$iterator from "@babel/runtime-corejs2/core-js/symbol/iterator"; | ||
import _Array$from from "@babel/runtime-corejs2/core-js/array/from"; | ||
export default function _iterableToArray(iter) { | ||
if (typeof _Symbol !== "undefined" && _isIterable(Object(iter))) return _Array$from(iter); | ||
if (typeof _Symbol !== "undefined" && iter[_Symbol$iterator] != null || iter["@@iterator"] != null) return _Array$from(iter); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export default function _iterableToArray(iter) { | ||
if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); | ||
if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters