Skip to content

Commit 758dd0d

Browse files
author
Amjad Masad
committed
[react-packager] Add Array.prototype.es6 polyfill
1 parent f7aeefa commit 758dd0d

3 files changed

Lines changed: 145 additions & 1 deletion

File tree

packager/react-packager/src/DependencyResolver/haste/__tests__/HasteDependencyResolver-test.js

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,18 @@ describe('HasteDependencyResolver', function() {
9393
'polyfills/error-guard.js'
9494
],
9595
},
96+
{ id: 'polyfills/Array.prototype.es6.js',
97+
isPolyfill: true,
98+
path: 'polyfills/Array.prototype.es6.js',
99+
dependencies: [
100+
'polyfills/prelude.js',
101+
'polyfills/require.js',
102+
'polyfills/polyfills.js',
103+
'polyfills/console.js',
104+
'polyfills/error-guard.js',
105+
'polyfills/String.prototype.es6.js',
106+
],
107+
},
96108
module
97109
]);
98110
});
@@ -164,6 +176,18 @@ describe('HasteDependencyResolver', function() {
164176
'polyfills/error-guard.js'
165177
],
166178
},
179+
{ id: 'polyfills/Array.prototype.es6.js',
180+
isPolyfill: true,
181+
path: 'polyfills/Array.prototype.es6.js',
182+
dependencies: [
183+
'polyfills/prelude_dev.js',
184+
'polyfills/require.js',
185+
'polyfills/polyfills.js',
186+
'polyfills/console.js',
187+
'polyfills/error-guard.js',
188+
'polyfills/String.prototype.es6.js'
189+
],
190+
},
167191
module
168192
]);
169193
});
@@ -236,6 +260,18 @@ describe('HasteDependencyResolver', function() {
236260
'polyfills/error-guard.js'
237261
],
238262
},
263+
{ id: 'polyfills/Array.prototype.es6.js',
264+
isPolyfill: true,
265+
path: 'polyfills/Array.prototype.es6.js',
266+
dependencies: [
267+
'polyfills/prelude.js',
268+
'polyfills/require.js',
269+
'polyfills/polyfills.js',
270+
'polyfills/console.js',
271+
'polyfills/error-guard.js',
272+
'polyfills/String.prototype.es6.js',
273+
],
274+
},
239275
{ path: 'some module',
240276
id: 'some module',
241277
isPolyfill: true,
@@ -245,7 +281,8 @@ describe('HasteDependencyResolver', function() {
245281
'polyfills/polyfills.js',
246282
'polyfills/console.js',
247283
'polyfills/error-guard.js',
248-
'polyfills/String.prototype.es6.js'
284+
'polyfills/String.prototype.es6.js',
285+
'polyfills/Array.prototype.es6.js'
249286
]
250287
},
251288
module

packager/react-packager/src/DependencyResolver/haste/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ HasteDependencyResolver.prototype._prependPolyfillDependencies = function(
113113
path.join(__dirname, 'polyfills/console.js'),
114114
path.join(__dirname, 'polyfills/error-guard.js'),
115115
path.join(__dirname, 'polyfills/String.prototype.es6.js'),
116+
path.join(__dirname, 'polyfills/Array.prototype.es6.js'),
116117
].concat(this._polyfillModuleNames);
117118

118119
var polyfillModules = polyfillModuleNames.map(
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/**
2+
* Copyright 2004-present Facebook. All Rights Reserved.
3+
*
4+
* @provides Array.prototype.es6
5+
* @polyfill
6+
* @requires __DEV__
7+
*/
8+
9+
/*eslint-disable */
10+
/*jslint bitwise: true */
11+
12+
(function (undefined) {
13+
if (__DEV__) {
14+
// Define DEV-only setter that blows up when someone incorrectly
15+
// iterates over arrays.
16+
try {
17+
Object.defineProperty && Object.defineProperty(
18+
Array.prototype,
19+
'__ARRAY_ENUMERATION_GUARD__',
20+
{
21+
configurable: true,
22+
enumerable: true,
23+
get: function() {
24+
console.error(
25+
'Your code is broken! Do not iterate over arrays with ' +
26+
'for...in. See https://fburl.com/31944000 for more information.'
27+
);
28+
}
29+
}
30+
);
31+
} catch (e) {
32+
// Nothing
33+
}
34+
}
35+
36+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex
37+
function findIndex(predicate, context) {
38+
/**
39+
* Why am I seeing this "findIndex" method as a value in my array!?
40+
*
41+
* We polyfill the "findIndex" method -- called like
42+
* `[1, 2, 3].findIndex(1)` -- for older browsers. A side effect of the way
43+
* we do that is that the method is enumerable. If you were incorrectly
44+
* iterating over your array using the object property iterator syntax
45+
* `for (key in obj)` you will see the method name "findIndex" as a key.
46+
*
47+
* To fix your code please do one of the following:
48+
*
49+
* - Use a regular for loop with index.
50+
* - Use one of the array methods: a.forEach, a.map, etc.
51+
* - Guard your body of your loop with a `arr.hasOwnProperty(key)` check.
52+
*
53+
* More info:
54+
* https://our.intern.facebook.com/intern/dex/qa/669736606441771/
55+
*/
56+
if (this == null) {
57+
throw new TypeError(
58+
'Array.prototype.findIndex called on null or undefined'
59+
);
60+
}
61+
if (typeof predicate !== 'function') {
62+
throw new TypeError('predicate must be a function');
63+
}
64+
var list = Object(this);
65+
var length = list.length >>> 0;
66+
for (var i = 0; i < length; i++) {
67+
if (predicate.call(context, list[i], i, list)) {
68+
return i;
69+
}
70+
}
71+
return -1;
72+
}
73+
74+
if (!Array.prototype.findIndex) {
75+
Array.prototype.findIndex = findIndex;
76+
}
77+
78+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
79+
if (!Array.prototype.find) {
80+
Array.prototype.find = function(predicate, context) {
81+
/**
82+
* Why am I seeing this "find" method as a value in my array!?
83+
*
84+
* We polyfill the "find" method -- called like
85+
* `[1, 2, 3].find(1)` -- for older browsers. A side effect of the way
86+
* we do that is that the method is enumerable. If you were incorrectly
87+
* iterating over your array using the object property iterator syntax
88+
* `for (key in obj)` you will see the method name "find" as a key.
89+
*
90+
* To fix your code please do one of the following:
91+
*
92+
* - Use a regular for loop with index.
93+
* - Use one of the array methods: a.forEach, a.map, etc.
94+
* - Guard your body of your loop with a `arr.hasOwnProperty(key)` check.
95+
*
96+
* More info:
97+
* https://our.intern.facebook.com/intern/dex/qa/669736606441771/
98+
*/
99+
if (this == null) {
100+
throw new TypeError('Array.prototype.find called on null or undefined');
101+
}
102+
var index = findIndex.call(this, predicate, context);
103+
return index === -1 ? undefined : this[index];
104+
};
105+
}
106+
})();

0 commit comments

Comments
 (0)