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
Allow rest/spread on polyfilled or builtin iterables without Symbol
support
#11268
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -902,49 +902,72 @@ helpers.temporalRef = helper("7.0.0-beta.0")` | |
helpers.slicedToArray = helper("7.0.0-beta.0")` | ||
import arrayWithHoles from "arrayWithHoles"; | ||
import iterableToArrayLimit from "iterableToArrayLimit"; | ||
import unsupportedIterableToArray from "unsupportedIterableToArray"; | ||
import nonIterableRest from "nonIterableRest"; | ||
|
||
export default function _slicedToArray(arr, i) { | ||
return arrayWithHoles(arr) || iterableToArrayLimit(arr, i) || nonIterableRest(); | ||
return ( | ||
arrayWithHoles(arr) || | ||
iterableToArrayLimit(arr, i) || | ||
unsupportedIterableToArray(arr, i) || | ||
nonIterableRest() | ||
); | ||
} | ||
`; | ||
|
||
helpers.slicedToArrayLoose = helper("7.0.0-beta.0")` | ||
import arrayWithHoles from "arrayWithHoles"; | ||
import iterableToArrayLimitLoose from "iterableToArrayLimitLoose"; | ||
import unsupportedIterableToArray from "unsupportedIterableToArray"; | ||
import nonIterableRest from "nonIterableRest"; | ||
|
||
export default function _slicedToArrayLoose(arr, i) { | ||
return arrayWithHoles(arr) || iterableToArrayLimitLoose(arr, i) || nonIterableRest(); | ||
return ( | ||
arrayWithHoles(arr) || | ||
iterableToArrayLimitLoose(arr, i) || | ||
unsupportedIterableToArray(arr, i) || | ||
nonIterableRest() | ||
); | ||
} | ||
`; | ||
|
||
helpers.toArray = helper("7.0.0-beta.0")` | ||
import arrayWithHoles from "arrayWithHoles"; | ||
import iterableToArray from "iterableToArray"; | ||
import unsupportedIterableToArray from "unsupportedIterableToArray"; | ||
import nonIterableRest from "nonIterableRest"; | ||
|
||
export default function _toArray(arr) { | ||
return arrayWithHoles(arr) || iterableToArray(arr) || nonIterableRest(); | ||
return ( | ||
arrayWithHoles(arr) || | ||
iterableToArray(arr) || | ||
unsupportedIterableToArray(arr) || | ||
nonIterableRest() | ||
); | ||
} | ||
`; | ||
|
||
helpers.toConsumableArray = helper("7.0.0-beta.0")` | ||
import arrayWithoutHoles from "arrayWithoutHoles"; | ||
import iterableToArray from "iterableToArray"; | ||
import unsupportedIterableToArray from "unsupportedIterableToArray"; | ||
import nonIterableSpread from "nonIterableSpread"; | ||
|
||
export default function _toConsumableArray(arr) { | ||
return arrayWithoutHoles(arr) || iterableToArray(arr) || nonIterableSpread(); | ||
return ( | ||
arrayWithoutHoles(arr) || | ||
iterableToArray(arr) || | ||
unsupportedIterableToArray(arr) || | ||
nonIterableSpread() | ||
); | ||
} | ||
`; | ||
|
||
helpers.arrayWithoutHoles = helper("7.0.0-beta.0")` | ||
import arrayLikeToArray from "arrayLikeToArray"; | ||
|
||
export default function _arrayWithoutHoles(arr) { | ||
if (Array.isArray(arr)) { | ||
for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; | ||
return arr2; | ||
} | ||
if (Array.isArray(arr)) return arrayLikeToArray(arr); | ||
} | ||
`; | ||
|
||
|
@@ -956,11 +979,7 @@ helpers.arrayWithHoles = helper("7.0.0-beta.0")` | |
|
||
helpers.iterableToArray = helper("7.0.0-beta.0")` | ||
export default function _iterableToArray(iter) { | ||
if ( | ||
typeof iter === 'string' | ||
|| Object.prototype.toString.call(iter) === "[object Arguments]" | ||
|| (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) | ||
) return Array.from(iter); | ||
if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); | ||
} | ||
`; | ||
|
||
|
@@ -976,10 +995,7 @@ helpers.iterableToArrayLimit = helper("7.0.0-beta.0")` | |
// _i = _iterator | ||
// _s = _step | ||
|
||
if ( | ||
(typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) && | ||
Object.prototype.toString.call(arr) !== "[object Arguments]" | ||
) return; | ||
if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; | ||
|
||
var _arr = []; | ||
var _n = true; | ||
|
@@ -1006,10 +1022,7 @@ helpers.iterableToArrayLimit = helper("7.0.0-beta.0")` | |
|
||
helpers.iterableToArrayLimitLoose = helper("7.0.0-beta.0")` | ||
export default function _iterableToArrayLimitLoose(arr, i) { | ||
if ( | ||
(typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) && | ||
Object.prototype.toString.call(arr) !== "[object Arguments]" | ||
) return; | ||
if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; | ||
|
||
var _arr = []; | ||
for (var _iterator = arr[Symbol.iterator](), _step; !(_step = _iterator.next()).done;) { | ||
|
@@ -1020,6 +1033,28 @@ helpers.iterableToArrayLimitLoose = helper("7.0.0-beta.0")` | |
} | ||
`; | ||
|
||
helpers.unsupportedIterableToArray = helper("7.9.0")` | ||
import arrayLikeToArray from "arrayLikeToArray"; | ||
|
||
export default function _unsupportedIterableToArray(o, minLen) { | ||
if (!o) return; | ||
if (typeof o === "string") return arrayLikeToArray(o, minLen); | ||
var n = Object.prototype.toString.call(o).slice(8, -1); | ||
if (n === "Object" && o.constructor) n = o.constructor.name; | ||
if (n === "Map" || n === "Set") return Array.from(n); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @nicolo-ribaudo This should really be if (n === "Map" || n === "Set") return Array.from(o); because |
||
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) | ||
return arrayLikeToArray(o, minLen); | ||
} | ||
`; | ||
|
||
helpers.arrayLikeToArray = helper("7.9.0")` | ||
export default function _arrayLikeToArray(arr, len) { | ||
if (len == null || len > arr.length) len = arr.length; | ||
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; | ||
return arr2; | ||
} | ||
`; | ||
|
||
helpers.nonIterableSpread = helper("7.0.0-beta.0")` | ||
export default function _nonIterableSpread() { | ||
throw new TypeError( | ||
|
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
8 changes: 6 additions & 2 deletions
8
...ages/babel-preset-env/test/fixtures/preset-options/browserslist-defaults-not-ie/output.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
8 changes: 6 additions & 2 deletions
8
packages/babel-preset-env/test/fixtures/sanity/block-scoping-for-of/output.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import arrayWithHoles from "./arrayWithHoles"; | ||
import iterableToArray from "./iterableToArray"; | ||
import unsupportedIterableToArray from "./unsupportedIterableToArray"; | ||
import nonIterableRest from "./nonIterableRest"; | ||
export default function _toArray(arr) { | ||
return arrayWithHoles(arr) || iterableToArray(arr) || nonIterableRest(); | ||
return arrayWithHoles(arr) || iterableToArray(arr) || unsupportedIterableToArray(arr) || nonIterableRest(); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export default function _iterableToArray(iter) { | ||
if (typeof iter === 'string' || Object.prototype.toString.call(iter) === "[object Arguments]" || typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); | ||
if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import arrayWithHoles from "./arrayWithHoles"; | ||
import iterableToArray from "./iterableToArray"; | ||
import unsupportedIterableToArray from "./unsupportedIterableToArray"; | ||
import nonIterableRest from "./nonIterableRest"; | ||
export default function _toArray(arr) { | ||
return arrayWithHoles(arr) || iterableToArray(arr) || nonIterableRest(); | ||
return arrayWithHoles(arr) || iterableToArray(arr) || unsupportedIterableToArray(arr) || nonIterableRest(); | ||
} |
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,5 +1,5 @@ | ||
function _iterableToArray(iter) { | ||
if (typeof iter === 'string' || Object.prototype.toString.call(iter) === "[object Arguments]" || typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); | ||
if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); | ||
} | ||
|
||
module.exports = _iterableToArray; |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should work with
es6-shim
. As an alternative, we could detect the presence of a_es6-shim iterator_
property.I'm thinking specifically about
es6-shim
because it's the polyfill with most downloads on npm aftercore-js
, andcore-js
's future is uncertain.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By using
Array.from
, it already works with es6-shim without having to hardcode anything.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Array.from
is not supported on IE11. Can we usearrayLikeToArray
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem is that Maps and Sets are not array-like.
I decided to use
Array.from
because because it is way more easily polyfilled than symbols.WDYT about using
arrayLikeToArray
for strings/arguments/binary arrays, andArray.from
forMap
/Set
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's included with es6-shim tho, and imo it's completely reasonable to require anything faithfully polyfillable/shimmable, but symbols are not that.
@nicolo-ribaudo's suggestion works for me as well.