mirror of
https://github.com/facebook/react.git
synced 2026-11-18 20:15:56 +00:00
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.
This commit is contained in:
1 parent
8b580a89d6
commit
18d2e0c03e
13 files changed
+291
-50
No files matched your search
@@ -0,0 +1,52 @@
|
||||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Forked from fbjs/warning:
|
||||
* https://github.com/facebook/fbjs/blob/e66ba20ad5be433eb54423f2b097d829324d9de6/packages/fbjs/src/__forks__/warning.js
|
||||
*
|
||||
* Only change is we use console.warn instead of console.error,
|
||||
* and do nothing when 'console' is not supported.
|
||||
* This really simplifies the code.
|
||||
* ---
|
||||
* Similar to invariant but only logs a warning if the condition is not met.
|
||||
* This can be used to log issues in development environments in critical
|
||||
* paths. Removing the logging code for production environments will keep the
|
||||
* same logic and follow the same code paths.
|
||||
*/
|
||||
|
||||
let lowPriorityWarningWithoutStack = function() {};
|
||||
|
||||
if (__DEV__) {
|
||||
const printWarning = function(format, ...args) {
|
||||
let argIndex = 0;
|
||||
const message = 'Warning: ' + format.replace(/%s/g, () => args[argIndex++]);
|
||||
if (typeof console !== 'undefined') {
|
||||
console.warn(message);
|
||||
}
|
||||
try {
|
||||
// --- Welcome to debugging React ---
|
||||
// This error was thrown as a convenience so that you can use this stack
|
||||
// to find the callsite that caused this warning to fire.
|
||||
throw new Error(message);
|
||||
} catch (x) {}
|
||||
};
|
||||
|
||||
lowPriorityWarningWithoutStack = function(condition, format, ...args) {
|
||||
if (format === undefined) {
|
||||
throw new Error(
|
||||
'`lowPriorityWarningWithoutStack(condition, format, ...args)` requires a warning ' +
|
||||
'message argument',
|
||||
);
|
||||
}
|
||||
if (!condition) {
|
||||
printWarning(format, ...args);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export default lowPriorityWarningWithoutStack;
|
||||
Reference in new issue
Block a user