Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore[react-devtools/backend]: remove consoleManagedByDevToolsDuringStrictMode #29856

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 53 additions & 58 deletions packages/react-devtools-shared/src/backend/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import {
getStackByFiberInDevAndProd,
supportsNativeConsoleTasks,
} from './DevToolsFiberComponentStack';
import {consoleManagedByDevToolsDuringStrictMode} from 'react-devtools-feature-flags';
import {castBool, castBrowserTheme} from '../utils';

const OVERRIDE_CONSOLE_METHODS = ['error', 'trace', 'warn'];
Expand Down Expand Up @@ -302,76 +301,72 @@ let unpatchForStrictModeFn: null | (() => void) = null;

// NOTE: KEEP IN SYNC with src/hook.js:patchConsoleForInitialCommitInStrictMode
export function patchForStrictMode() {
if (consoleManagedByDevToolsDuringStrictMode) {
const overrideConsoleMethods = [
'error',
'group',
'groupCollapsed',
'info',
'log',
'trace',
'warn',
];

if (unpatchForStrictModeFn !== null) {
// Don't patch twice.
return;
}
const overrideConsoleMethods = [
'error',
'group',
'groupCollapsed',
'info',
'log',
'trace',
'warn',
];

if (unpatchForStrictModeFn !== null) {
// Don't patch twice.
return;
}

const originalConsoleMethods: {[string]: $FlowFixMe} = {};
const originalConsoleMethods: {[string]: $FlowFixMe} = {};

unpatchForStrictModeFn = () => {
for (const method in originalConsoleMethods) {
try {
targetConsole[method] = originalConsoleMethods[method];
} catch (error) {}
}
};

overrideConsoleMethods.forEach(method => {
unpatchForStrictModeFn = () => {
for (const method in originalConsoleMethods) {
try {
const originalMethod = (originalConsoleMethods[method] = targetConsole[
method
].__REACT_DEVTOOLS_STRICT_MODE_ORIGINAL_METHOD__
? targetConsole[method].__REACT_DEVTOOLS_STRICT_MODE_ORIGINAL_METHOD__
: targetConsole[method]);
targetConsole[method] = originalConsoleMethods[method];
} catch (error) {}
}
};

// $FlowFixMe[missing-local-annot]
const overrideMethod = (...args) => {
if (!consoleSettingsRef.hideConsoleLogsInStrictMode) {
// Dim the text color of the double logs if we're not
// hiding them.
if (isNode) {
originalMethod(DIMMED_NODE_CONSOLE_COLOR, format(...args));
overrideConsoleMethods.forEach(method => {
try {
const originalMethod = (originalConsoleMethods[method] = targetConsole[
method
].__REACT_DEVTOOLS_STRICT_MODE_ORIGINAL_METHOD__
? targetConsole[method].__REACT_DEVTOOLS_STRICT_MODE_ORIGINAL_METHOD__
: targetConsole[method]);

// $FlowFixMe[missing-local-annot]
const overrideMethod = (...args) => {
if (!consoleSettingsRef.hideConsoleLogsInStrictMode) {
// Dim the text color of the double logs if we're not
// hiding them.
if (isNode) {
originalMethod(DIMMED_NODE_CONSOLE_COLOR, format(...args));
} else {
const color = getConsoleColor(method);
if (color) {
originalMethod(...formatWithStyles(args, `color: ${color}`));
} else {
const color = getConsoleColor(method);
if (color) {
originalMethod(...formatWithStyles(args, `color: ${color}`));
} else {
throw Error('Console color is not defined');
}
throw Error('Console color is not defined');
}
}
};
}
};

overrideMethod.__REACT_DEVTOOLS_STRICT_MODE_ORIGINAL_METHOD__ =
originalMethod;
originalMethod.__REACT_DEVTOOLS_STRICT_MODE_OVERRIDE_METHOD__ =
overrideMethod;
overrideMethod.__REACT_DEVTOOLS_STRICT_MODE_ORIGINAL_METHOD__ =
originalMethod;
originalMethod.__REACT_DEVTOOLS_STRICT_MODE_OVERRIDE_METHOD__ =
overrideMethod;

targetConsole[method] = overrideMethod;
} catch (error) {}
});
}
targetConsole[method] = overrideMethod;
} catch (error) {}
});
}

// NOTE: KEEP IN SYNC with src/hook.js:unpatchConsoleForInitialCommitInStrictMode
export function unpatchForStrictMode(): void {
if (consoleManagedByDevToolsDuringStrictMode) {
if (unpatchForStrictModeFn !== null) {
unpatchForStrictModeFn();
unpatchForStrictModeFn = null;
}
if (unpatchForStrictModeFn !== null) {
unpatchForStrictModeFn();
unpatchForStrictModeFn = null;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
* It should always be imported from "react-devtools-feature-flags".
************************************************************************/

export const consoleManagedByDevToolsDuringStrictMode = false;
export const enableLogger = true;
export const enableStyleXFeatures = true;
export const isInternalFacebookBuild = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
* It should always be imported from "react-devtools-feature-flags".
************************************************************************/

export const consoleManagedByDevToolsDuringStrictMode = false;
export const enableLogger = false;
export const enableStyleXFeatures = false;
export const isInternalFacebookBuild = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
* It should always be imported from "react-devtools-feature-flags".
************************************************************************/

export const consoleManagedByDevToolsDuringStrictMode = true;
export const enableLogger = false;
export const enableStyleXFeatures = false;
export const isInternalFacebookBuild = false;
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
* It should always be imported from "react-devtools-feature-flags".
************************************************************************/

export const consoleManagedByDevToolsDuringStrictMode = true;
export const enableLogger = true;
export const enableStyleXFeatures = true;
export const isInternalFacebookBuild = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
* It should always be imported from "react-devtools-feature-flags".
************************************************************************/

export const consoleManagedByDevToolsDuringStrictMode = true;
export const enableLogger = false;
export const enableStyleXFeatures = false;
export const isInternalFacebookBuild = false;
Expand Down