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: early return when instance is not iterable #10396
Conversation
@@ -943,7 +943,7 @@ helpers.iterableToArrayLimit = helper("7.0.0-beta.0")` | |||
// _e = _iteratorError | |||
// _i = _iterator | |||
// _s = _step | |||
|
|||
if (!Symbol.iterator in Object(arr)) return; |
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 iterableToArray
has guarded this check with an arguments
check
export default function _iterableToArray(iter) {
if (
Symbol.iterator in Object(iter) ||
Object.prototype.toString.call(iter) === "[object Arguments]"
) return Array.from(iter);
}
But
(function () { return Symbol.iterator in Object(arguments) })()
returns true in Chrome 76. I am not sure if this behavior is inconsistent across different browsers.
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.
Oh, there is a browser support matrix on arguments[@@iterator]
: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments/@@iterator
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/11483/ |
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/11471/ |
Actually yes, we need to check for arguments
. This is because arguments
can't be polyfilled (becaue it is re-created every time), so Symbol.iterator in Object(arguments)
will always be false in old browsers, even with a polyfill.
Babel output changed. See babel/babel#10396
The
slicedToArray
is supposed to thrownonIterableRest
error when applied to an non-iterable instance. However, theiterableToArrayLimit
will always throw error inarr[Symbol.iterator]
whenarr
is not iterable.Here we early return in
iterableToArrayLimit
so that we can offer better error message in runtime. Note that by doing so we also aligniterableToArrayLimit
withiterableToArray
.