Skip to content

Commit 00cf74e

Browse files
brad4dcopybara-github
authored andcommitted
for-of on a non-iterable, non-arraylike should throw an error.
The utility JS code used when transpiling a for-of loop down to ES5 checks for the `Symbol.iterator` property first, then falls back to iterating as if the thing to be iterated is an array-like-thing. This can end up hiding a `TypedError` that should have been thrown. Check to make sure the thing to iterate over has a `length` property before just treating it like an array when transpiling a for-of loop. If it doesn't, throw a `TypeError`. PiperOrigin-RevId: 509969328
1 parent 3241a65 commit 00cf74e

File tree

4 files changed

+305
-196
lines changed

4 files changed

+305
-196
lines changed

src/com/google/javascript/jscomp/js/es6/util/makeiterator.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ $jscomp.makeIterator = function(iterable) {
3232
// NOTE: Disabling typechecking because [] not allowed on @struct.
3333
var iteratorFunction = typeof Symbol != 'undefined' && Symbol.iterator &&
3434
(/** @type {?} */ (iterable)[Symbol.iterator]);
35-
return iteratorFunction ? iteratorFunction.call(iterable) :
36-
$jscomp.arrayIterator(/** @type {!Array} */ (iterable));
35+
if (iteratorFunction) {
36+
return iteratorFunction.call(iterable);
37+
}
38+
if (typeof iterable['length'] == 'number') {
39+
return $jscomp.arrayIterator(/** @type {!Array} */ (iterable));
40+
}
41+
throw new Error(String(iterable) + ' is not an iterable or ArrayLike');
3742
};

0 commit comments

Comments
 (0)