|
| 1 | +/** |
| 2 | + * Copyright 2014-2015, Facebook, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | + * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | + */ |
| 9 | + |
| 10 | +'use strict'; |
| 11 | + |
| 12 | +/** |
| 13 | + * Similar to invariant but only logs a warning if the condition is not met. |
| 14 | + * This can be used to log issues in development environments in critical |
| 15 | + * paths. Removing the logging code for production environments will keep the |
| 16 | + * same logic and follow the same code paths. |
| 17 | + */ |
| 18 | + |
| 19 | +var warning = function() {}; |
| 20 | + |
| 21 | +if (process.env.NODE_ENV !== 'production') { |
| 22 | + warning = function(condition, format, args) { |
| 23 | + var len = arguments.length; |
| 24 | + args = new Array(len > 2 ? len - 2 : 0); |
| 25 | + for (var key = 2; key < len; key++) { |
| 26 | + args[key - 2] = arguments[key]; |
| 27 | + } |
| 28 | + if (format === undefined) { |
| 29 | + throw new Error( |
| 30 | + '`warning(condition, format, ...args)` requires a warning ' + |
| 31 | + 'message argument' |
| 32 | + ); |
| 33 | + } |
| 34 | + |
| 35 | + if (format.length < 10 || (/^[s\W]*$/).test(format)) { |
| 36 | + throw new Error( |
| 37 | + 'The warning format should be able to uniquely identify this ' + |
| 38 | + 'warning. Please, use a more descriptive format than: ' + format |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + if (!condition) { |
| 43 | + var argIndex = 0; |
| 44 | + var message = 'Warning: ' + |
| 45 | + format.replace(/%s/g, function() { |
| 46 | + return args[argIndex++]; |
| 47 | + }); |
| 48 | + if (typeof console !== 'undefined') { |
| 49 | + console.error(message); |
| 50 | + } |
| 51 | + try { |
| 52 | + // This error was thrown as a convenience so that you can use this stack |
| 53 | + // to find the callsite that caused this warning to fire. |
| 54 | + throw new Error(message); |
| 55 | + } catch(x) {} |
| 56 | + } |
| 57 | + }; |
| 58 | +} |
| 59 | + |
| 60 | +module.exports = warning; |
0 commit comments