warning.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. 'use strict';
  8. /**
  9. * Similar to invariant but only logs a warning if the condition is not met.
  10. * This can be used to log issues in development environments in critical
  11. * paths. Removing the logging code for production environments will keep the
  12. * same logic and follow the same code paths.
  13. */
  14. var __DEV__ = process.env.NODE_ENV !== 'production';
  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. module.exports = warning;