warning.js.flow 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. var emptyFunction = require('./emptyFunction');
  11. /**
  12. * Similar to invariant but only logs a warning if the condition is not met.
  13. * This can be used to log issues in development environments in critical
  14. * paths. Removing the logging code for production environments will keep the
  15. * same logic and follow the same code paths.
  16. */
  17. var warning = emptyFunction;
  18. if (__DEV__) {
  19. function printWarning(format, ...args) {
  20. var argIndex = 0;
  21. var message = 'Warning: ' + format.replace(/%s/g, () => args[argIndex++]);
  22. if (typeof console !== 'undefined') {
  23. console.error(message);
  24. }
  25. try {
  26. // --- Welcome to debugging React ---
  27. // This error was thrown as a convenience so that you can use this stack
  28. // to find the callsite that caused this warning to fire.
  29. throw new Error(message);
  30. } catch (x) {}
  31. }
  32. warning = function (condition, format, ...args) {
  33. if (format === undefined) {
  34. throw new Error('`warning(condition, format, ...args)` requires a warning ' + 'message argument');
  35. }
  36. if (format.indexOf('Failed Composite propType: ') === 0) {
  37. return; // Ignore CompositeComponent proptype check.
  38. }
  39. if (!condition) {
  40. printWarning(format, ...args);
  41. }
  42. };
  43. }
  44. module.exports = warning;