Skip to content
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 Array.from polyfill: 2nd arg of mapFn is missing #2911

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/com/google/javascript/jscomp/js/es6/array/from.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ $jscomp.polyfill('Array.from', function(orig) {
*
* @param {!IArrayLike<INPUT>|!Iterable<INPUT>} arrayLike
* An array-like or iterable.
* @param {(function(this: THIS, INPUT): OUTPUT)=} opt_mapFn
* @param {(function(this: THIS, INPUT, number): OUTPUT)=} opt_mapFn
* Function to call on each argument.
* @param {THIS=} opt_thisArg
* Object to use as 'this' when calling mapFn.
Expand All @@ -47,15 +47,16 @@ $jscomp.polyfill('Array.from', function(orig) {
if (typeof iteratorFunction == 'function') {
arrayLike = iteratorFunction.call(arrayLike);
var next;
var k = 0;
while (!(next = arrayLike.next()).done) {
result.push(
opt_mapFn.call(/** @type {?} */ (opt_thisArg), next.value));
opt_mapFn.call(/** @type {?} */ (opt_thisArg), next.value, k++));
}
} else {
var len = arrayLike.length; // need to support non-iterables
for (var i = 0; i < len; i++) {
result.push(
opt_mapFn.call(/** @type {?} */ (opt_thisArg), arrayLike[i]));
opt_mapFn.call(/** @type {?} */ (opt_thisArg), arrayLike[i], i));
}
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,16 @@ testSuite({
})('x', 'y');

assertObjectEquals(['x', 'y'], Array.from(iterable('x', 'y')));
},

const x2 = x => x + x;
assertObjectEquals([2, 4, 8], Array.from([1, 2, 4], x2));
assertObjectEquals(['aa', 'cc', 'bb'], Array.from(noCheck('acb'), x2));
assertObjectEquals([6], Array.from({length: 1, 0: 3}, x2));
assertObjectEquals([6, 'xx'], Array.from(iterable(3, 'x'), x2));
testMapFn() {
const id = (x, i) => [x, i];
assertObjectEquals([[1, 0], [2, 1], [4, 2]], Array.from([1, 2, 4], id));
assertObjectEquals([['a', 0], ['c', 1], ['b', 2]], Array.from(noCheck('acb'), id));
assertObjectEquals([[3, 0], [undefined, 1]], Array.from({length: 2, 0: 3}, id));
assertObjectEquals([[3, 0], ['x', 1]], Array.from(iterable(3, 'x'), id));

const x2 = x => x + x;
/**
* @this {!Function}
* @param {?} x
Expand Down