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