-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
When using babel-runtime, Symbol.iterator is different in user code & in polyfills #2500
Copy link
Copy link
Closed
Labels
outdatedA closed issue/PR that is archived due to age. Recommended to make a new issueA closed issue/PR that is archived due to age. Recommended to make a new issue
Description
The following code:
const o = {0: 'a', 1: 'b', 2: 'c', length: 3};
o[Symbol.iterator] = Array.prototype[Symbol.iterator];
let counter = 0;
for (const div of o) {
counter++;
}
console.log('counter', 3);
console.assert(counter === 3, 'Iterability doesn\'t work!');is transformed by:
babel --optional runtime iterable-test.js > iterable-test-transformed.js
to:
'use strict';
var _Symbol$iterator = require('babel-runtime/core-js/symbol/iterator')['default'];
var _getIterator = require('babel-runtime/core-js/get-iterator')['default'];
var o = { 0: 'a', 1: 'b', 2: 'c', length: 3 };
o[_Symbol$iterator] = Array.prototype[_Symbol$iterator];
var counter = 0;
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = _getIterator(o), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var div = _step.value;
counter++;
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator['return']) {
_iterator['return']();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
console.log('counter', 3);
console.assert(counter === 3, 'Iterability doesn\'t work!');After invoking:
npm i babel-runtime
this transformed script prints 3 correctly in Node 4.1.2 & 0.12.7 but in 0.10.40 it throws:
/Users/mgol/_/iterable-test-transformed.js:31
throw _iteratorError;
^
TypeError: [object Object] is not iterable!
at TypeError (<anonymous>)
at module.exports.require.getIterator (/Users/mgol/_/node_modules/core-js/library/modules/core.get-iterator.js:5:40)
at Object.<anonymous> (/Users/mgol/_/iterable-test-transformed.js:16:23)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:935:3
If you don't use babel-runtime but include core-js in a separate script, it works fine.
babel-runtime 5.8.25.
@zloirock presents a solution to the problem in his comment under the core-js issue.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
outdatedA closed issue/PR that is archived due to age. Recommended to make a new issueA closed issue/PR that is archived due to age. Recommended to make a new issue