warning.js.flow 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /**
  2. * Copyright (c) 2014-present, Facebook, Inc.
  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. * @providesModule warning
  8. */
  9. 'use strict';
  10. declare var __DEV__: boolean;
  11. var emptyFunction = require("./emptyFunction");
  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. function printWarning(format, ...args) {
  19. var argIndex = 0;
  20. var message = 'Warning: ' + format.replace(/%s/g, () => args[argIndex++]);
  21. if (typeof console !== 'undefined') {
  22. console.error(message);
  23. }
  24. try {
  25. // --- Welcome to debugging React ---
  26. // This error was thrown as a convenience so that you can use this stack
  27. // to find the callsite that caused this warning to fire.
  28. throw new Error(message);
  29. } catch (x) {}
  30. }
  31. var warning = __DEV__ ? function (condition, format, ...args) {
  32. if (format === undefined) {
  33. throw new Error('`warning(condition, format, ...args)` requires a warning ' + 'message argument');
  34. }
  35. if (!condition) {
  36. printWarning(format, ...args);
  37. }
  38. } : emptyFunction;
  39. module.exports = warning;