Skip to content

Commit 0c49ccc

Browse files
taiontimdorr
authored andcommitted
Do not include deprecation code in production builds (#3296)
1 parent 216477f commit 0c49ccc

File tree

1 file changed

+40
-25
lines changed

1 file changed

+40
-25
lines changed
Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,56 @@
1-
/*eslint no-empty: 0*/
21
import warning from './routerWarning'
32

4-
let useMembrane = false
3+
// No-op by default.
4+
let deprecateObjectProperties = object => object
55

66
if (__DEV__) {
7+
let useMembrane = false
8+
79
try {
810
if (Object.defineProperty({}, 'x', { get() { return true } }).x) {
911
useMembrane = true
1012
}
11-
} catch(e) { }
12-
}
13+
/* eslint-disable no-empty */
14+
} catch(e) {}
15+
/* eslint-enable no-empty */
1316

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 = {}
1821

19-
const membrane = {}
22+
for (const prop in object) {
23+
if (!Object.prototype.hasOwnProperty.call(object, prop)) {
24+
continue
25+
}
2026

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
3434
}
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
3652
}
3753
}
38-
39-
return membrane
4054
}
4155

56+
export default deprecateObjectProperties

0 commit comments

Comments
 (0)