|
1 |
| -/*eslint no-empty: 0*/ |
2 | 1 | import warning from './routerWarning'
|
3 | 2 |
|
4 |
| -let useMembrane = false |
| 3 | +// No-op by default. |
| 4 | +let deprecateObjectProperties = object => object |
5 | 5 |
|
6 | 6 | if (__DEV__) {
|
| 7 | + let useMembrane = false |
| 8 | + |
7 | 9 | try {
|
8 | 10 | if (Object.defineProperty({}, 'x', { get() { return true } }).x) {
|
9 | 11 | useMembrane = true
|
10 | 12 | }
|
11 |
| - } catch(e) { } |
12 |
| -} |
| 13 | + /* eslint-disable no-empty */ |
| 14 | + } catch(e) {} |
| 15 | + /* eslint-enable no-empty */ |
13 | 16 |
|
14 |
| -// wraps an object in a membrane to warn about deprecated property access |
15 |
| -export default function deprecateObjectProperties(object, message) { |
16 |
| - if (!useMembrane) |
17 |
| - return object |
| 17 | + if (useMembrane) { |
| 18 | + deprecateObjectProperties = (object, message) => { |
| 19 | + // Wrap the deprecated object in a membrane to warn on property access. |
| 20 | + const membrane = {} |
18 | 21 |
|
19 |
| - const membrane = {} |
| 22 | + for (const prop in object) { |
| 23 | + if (!Object.prototype.hasOwnProperty.call(object, prop)) { |
| 24 | + continue |
| 25 | + } |
20 | 26 |
|
21 |
| - for (let prop in object) { |
22 |
| - if (typeof object[prop] === 'function') { |
23 |
| - membrane[prop] = function () { |
24 |
| - warning(false, message) |
25 |
| - return object[prop].apply(object, arguments) |
26 |
| - } |
27 |
| - } else { |
28 |
| - Object.defineProperty(membrane, prop, { |
29 |
| - configurable: false, |
30 |
| - enumerable: false, |
31 |
| - get() { |
32 |
| - warning(false, message) |
33 |
| - return object[prop] |
| 27 | + if (typeof object[prop] === 'function') { |
| 28 | + // Can't use fat arrow here because of use of arguments below. |
| 29 | + membrane[prop] = function () { |
| 30 | + warning(false, message) |
| 31 | + return object[prop].apply(object, arguments) |
| 32 | + } |
| 33 | + continue |
34 | 34 | }
|
35 |
| - }) |
| 35 | + |
| 36 | + // These properties are non-enumerable to prevent React dev tools from |
| 37 | + // seeing them and causing spurious warnings when accessing them. In |
| 38 | + // principle this could be done with a proxy, but support for the |
| 39 | + // ownKeys trap on proxies is not universal, even among browsers that |
| 40 | + // otherwise support proxies. |
| 41 | + Object.defineProperty(membrane, prop, { |
| 42 | + configurable: false, |
| 43 | + enumerable: false, |
| 44 | + get() { |
| 45 | + warning(false, message) |
| 46 | + return object[prop] |
| 47 | + } |
| 48 | + }) |
| 49 | + } |
| 50 | + |
| 51 | + return membrane |
36 | 52 | }
|
37 | 53 | }
|
38 |
| - |
39 |
| - return membrane |
40 | 54 | }
|
41 | 55 |
|
| 56 | +export default deprecateObjectProperties |
0 commit comments