deprecation.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. (function() {
  2. var Deprecation, SourceMapCache;
  3. SourceMapCache = {};
  4. module.exports = Deprecation = (function() {
  5. Deprecation.getFunctionNameFromCallsite = function(callsite) {};
  6. Deprecation.deserialize = function(_arg) {
  7. var deprecation, fileName, lineNumber, message, stack, stacks, _i, _len;
  8. message = _arg.message, fileName = _arg.fileName, lineNumber = _arg.lineNumber, stacks = _arg.stacks;
  9. deprecation = new Deprecation(message, fileName, lineNumber);
  10. for (_i = 0, _len = stacks.length; _i < _len; _i++) {
  11. stack = stacks[_i];
  12. deprecation.addStack(stack, stack.metadata);
  13. }
  14. return deprecation;
  15. };
  16. function Deprecation(message, fileName, lineNumber) {
  17. this.message = message;
  18. this.fileName = fileName;
  19. this.lineNumber = lineNumber;
  20. this.callCount = 0;
  21. this.stackCount = 0;
  22. this.stacks = {};
  23. this.stackCallCounts = {};
  24. }
  25. Deprecation.prototype.getFunctionNameFromCallsite = function(callsite) {
  26. var _ref, _ref1, _ref2;
  27. if (callsite.functionName != null) {
  28. return callsite.functionName;
  29. }
  30. if (callsite.isToplevel()) {
  31. return (_ref = callsite.getFunctionName()) != null ? _ref : '<unknown>';
  32. } else {
  33. if (callsite.isConstructor()) {
  34. return "new " + (callsite.getFunctionName());
  35. } else if (callsite.getMethodName() && !callsite.getFunctionName()) {
  36. return callsite.getMethodName();
  37. } else {
  38. return "" + (callsite.getTypeName()) + "." + ((_ref1 = (_ref2 = callsite.getMethodName()) != null ? _ref2 : callsite.getFunctionName()) != null ? _ref1 : '<anonymous>');
  39. }
  40. }
  41. };
  42. Deprecation.prototype.getLocationFromCallsite = function(callsite) {
  43. var column, fileName, line;
  44. if (callsite.location != null) {
  45. return callsite.location;
  46. }
  47. if (callsite.isNative()) {
  48. return "native";
  49. } else if (callsite.isEval()) {
  50. return "eval at " + (this.getLocationFromCallsite(callsite.getEvalOrigin()));
  51. } else {
  52. fileName = callsite.getFileName();
  53. line = callsite.getLineNumber();
  54. column = callsite.getColumnNumber();
  55. return "" + fileName + ":" + line + ":" + column;
  56. }
  57. };
  58. Deprecation.prototype.getFileNameFromCallSite = function(callsite) {
  59. var _ref;
  60. return (_ref = callsite.fileName) != null ? _ref : callsite.getFileName();
  61. };
  62. Deprecation.prototype.getOriginName = function() {
  63. return this.originName;
  64. };
  65. Deprecation.prototype.getMessage = function() {
  66. return this.message;
  67. };
  68. Deprecation.prototype.getStacks = function() {
  69. var location, parsedStack, parsedStacks, stack, _ref;
  70. parsedStacks = [];
  71. _ref = this.stacks;
  72. for (location in _ref) {
  73. stack = _ref[location];
  74. parsedStack = this.parseStack(stack);
  75. parsedStack.callCount = this.stackCallCounts[location];
  76. parsedStack.metadata = stack.metadata;
  77. parsedStacks.push(parsedStack);
  78. }
  79. return parsedStacks;
  80. };
  81. Deprecation.prototype.getStackCount = function() {
  82. return this.stackCount;
  83. };
  84. Deprecation.prototype.getCallCount = function() {
  85. return this.callCount;
  86. };
  87. Deprecation.prototype.addStack = function(stack, metadata) {
  88. var callerLocation, _base, _base1;
  89. if (this.originName == null) {
  90. this.originName = this.getFunctionNameFromCallsite(stack[0]);
  91. }
  92. if (this.fileName == null) {
  93. this.fileName = this.getFileNameFromCallSite(stack[0]);
  94. }
  95. if (this.lineNumber == null) {
  96. this.lineNumber = typeof (_base = stack[0]).getLineNumber === "function" ? _base.getLineNumber() : void 0;
  97. }
  98. this.callCount++;
  99. stack.metadata = metadata;
  100. callerLocation = this.getLocationFromCallsite(stack[1]);
  101. if (this.stacks[callerLocation] == null) {
  102. this.stacks[callerLocation] = stack;
  103. this.stackCount++;
  104. }
  105. if ((_base1 = this.stackCallCounts)[callerLocation] == null) {
  106. _base1[callerLocation] = 0;
  107. }
  108. return this.stackCallCounts[callerLocation]++;
  109. };
  110. Deprecation.prototype.parseStack = function(stack) {
  111. return stack.map((function(_this) {
  112. return function(callsite) {
  113. return {
  114. functionName: _this.getFunctionNameFromCallsite(callsite),
  115. location: _this.getLocationFromCallsite(callsite),
  116. fileName: _this.getFileNameFromCallSite(callsite)
  117. };
  118. };
  119. })(this));
  120. };
  121. Deprecation.prototype.serialize = function() {
  122. return {
  123. message: this.getMessage(),
  124. lineNumber: this.lineNumber,
  125. fileName: this.fileName,
  126. stacks: this.getStacks()
  127. };
  128. };
  129. return Deprecation;
  130. })();
  131. }).call(this);