@samitouri / QOS-React-2 / commits / 5b98656909

fix: use define property to overwrite console, close #19099 (#19123)

* fix: use define property to overwrite console, close #19099 * fix: lint error

Jack Works committed Jun 23, 2020 at 22:34 UTC 5b98656909d46fff6da135a8e60ffdc529e432f5
1 file changed +24 -8
packages/shared/ConsolePatchingDev.js
+24 -8
@@ -29,8 +29,20 @@ export function disableLogs(): void {
29 prevInfo = console.info;
30 prevWarn = console.warn;
31 prevError = console.error;
32 + // https://github.com/facebook/react/issues/19099
33 + const props = {
34 + configurable: true,
35 + enumerable: true,
36 + value: disabledLog,
37 + writable: true,
38 + };
39 // $FlowFixMe Flow thinks console is immutable.
33 - console.log = console.info = console.warn = console.error = disabledLog;
40 + Object.defineProperties(console, {
41 + info: props,
42 + log: props,
43 + warn: props,
44 + error: props,
45 + });
46 /* eslint-enable react-internal/no-production-logging */
47 }
48 disabledDepth++;
@@ -42,14 +54,18 @@ export function reenableLogs(): void {
54 disabledDepth--;
55 if (disabledDepth === 0) {
56 /* eslint-disable react-internal/no-production-logging */
57 + const props = {
58 + configurable: true,
59 + enumerable: true,
60 + writable: true,
61 + };
62 // $FlowFixMe Flow thinks console is immutable.
46 - console.log = prevLog;
47 - // $FlowFixMe Flow thinks console is immutable.
48 - console.info = prevInfo;
49 - // $FlowFixMe Flow thinks console is immutable.
50 - console.warn = prevWarn;
51 - // $FlowFixMe Flow thinks console is immutable.
52 - console.error = prevError;
63 + Object.defineProperties(console, {
64 + log: {...props, value: prevLog},
65 + info: {...props, value: prevInfo},
66 + warn: {...props, value: prevWarn},
67 + error: {...props, value: prevError},
68 + });
69 /* eslint-enable react-internal/no-production-logging */
70 }
71 if (disabledDepth < 0) {