Skip to content

Commit 0cf22a5

Browse files
authored
Use console directly instead of warning() modules (#17599)
* Replace all warning/lowPriWarning with console calls * Replace console.warn/error with a custom wrapper at build time * Fail the build for console.error/warn() where we can't read the stack
1 parent b6c423d commit 0cf22a5

File tree

102 files changed

+753
-776
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

102 files changed

+753
-776
lines changed

.eslintrc.js

Lines changed: 14 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,18 @@ module.exports = {
135136
'jest/valid-expect-in-promise': ERROR,
136137
},
137138
},
139+
{
140+
files: [
141+
'**/__tests__/**/*.js',
142+
'scripts/**/*.js',
143+
'packages/*/npm/**/*.js',
144+
'packages/react-devtools*/**/*.js'
145+
],
146+
rules: {
147+
'react-internal/no-production-logging': OFF,
148+
'react-internal/warning-args': OFF,
149+
},
150+
},
138151
{
139152
files: ['packages/react-native-renderer/**/*.js'],
140153
globals: {

packages/create-subscription/src/createSubscription.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
import React from 'react';
1111
import invariant from 'shared/invariant';
12-
import warning from 'shared/warning';
1312

1413
type Unsubscribe = () => void;
1514

@@ -38,10 +37,10 @@ export function createSubscription<Property, Value>(
3837

3938
if (__DEV__) {
4039
if (typeof getCurrentValue !== 'function') {
41-
warning('Subscription must specify a getCurrentValue function');
40+
console.error('Subscription must specify a getCurrentValue function');
4241
}
4342
if (typeof subscribe !== 'function') {
44-
warning('Subscription must specify a subscribe function');
43+
console.error('Subscription must specify a subscribe function');
4544
}
4645
}
4746

packages/legacy-events/EventPluginUtils.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
import {invokeGuardedCallbackAndCatchFirstError} from 'shared/ReactErrorUtils';
99
import invariant from 'shared/invariant';
10-
import warning from 'shared/warning';
1110

1211
export let getFiberCurrentPropsFromNode = null;
1312
export let getInstanceFromNode = null;
@@ -23,7 +22,7 @@ export function setComponentTree(
2322
getNodeFromInstance = getNodeFromInstanceImpl;
2423
if (__DEV__) {
2524
if (!getNodeFromInstance || !getInstanceFromNode) {
26-
warning(
25+
console.error(
2726
'EventPluginUtils.setComponentTree(...): Injected ' +
2827
'module is missing getNodeFromInstance or getInstanceFromNode.',
2928
);
@@ -52,7 +51,7 @@ if (__DEV__) {
5251
: 0;
5352

5453
if (instancesIsArr !== listenersIsArr || instancesLen !== listenersLen) {
55-
warning('EventPluginUtils: Invalid `event`.');
54+
console.error('EventPluginUtils: Invalid `event`.');
5655
}
5756
};
5857
}

packages/legacy-events/EventPropagators.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
traverseTwoPhase,
1111
traverseEnterLeave,
1212
} from 'shared/ReactTreeTraversal';
13-
import warning from 'shared/warning';
1413

1514
import {getListener} from './EventPluginHub';
1615
import accumulateInto from './accumulateInto';
@@ -47,7 +46,7 @@ function listenerAtPhase(inst, event, propagationPhase: PropagationPhases) {
4746
function accumulateDirectionalDispatches(inst, phase, event) {
4847
if (__DEV__) {
4948
if (!inst) {
50-
warning('Dispatching inst must not be null');
49+
console.error('Dispatching inst must not be null');
5150
}
5251
}
5352
const listener = listenerAtPhase(inst, event, phase);

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: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
*/
99

1010
import invariant from 'shared/invariant';
11-
import warning from 'shared/warning';
1211

1312
import {isStartish, isMoveish, isEndish} from './ResponderTopLevelEventTypes';
1413

@@ -96,7 +95,7 @@ function getTouchIdentifier({identifier}: Touch): number {
9695
invariant(identifier != null, 'Touch object is missing identifier.');
9796
if (__DEV__) {
9897
if (identifier > MAX_TOUCH_BANK) {
99-
warning(
98+
console.error(
10099
'Touch identifier %s is greater than maximum supported %s which causes ' +
101100
'performance issues backfilling array locations for all of the indices.',
102101
identifier,
@@ -130,12 +129,15 @@ function recordTouchMove(touch: Touch): void {
130129
touchRecord.currentTimeStamp = timestampForTouch(touch);
131130
touchHistory.mostRecentTimeStamp = timestampForTouch(touch);
132131
} else {
133-
console.warn(
134-
'Cannot record touch move without a touch start.\n' + 'Touch Move: %s\n',
135-
'Touch Bank: %s',
136-
printTouch(touch),
137-
printTouchBank(),
138-
);
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+
}
139141
}
140142
}
141143

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

@@ -202,7 +207,7 @@ const ResponderTouchHistoryStore = {
202207
if (__DEV__) {
203208
const activeRecord = touchBank[touchHistory.indexOfSingleActiveTouch];
204209
if (activeRecord == null || !activeRecord.touchActive) {
205-
warning('Cannot find single active touch.');
210+
console.error('Cannot find single active touch.');
206211
}
207212
}
208213
}

packages/legacy-events/SyntheticEvent.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
/* eslint valid-typeof: 0 */
99

1010
import invariant from 'shared/invariant';
11-
import warning from 'shared/warning';
1211

1312
const EVENT_POOL_SIZE = 10;
1413

@@ -284,7 +283,7 @@ function getPooledWarningPropertyDefinition(propName, getVal) {
284283

285284
function warn(action, result) {
286285
if (__DEV__) {
287-
warning(
286+
console.error(
288287
"This synthetic event is reused for performance reasons. If you're seeing this, " +
289288
"you're %s `%s` on a released/nullified synthetic event. %s. " +
290289
'If you must keep the original synthetic event around, use event.persist(). ' +

packages/react-cache/src/ReactCache.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
*/
99

1010
import React from 'react';
11-
import warning from 'shared/warning';
1211

1312
import {createLRU} from './LRU';
1413

@@ -71,7 +70,7 @@ function identityHashFn(input) {
7170
input !== undefined &&
7271
input !== null
7372
) {
74-
warning(
73+
console.error(
7574
'Invalid key type. Expected a string, number, symbol, or boolean, ' +
7675
'but instead received: %s' +
7776
'\n\nTo use non-primitive values as keys, you must pass a hash ' +

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ import {
5353
} from 'legacy-events/EventPropagators';
5454
import ReactVersion from 'shared/ReactVersion';
5555
import invariant from 'shared/invariant';
56-
import lowPriorityWarning from 'shared/lowPriorityWarning';
57-
import warning from 'shared/warning';
5856
import {exposeConcurrentModeAPIs} from 'shared/ReactFeatureFlags';
5957

6058
import {
@@ -92,7 +90,7 @@ if (__DEV__) {
9290
typeof Set.prototype.clear !== 'function' ||
9391
typeof Set.prototype.forEach !== 'function'
9492
) {
95-
warning(
93+
console.error(
9694
'React depends on Map and Set built-in types. Make sure that you load a ' +
9795
'polyfill in older browsers. https://fb.me/react-polyfills',
9896
);
@@ -144,7 +142,7 @@ const ReactDOM: Object = {
144142
if (__DEV__) {
145143
if (!didWarnAboutUnstableCreatePortal) {
146144
didWarnAboutUnstableCreatePortal = true;
147-
lowPriorityWarning(
145+
console.warn(
148146
'The ReactDOM.unstable_createPortal() alias has been deprecated, ' +
149147
'and will be removed in React 17+. Update your code to use ' +
150148
'ReactDOM.createPortal() instead. It has the exact same API, ' +
@@ -213,6 +211,7 @@ if (__DEV__) {
213211
const protocol = window.location.protocol;
214212
// Don't warn in exotic cases like chrome-extension://.
215213
if (/^(https?|file):$/.test(protocol)) {
214+
// eslint-disable-next-line react-internal/no-production-logging
216215
console.info(
217216
'%cDownload the React DevTools ' +
218217
'for a better development experience: ' +

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

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import {getCurrentFiberOwnerNameInDevOrNull} from 'react-reconciler/src/ReactCurrentFiber';
1212
import {registrationNameModules} from 'legacy-events/EventPluginRegistry';
1313
import {canUseDOM} from 'shared/ExecutionEnvironment';
14-
import warning from 'shared/warning';
1514
import endsWith from 'shared/endsWith';
1615
import {setListenToResponderEventTypes} from '../events/DOMEventResponderSystem';
1716

@@ -183,7 +182,7 @@ if (__DEV__) {
183182
return;
184183
}
185184
didWarnInvalidHydration = true;
186-
warning(
185+
console.error(
187186
'Text content did not match. Server: "%s" Client: "%s"',
188187
normalizedServerText,
189188
normalizedClientText,
@@ -208,7 +207,7 @@ if (__DEV__) {
208207
return;
209208
}
210209
didWarnInvalidHydration = true;
211-
warning(
210+
console.error(
212211
'Prop `%s` did not match. Server: %s Client: %s',
213212
propName,
214213
JSON.stringify(normalizedServerValue),
@@ -225,12 +224,12 @@ if (__DEV__) {
225224
attributeNames.forEach(function(name) {
226225
names.push(name);
227226
});
228-
warning('Extra attributes from the server: %s', names);
227+
console.error('Extra attributes from the server: %s', names);
229228
};
230229

231230
warnForInvalidEventListener = function(registrationName, listener) {
232231
if (listener === false) {
233-
warning(
232+
console.error(
234233
'Expected `%s` listener to be a function, instead got `false`.\n\n' +
235234
'If you used to conditionally omit it with %s={condition && value}, ' +
236235
'pass %s={condition ? value : undefined} instead.',
@@ -239,7 +238,7 @@ if (__DEV__) {
239238
registrationName,
240239
);
241240
} else {
242-
warning(
241+
console.error(
243242
'Expected `%s` listener to be a function, instead got a value of `%s` type.',
244243
registrationName,
245244
typeof listener,
@@ -412,7 +411,7 @@ export function createElement(
412411
// Should this check be gated by parent namespace? Not sure we want to
413412
// allow <SVG> or <mATH>.
414413
if (!isCustomComponentTag && type !== type.toLowerCase()) {
415-
warning(
414+
console.error(
416415
'<%s /> is using incorrect casing. ' +
417416
'Use PascalCase for React components, ' +
418417
'or lowercase for HTML elements.',
@@ -427,7 +426,7 @@ export function createElement(
427426
const div = ownerDocument.createElement('div');
428427
if (__DEV__) {
429428
if (enableTrustedTypesIntegration && !didWarnScriptTags) {
430-
warning(
429+
console.error(
431430
'Encountered a script tag while rendering React component. ' +
432431
'Scripts inside React components are never executed when rendering ' +
433432
'on the client. Consider using template tag instead ' +
@@ -482,7 +481,7 @@ export function createElement(
482481
!Object.prototype.hasOwnProperty.call(warnedUnknownTags, type)
483482
) {
484483
warnedUnknownTags[type] = true;
485-
warning(
484+
console.error(
486485
'The tag <%s> is unrecognized in this browser. ' +
487486
'If you meant to render a React component, start its name with ' +
488487
'an uppercase letter.',
@@ -518,7 +517,7 @@ export function setInitialProperties(
518517
!didWarnShadyDOM &&
519518
(domElement: any).shadyRoot
520519
) {
521-
warning(
520+
console.error(
522521
'%s is using shady DOM. Using shady DOM with React can ' +
523522
'cause things to break subtly.',
524523
getCurrentFiberOwnerNameInDevOrNull() || 'A component',
@@ -918,7 +917,7 @@ export function diffHydratedProperties(
918917
!didWarnShadyDOM &&
919918
(domElement: any).shadyRoot
920919
) {
921-
warning(
920+
console.error(
922921
'%s is using shady DOM. Using shady DOM with React can ' +
923922
'cause things to break subtly.',
924923
getCurrentFiberOwnerNameInDevOrNull() || 'A component',
@@ -1210,7 +1209,7 @@ export function warnForDeletedHydratableElement(
12101209
return;
12111210
}
12121211
didWarnInvalidHydration = true;
1213-
warning(
1212+
console.error(
12141213
'Did not expect server HTML to contain a <%s> in <%s>.',
12151214
child.nodeName.toLowerCase(),
12161215
parentNode.nodeName.toLowerCase(),
@@ -1227,7 +1226,7 @@ export function warnForDeletedHydratableText(
12271226
return;
12281227
}
12291228
didWarnInvalidHydration = true;
1230-
warning(
1229+
console.error(
12311230
'Did not expect server HTML to contain the text node "%s" in <%s>.',
12321231
child.nodeValue,
12331232
parentNode.nodeName.toLowerCase(),
@@ -1245,7 +1244,7 @@ export function warnForInsertedHydratedElement(
12451244
return;
12461245
}
12471246
didWarnInvalidHydration = true;
1248-
warning(
1247+
console.error(
12491248
'Expected server HTML to contain a matching <%s> in <%s>.',
12501249
tag,
12511250
parentNode.nodeName.toLowerCase(),
@@ -1269,7 +1268,7 @@ export function warnForInsertedHydratedText(
12691268
return;
12701269
}
12711270
didWarnInvalidHydration = true;
1272-
warning(
1271+
console.error(
12731272
'Expected server HTML to contain a matching text node for "%s" in <%s>.',
12741273
text,
12751274
parentNode.nodeName.toLowerCase(),

0 commit comments

Comments
 (0)