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