grim.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. (function() {
  2. var Deprecation, Emitter, getRawStack, grim;
  3. Emitter = require('emissary').Emitter;
  4. Deprecation = require('./deprecation');
  5. if (global.__grim__ == null) {
  6. grim = global.__grim__ = {
  7. deprecations: {},
  8. includeDeprecatedAPIs: true,
  9. getDeprecations: function() {
  10. var deprecation, deprecations, deprecationsByLineNumber, deprecationsByPackage, fileName, lineNumber, packageName, _ref;
  11. deprecations = [];
  12. _ref = grim.deprecations;
  13. for (fileName in _ref) {
  14. deprecationsByLineNumber = _ref[fileName];
  15. for (lineNumber in deprecationsByLineNumber) {
  16. deprecationsByPackage = deprecationsByLineNumber[lineNumber];
  17. for (packageName in deprecationsByPackage) {
  18. deprecation = deprecationsByPackage[packageName];
  19. deprecations.push(deprecation);
  20. }
  21. }
  22. }
  23. return deprecations;
  24. },
  25. getDeprecationsLength: function() {
  26. return this.getDeprecations().length;
  27. },
  28. clearDeprecations: function() {
  29. grim.deprecations = {};
  30. },
  31. logDeprecations: function() {
  32. var deprecation, deprecations, _i, _len;
  33. deprecations = this.getDeprecations();
  34. deprecations.sort(function(a, b) {
  35. return b.getCallCount() - a.getCallCount();
  36. });
  37. console.warn("\nCalls to deprecated functions\n-----------------------------");
  38. for (_i = 0, _len = deprecations.length; _i < _len; _i++) {
  39. deprecation = deprecations[_i];
  40. console.warn("(" + (deprecation.getCallCount()) + ") " + (deprecation.getOriginName()) + " : " + (deprecation.getMessage()), deprecation);
  41. }
  42. },
  43. deprecate: function(message, metadata) {
  44. var deprecation, deprecationSite, error, fileName, lineNumber, originalStackTraceLimit, packageName, stack, _base, _base1, _base2, _ref, _ref1;
  45. originalStackTraceLimit = Error.stackTraceLimit;
  46. Error.stackTraceLimit = 7;
  47. error = new Error;
  48. Error.captureStackTrace(error);
  49. Error.stackTraceLimit = originalStackTraceLimit;
  50. stack = (_ref = typeof error.getRawStack === "function" ? error.getRawStack() : void 0) != null ? _ref : getRawStack(error);
  51. stack = stack.slice(1);
  52. deprecationSite = stack[0];
  53. fileName = deprecationSite.getFileName();
  54. lineNumber = deprecationSite.getLineNumber();
  55. packageName = (_ref1 = metadata != null ? metadata.packageName : void 0) != null ? _ref1 : "";
  56. if ((_base = grim.deprecations)[fileName] == null) {
  57. _base[fileName] = {};
  58. }
  59. if ((_base1 = grim.deprecations[fileName])[lineNumber] == null) {
  60. _base1[lineNumber] = {};
  61. }
  62. if ((_base2 = grim.deprecations[fileName][lineNumber])[packageName] == null) {
  63. _base2[packageName] = new Deprecation(message);
  64. }
  65. deprecation = grim.deprecations[fileName][lineNumber][packageName];
  66. deprecation.addStack(stack, metadata);
  67. grim.emit("updated", deprecation);
  68. },
  69. addSerializedDeprecation: function(serializedDeprecation) {
  70. var deprecation, fileName, lineNumber, message, packageName, stack, stacks, _base, _base1, _base2, _i, _len, _ref, _ref1, _ref2;
  71. deprecation = Deprecation.deserialize(serializedDeprecation);
  72. message = deprecation.getMessage();
  73. fileName = deprecation.fileName, lineNumber = deprecation.lineNumber;
  74. stacks = deprecation.getStacks();
  75. packageName = (_ref = (_ref1 = stacks[0]) != null ? (_ref2 = _ref1.metadata) != null ? _ref2.packageName : void 0 : void 0) != null ? _ref : "";
  76. if ((_base = grim.deprecations)[fileName] == null) {
  77. _base[fileName] = {};
  78. }
  79. if ((_base1 = grim.deprecations[fileName])[lineNumber] == null) {
  80. _base1[lineNumber] = {};
  81. }
  82. if ((_base2 = grim.deprecations[fileName][lineNumber])[packageName] == null) {
  83. _base2[packageName] = new Deprecation(message, fileName, lineNumber);
  84. }
  85. deprecation = grim.deprecations[fileName][lineNumber][packageName];
  86. for (_i = 0, _len = stacks.length; _i < _len; _i++) {
  87. stack = stacks[_i];
  88. deprecation.addStack(stack, stack.metadata);
  89. }
  90. grim.emit("updated", deprecation);
  91. }
  92. };
  93. Emitter.extend(grim);
  94. }
  95. getRawStack = function(error) {
  96. var originalPrepareStackTrace, result;
  97. originalPrepareStackTrace = Error.prepareStackTrace;
  98. Error.prepareStackTrace = function(error, stack) {
  99. return stack;
  100. };
  101. result = error.stack;
  102. Error.prepareStackTrace = originalPrepareStackTrace;
  103. return result;
  104. };
  105. module.exports = global.__grim__;
  106. }).call(this);