Skip to content

Commit 092f6fd

Browse files
committed
Replace console.warn/error with a custom wrapper at build time
1 parent 43d70d7 commit 092f6fd

31 files changed

+364
-341
lines changed

.eslintrc.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ module.exports = {
3333
'comma-dangle': [ERROR, 'always-multiline'],
3434
'consistent-return': OFF,
3535
'dot-location': [ERROR, 'property'],
36-
'dot-notation': ERROR,
36+
// We use console['error']() as a signal to not transform it:
37+
'dot-notation': [ERROR, {allowPattern: '^(error|warn)$'}],
3738
'eol-last': ERROR,
3839
eqeqeq: [ERROR, 'allow-null'],
3940
indent: OFF,
@@ -135,6 +136,16 @@ module.exports = {
135136
'jest/valid-expect-in-promise': ERROR,
136137
},
137138
},
139+
{
140+
files: [
141+
'**/__tests__/**/*.js',
142+
'scripts/**/*.js',
143+
'packages/react-devtools*/**/*.js'
144+
],
145+
rules: {
146+
'react-internal/no-production-logging': OFF,
147+
},
148+
},
138149
{
139150
files: ['packages/react-native-renderer/**/*.js'],
140151
globals: {

packages/legacy-events/ResponderEventPlugin.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -515,9 +515,11 @@ const ResponderEventPlugin = {
515515
if (trackedTouchCount >= 0) {
516516
trackedTouchCount -= 1;
517517
} else {
518-
console.warn(
519-
'Ended a touch event which was not counted in `trackedTouchCount`.',
520-
);
518+
if (__DEV__) {
519+
console.warn(
520+
'Ended a touch event which was not counted in `trackedTouchCount`.',
521+
);
522+
}
521523
return null;
522524
}
523525
}

packages/legacy-events/ResponderTouchHistoryStore.js

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,15 @@ function recordTouchMove(touch: Touch): void {
129129
touchRecord.currentTimeStamp = timestampForTouch(touch);
130130
touchHistory.mostRecentTimeStamp = timestampForTouch(touch);
131131
} else {
132-
console.warn(
133-
'Cannot record touch move without a touch start.\n' + 'Touch Move: %s\n',
134-
'Touch Bank: %s',
135-
printTouch(touch),
136-
printTouchBank(),
137-
);
132+
if (__DEV__) {
133+
console.warn(
134+
'Cannot record touch move without a touch start.\n' +
135+
'Touch Move: %s\n',
136+
'Touch Bank: %s',
137+
printTouch(touch),
138+
printTouchBank(),
139+
);
140+
}
138141
}
139142
}
140143

@@ -150,12 +153,14 @@ function recordTouchEnd(touch: Touch): void {
150153
touchRecord.currentTimeStamp = timestampForTouch(touch);
151154
touchHistory.mostRecentTimeStamp = timestampForTouch(touch);
152155
} else {
153-
console.warn(
154-
'Cannot record touch end without a touch start.\n' + 'Touch End: %s\n',
155-
'Touch Bank: %s',
156-
printTouch(touch),
157-
printTouchBank(),
158-
);
156+
if (__DEV__) {
157+
console.warn(
158+
'Cannot record touch end without a touch start.\n' + 'Touch End: %s\n',
159+
'Touch Bank: %s',
160+
printTouch(touch),
161+
printTouchBank(),
162+
);
163+
}
159164
}
160165
}
161166

packages/react-dom/npm/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function checkDCE() {
2424
} catch (err) {
2525
// DevTools shouldn't crash React, no matter what.
2626
// We should still report in case we break this code.
27-
console.error(err);
27+
console['error'](err);
2828
}
2929
}
3030

packages/react-dom/npm/profiling.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function checkDCE() {
2424
} catch (err) {
2525
// DevTools shouldn't crash React, no matter what.
2626
// We should still report in case we break this code.
27-
console.error(err);
27+
console['error'](err);
2828
}
2929
}
3030

packages/react-dom/src/client/ReactDOM.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ if (__DEV__) {
211211
const protocol = window.location.protocol;
212212
// Don't warn in exotic cases like chrome-extension://.
213213
if (/^(https?|file):$/.test(protocol)) {
214+
// eslint-disable-next-line react-internal/no-production-logging
214215
console.info(
215216
'%cDownload the React DevTools ' +
216217
'for a better development experience: ' +

packages/react-dom/src/test-utils/ReactTestUtilsAct.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ function act(callback: () => Thenable) {
7979
if (!__DEV__) {
8080
if (didWarnAboutUsingActInProd === false) {
8181
didWarnAboutUsingActInProd = true;
82+
// eslint-disable-next-line react-internal/no-production-logging
8283
console.error(
8384
'act(...) is not supported in production builds of React, and might not behave as expected.',
8485
);

packages/react-interactions/events/src/dom/testing-library/domEnvironment.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ export function hasPointerEvent() {
2222
export function setPointerEvent(bool) {
2323
const pointerCaptureFn = name => id => {
2424
if (typeof id !== 'number') {
25-
console.error(`A pointerId must be passed to "${name}"`);
25+
if (__DEV__) {
26+
console.error(`A pointerId must be passed to "${name}"`);
27+
}
2628
}
2729
};
2830
global.PointerEvent = bool ? emptyFunction : undefined;

packages/react-is/src/ReactIs.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import {
2525
REACT_SUSPENSE_TYPE,
2626
} from 'shared/ReactSymbols';
2727
import isValidElementType from 'shared/isValidElementType';
28-
import lowPriorityWarningWithoutStack from 'shared/lowPriorityWarningWithoutStack';
2928

3029
export function typeOf(object: any) {
3130
if (typeof object === 'object' && object !== null) {
@@ -88,7 +87,7 @@ export function isAsyncMode(object: any) {
8887
if (__DEV__) {
8988
if (!hasWarnedAboutDeprecatedIsAsyncMode) {
9089
hasWarnedAboutDeprecatedIsAsyncMode = true;
91-
lowPriorityWarningWithoutStack(
90+
console.warn(
9291
'The ReactIs.isAsyncMode() alias has been deprecated, ' +
9392
'and will be removed in React 17+. Update your code to use ' +
9493
'ReactIs.isConcurrentMode() instead. It has the exact same API.',

packages/react-native-renderer/src/NativeMethodsMixinUtils.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,19 @@ export function throwOnStylesProp(component: any, props: any) {
6666
}
6767

6868
export function warnForStyleProps(props: any, validAttributes: any) {
69-
for (const key in validAttributes.style) {
70-
if (!(validAttributes[key] || props[key] === undefined)) {
71-
console.error(
72-
'You are setting the style `{ ' +
73-
key +
74-
': ... }` as a prop. You ' +
75-
'should nest it in a style object. ' +
76-
'E.g. `{ style: { ' +
77-
key +
78-
': ... } }`',
79-
);
69+
if (__DEV__) {
70+
for (const key in validAttributes.style) {
71+
if (!(validAttributes[key] || props[key] === undefined)) {
72+
console.error(
73+
'You are setting the style `{ ' +
74+
key +
75+
': ... }` as a prop. You ' +
76+
'should nest it in a style object. ' +
77+
'E.g. `{ style: { ' +
78+
key +
79+
': ... } }`',
80+
);
81+
}
8082
}
8183
}
8284
}

0 commit comments

Comments
 (0)