warning.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. /**
  8. * Similar to invariant but only logs a warning if the condition is not met.
  9. * This can be used to log issues in development environments in critical
  10. * paths. Removing the logging code for production environments will keep the
  11. * same logic and follow the same code paths.
  12. */
  13. // var __DEV__ = typeof process !== 'undefined' && process.env && process.env.NODE_ENV !== 'production';
  14. var __DEV__ = true;
  15. var warning = function() {};
  16. if (__DEV__) {
  17. var printWarning = function printWarning(format, args) {
  18. var len = arguments.length;
  19. args = new Array(len > 1 ? len - 1 : 0);
  20. for (var key = 1; key < len; key++) {
  21. args[key - 1] = arguments[key];
  22. }
  23. var argIndex = 0;
  24. var message = 'Warning: ' +
  25. format.replace(/%s/g, function() {
  26. return args[argIndex++];
  27. });
  28. if (typeof console !== 'undefined') {
  29. console.error(message);
  30. }
  31. try {
  32. // --- Welcome to debugging React ---
  33. // This error was thrown as a convenience so that you can use this stack
  34. // to find the callsite that caused this warning to fire.
  35. throw new Error(message);
  36. } catch (x) {}
  37. }
  38. warning = function(condition, format, args) {
  39. var len = arguments.length;
  40. args = new Array(len > 2 ? len - 2 : 0);
  41. for (var key = 2; key < len; key++) {
  42. args[key - 2] = arguments[key];
  43. }
  44. if (format === undefined) {
  45. throw new Error(
  46. '`warning(condition, format, ...args)` requires a warning ' +
  47. 'message argument'
  48. );
  49. }
  50. if (!condition) {
  51. printWarning.apply(null, [format].concat(args));
  52. }
  53. };
  54. }
  55. export default warning;