invariant.js.flow 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /**
  2. * Copyright (c) 2013-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 invariant
  8. */
  9. 'use strict';
  10. /**
  11. * Use invariant() to assert state which your program assumes to be true.
  12. *
  13. * Provide sprintf-style format (only %s is supported) and arguments
  14. * to provide information about what broke and what you were
  15. * expecting.
  16. *
  17. * The invariant message will be stripped in production, but the invariant
  18. * will remain to ensure logic does not differ in production.
  19. */
  20. var validateFormat = function (format) {};
  21. if (__DEV__) {
  22. validateFormat = function (format) {
  23. if (format === undefined) {
  24. throw new Error('invariant requires an error message argument');
  25. }
  26. };
  27. }
  28. function invariant(condition, format, a, b, c, d, e, f) {
  29. validateFormat(format);
  30. if (!condition) {
  31. var error;
  32. if (format === undefined) {
  33. error = new Error('Minified exception occurred; use the non-minified dev environment ' + 'for the full error message and additional helpful warnings.');
  34. } else {
  35. var args = [a, b, c, d, e, f];
  36. var argIndex = 0;
  37. error = new Error(format.replace(/%s/g, function () {
  38. return args[argIndex++];
  39. }));
  40. error.name = 'Invariant Violation';
  41. }
  42. error.framesToPop = 1; // we don't care about invariant's own frame
  43. throw error;
  44. }
  45. }
  46. module.exports = invariant;