Skip to content

Commit 3911544

Browse files
committed
Improve low priority warning (facebook#9754)
* Add back caught error and other checks to 'lowPriorityWarning' **what is the change?:** This change makes 'lowPriorityWarning' an exact copy of 'warning.js' from https://github.com/facebook/fbjs/blob/e66ba20ad5be433eb54423f2b097d829324d9de6/packages/fbjs/src/__forks__/warning.js where before we had skipped some checks from that module. - Adds an error which we catch, in order to let people find the error and resulting stack trace when using devtools with 'pause on caught errors' checked. - Adds check that 'format' argument is passed **why make this change?:** - To maintain a closer fork to 'warning.js' - To allow easier debugging using 'pause on caught errors' - To validate inputs to 'lowPriorityWarning' **test plan:** `yarn test`
1 parent 80e87c4 commit 3911544

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

src/shared/utils/lowPriorityWarning.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
* and do nothing when 'console' is not supported.
2020
* This really simplifies the code.
2121
* ---
22-
*
2322
* Similar to invariant but only logs a warning if the condition is not met.
2423
* This can be used to log issues in development environments in critical
2524
* paths. Removing the logging code for production environments will keep the
@@ -29,12 +28,30 @@
2928
var lowPriorityWarning = function() {};
3029

3130
if (__DEV__) {
32-
lowPriorityWarning = function(condition, format, ...args) {
31+
const printWarning = function(format, ...args) {
3332
var argIndex = 0;
3433
var message = 'Warning: ' + format.replace(/%s/g, () => args[argIndex++]);
35-
if (!condition && typeof console !== 'undefined') {
34+
if (typeof console !== 'undefined') {
3635
console.warn(message);
3736
}
37+
try {
38+
// --- Welcome to debugging React ---
39+
// This error was thrown as a convenience so that you can use this stack
40+
// to find the callsite that caused this warning to fire.
41+
throw new Error(message);
42+
} catch (x) {}
43+
};
44+
45+
lowPriorityWarning = function(condition, format, ...args) {
46+
if (format === undefined) {
47+
throw new Error(
48+
'`warning(condition, format, ...args)` requires a warning ' +
49+
'message argument',
50+
);
51+
}
52+
if (!condition) {
53+
printWarning(format, ...args);
54+
}
3855
};
3956
}
4057

0 commit comments

Comments
 (0)