Skip to content

Commit 18d2e0c

Browse files
Jessidhiagaearon
authored andcommitted
Warning system refactoring (part 1) (#16799)
* Rename lowPriorityWarning to lowPriorityWarningWithoutStack This maintains parity with the other warning-like functions. * Duplicate the toWarnDev tests to test toLowPriorityWarnDev * Make a lowPriorityWarning version of warning.js * Extract both variants in print-warning Avoids parsing lowPriorityWarning.js itself as the way it forwards the call to lowPriorityWarningWithoutStack is not analyzable.
1 parent 8b580a8 commit 18d2e0c

File tree

13 files changed

+291
-50
lines changed

13 files changed

+291
-50
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ import ReactVersion from 'shared/ReactVersion';
6161
import ReactSharedInternals from 'shared/ReactSharedInternals';
6262
import getComponentName from 'shared/getComponentName';
6363
import invariant from 'shared/invariant';
64-
import lowPriorityWarning from 'shared/lowPriorityWarning';
64+
import lowPriorityWarningWithoutStack from 'shared/lowPriorityWarningWithoutStack';
6565
import warningWithoutStack from 'shared/warningWithoutStack';
6666
import {enableStableConcurrentModeAPIs} from 'shared/ReactFeatureFlags';
6767

@@ -542,7 +542,7 @@ function legacyCreateRootFromDOMContainer(
542542
if (__DEV__) {
543543
if (shouldHydrate && !forceHydrate && !warnedAboutHydrateAPI) {
544544
warnedAboutHydrateAPI = true;
545-
lowPriorityWarning(
545+
lowPriorityWarningWithoutStack(
546546
false,
547547
'render(): Calling ReactDOM.render() to hydrate server-rendered markup ' +
548548
'will stop working in React v17. Replace the ReactDOM.render() call ' +
@@ -801,7 +801,7 @@ const ReactDOM: Object = {
801801
unstable_createPortal(...args) {
802802
if (!didWarnAboutUnstableCreatePortal) {
803803
didWarnAboutUnstableCreatePortal = true;
804-
lowPriorityWarning(
804+
lowPriorityWarningWithoutStack(
805805
false,
806806
'The ReactDOM.unstable_createPortal() alias has been deprecated, ' +
807807
'and will be removed in React 17+. Update your code to use ' +

packages/react-dom/src/server/ReactPartialRenderer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import type {ReactProvider, ReactContext} from 'shared/ReactTypes';
1515
import React from 'react';
1616
import invariant from 'shared/invariant';
1717
import getComponentName from 'shared/getComponentName';
18-
import lowPriorityWarning from 'shared/lowPriorityWarning';
18+
import lowPriorityWarningWithoutStack from 'shared/lowPriorityWarningWithoutStack';
1919
import warning from 'shared/warning';
2020
import warningWithoutStack from 'shared/warningWithoutStack';
2121
import describeComponentFrame from 'shared/describeComponentFrame';
@@ -588,7 +588,7 @@ function resolve(
588588
const componentName = getComponentName(Component) || 'Unknown';
589589

590590
if (!didWarnAboutDeprecatedWillMount[componentName]) {
591-
lowPriorityWarning(
591+
lowPriorityWarningWithoutStack(
592592
false,
593593
// keep this warning in sync with ReactStrictModeWarning.js
594594
'componentWillMount has been renamed, and is not recommended for use. ' +

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
} from 'shared/ReactWorkTags';
1818
import SyntheticEvent from 'legacy-events/SyntheticEvent';
1919
import invariant from 'shared/invariant';
20-
import lowPriorityWarning from 'shared/lowPriorityWarning';
20+
import lowPriorityWarningWithoutStack from 'shared/lowPriorityWarningWithoutStack';
2121
import {ELEMENT_NODE} from '../shared/HTMLNodeType';
2222
import * as DOMTopLevelEventTypes from '../events/DOMTopLevelEventTypes';
2323
import {PLUGIN_EVENT_SYSTEM} from 'legacy-events/EventSystemFlags';
@@ -361,7 +361,7 @@ const ReactTestUtils = {
361361
mockComponent: function(module, mockTagName) {
362362
if (!hasWarnedAboutDeprecatedMockComponent) {
363363
hasWarnedAboutDeprecatedMockComponent = true;
364-
lowPriorityWarning(
364+
lowPriorityWarningWithoutStack(
365365
false,
366366
'ReactTestUtils.mockComponent() is deprecated. ' +
367367
'Use shallow rendering or jest.mock() instead.\n\n' +

packages/react-is/src/ReactIs.js

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

3030
export function typeOf(object: any) {
3131
if (typeof object === 'object' && object !== null) {
@@ -88,7 +88,7 @@ export function isAsyncMode(object: any) {
8888
if (__DEV__) {
8989
if (!hasWarnedAboutDeprecatedIsAsyncMode) {
9090
hasWarnedAboutDeprecatedIsAsyncMode = true;
91-
lowPriorityWarning(
91+
lowPriorityWarningWithoutStack(
9292
false,
9393
'The ReactIs.isAsyncMode() alias has been deprecated, ' +
9494
'and will be removed in React 17+. Update your code to use ' +

packages/react-reconciler/src/ReactStrictModeWarnings.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {getStackByFiberInDevAndProd} from './ReactCurrentFiber';
1313

1414
import getComponentName from 'shared/getComponentName';
1515
import {StrictMode} from './ReactTypeOfMode';
16-
import lowPriorityWarning from 'shared/lowPriorityWarning';
16+
import lowPriorityWarningWithoutStack from 'shared/lowPriorityWarningWithoutStack';
1717
import warningWithoutStack from 'shared/warningWithoutStack';
1818

1919
type FiberArray = Array<Fiber>;
@@ -237,7 +237,7 @@ if (__DEV__) {
237237
if (componentWillMountUniqueNames.size > 0) {
238238
const sortedNames = setToSortedString(componentWillMountUniqueNames);
239239

240-
lowPriorityWarning(
240+
lowPriorityWarningWithoutStack(
241241
false,
242242
'componentWillMount has been renamed, and is not recommended for use. ' +
243243
'See https://fb.me/react-unsafe-component-lifecycles for details.\n\n' +
@@ -256,7 +256,7 @@ if (__DEV__) {
256256
componentWillReceivePropsUniqueNames,
257257
);
258258

259-
lowPriorityWarning(
259+
lowPriorityWarningWithoutStack(
260260
false,
261261
'componentWillReceiveProps has been renamed, and is not recommended for use. ' +
262262
'See https://fb.me/react-unsafe-component-lifecycles for details.\n\n' +
@@ -276,7 +276,7 @@ if (__DEV__) {
276276
if (componentWillUpdateUniqueNames.size > 0) {
277277
const sortedNames = setToSortedString(componentWillUpdateUniqueNames);
278278

279-
lowPriorityWarning(
279+
lowPriorityWarningWithoutStack(
280280
false,
281281
'componentWillUpdate has been renamed, and is not recommended for use. ' +
282282
'See https://fb.me/react-unsafe-component-lifecycles for details.\n\n' +

packages/react/src/ReactBaseClasses.js

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

88
import invariant from 'shared/invariant';
9-
import lowPriorityWarning from 'shared/lowPriorityWarning';
9+
import lowPriorityWarningWithoutStack from 'shared/lowPriorityWarningWithoutStack';
1010

1111
import ReactNoopUpdateQueue from './ReactNoopUpdateQueue';
1212

@@ -105,7 +105,7 @@ if (__DEV__) {
105105
const defineDeprecationWarning = function(methodName, info) {
106106
Object.defineProperty(Component.prototype, methodName, {
107107
get: function() {
108-
lowPriorityWarning(
108+
lowPriorityWarningWithoutStack(
109109
false,
110110
'%s(...) is deprecated in plain JavaScript React classes. %s',
111111
info[0],

packages/react/src/ReactElementValidator.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* that support it.
1313
*/
1414

15-
import lowPriorityWarning from 'shared/lowPriorityWarning';
15+
import lowPriorityWarningWithoutStack from 'shared/lowPriorityWarningWithoutStack';
1616
import isValidElementType from 'shared/isValidElementType';
1717
import getComponentName from 'shared/getComponentName';
1818
import {
@@ -477,7 +477,7 @@ export function createFactoryWithValidation(type) {
477477
Object.defineProperty(validatedFactory, 'type', {
478478
enumerable: false,
479479
get: function() {
480-
lowPriorityWarning(
480+
lowPriorityWarningWithoutStack(
481481
false,
482482
'Factory.type is deprecated. Access the class directly ' +
483483
'before passing it to createFactory.',

packages/shared/forks/lowPriorityWarning.www.js renamed to packages/shared/forks/lowPriorityWarningWithoutStack.www.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8+
// This "lowPriorityWarning" is an external module
89
export default require('lowPriorityWarning');

packages/shared/lowPriorityWarning.js

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,47 +5,27 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8+
import lowPriorityWarningWithoutStack from 'shared/lowPriorityWarningWithoutStack';
9+
import ReactSharedInternals from 'shared/ReactSharedInternals';
10+
811
/**
9-
* Forked from fbjs/warning:
10-
* https://github.com/facebook/fbjs/blob/e66ba20ad5be433eb54423f2b097d829324d9de6/packages/fbjs/src/__forks__/warning.js
11-
*
12-
* Only change is we use console.warn instead of console.error,
13-
* and do nothing when 'console' is not supported.
14-
* This really simplifies the code.
15-
* ---
1612
* Similar to invariant but only logs a warning if the condition is not met.
1713
* This can be used to log issues in development environments in critical
1814
* paths. Removing the logging code for production environments will keep the
1915
* same logic and follow the same code paths.
2016
*/
2117

22-
let lowPriorityWarning = function() {};
18+
let lowPriorityWarning = lowPriorityWarningWithoutStack;
2319

2420
if (__DEV__) {
25-
const printWarning = function(format, ...args) {
26-
let argIndex = 0;
27-
const message = 'Warning: ' + format.replace(/%s/g, () => args[argIndex++]);
28-
if (typeof console !== 'undefined') {
29-
console.warn(message);
30-
}
31-
try {
32-
// --- Welcome to debugging React ---
33-
// This error was thrown as a convenience so that you can use this stack
34-
// to find the callsite that caused this warning to fire.
35-
throw new Error(message);
36-
} catch (x) {}
37-
};
38-
3921
lowPriorityWarning = function(condition, format, ...args) {
40-
if (format === undefined) {
41-
throw new Error(
42-
'`lowPriorityWarning(condition, format, ...args)` requires a warning ' +
43-
'message argument',
44-
);
45-
}
46-
if (!condition) {
47-
printWarning(format, ...args);
22+
if (condition) {
23+
return;
4824
}
25+
const ReactDebugCurrentFrame = ReactSharedInternals.ReactDebugCurrentFrame;
26+
const stack = ReactDebugCurrentFrame.getStackAddendum();
27+
// eslint-disable-next-line react-internal/warning-and-invariant-args
28+
lowPriorityWarningWithoutStack(false, format + '%s', ...args, stack);
4929
};
5030
}
5131

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
/**
9+
* Forked from fbjs/warning:
10+
* https://github.com/facebook/fbjs/blob/e66ba20ad5be433eb54423f2b097d829324d9de6/packages/fbjs/src/__forks__/warning.js
11+
*
12+
* Only change is we use console.warn instead of console.error,
13+
* and do nothing when 'console' is not supported.
14+
* This really simplifies the code.
15+
* ---
16+
* Similar to invariant but only logs a warning if the condition is not met.
17+
* This can be used to log issues in development environments in critical
18+
* paths. Removing the logging code for production environments will keep the
19+
* same logic and follow the same code paths.
20+
*/
21+
22+
let lowPriorityWarningWithoutStack = function() {};
23+
24+
if (__DEV__) {
25+
const printWarning = function(format, ...args) {
26+
let argIndex = 0;
27+
const message = 'Warning: ' + format.replace(/%s/g, () => args[argIndex++]);
28+
if (typeof console !== 'undefined') {
29+
console.warn(message);
30+
}
31+
try {
32+
// --- Welcome to debugging React ---
33+
// This error was thrown as a convenience so that you can use this stack
34+
// to find the callsite that caused this warning to fire.
35+
throw new Error(message);
36+
} catch (x) {}
37+
};
38+
39+
lowPriorityWarningWithoutStack = function(condition, format, ...args) {
40+
if (format === undefined) {
41+
throw new Error(
42+
'`lowPriorityWarningWithoutStack(condition, format, ...args)` requires a warning ' +
43+
'message argument',
44+
);
45+
}
46+
if (!condition) {
47+
printWarning(format, ...args);
48+
}
49+
};
50+
}
51+
52+
export default lowPriorityWarningWithoutStack;

0 commit comments

Comments
 (0)