index.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. Deprecated Decorator v0.1
  3. https://github.com/vilic/deprecated-decorator
  4. */
  5. "use strict";
  6. /** @internal */
  7. exports.options = {
  8. getWarner: undefined
  9. };
  10. function createWarner(type, name, alternative, version, url) {
  11. var warnedPositions = {};
  12. return function () {
  13. var stack = (new Error()).stack || '';
  14. var at = (stack.match(/(?:\s+at\s.+){2}\s+at\s(.+)/) || [undefined, ''])[1];
  15. if (/\)$/.test(at)) {
  16. at = at.match(/[^(]+(?=\)$)/)[0];
  17. }
  18. else {
  19. at = at.trim();
  20. }
  21. if (at in warnedPositions) {
  22. return;
  23. }
  24. warnedPositions[at] = true;
  25. var message;
  26. switch (type) {
  27. case 'class':
  28. message = 'Class';
  29. break;
  30. case 'property':
  31. message = 'Property';
  32. break;
  33. case 'method':
  34. message = 'Method';
  35. break;
  36. case 'function':
  37. message = 'Function';
  38. break;
  39. }
  40. message += " `" + name + "` has been deprecated";
  41. if (version) {
  42. message += " since version " + version;
  43. }
  44. if (alternative) {
  45. message += ", use `" + alternative + "` instead";
  46. }
  47. message += '.';
  48. if (at) {
  49. message += "\n at " + at;
  50. }
  51. if (url) {
  52. message += "\nCheck out " + url + " for more information.";
  53. }
  54. console.warn(message);
  55. };
  56. }
  57. function decorateProperty(type, name, descriptor, alternative, version, url) {
  58. var warner = (exports.options.getWarner || createWarner)(type, name, alternative, version, url);
  59. descriptor = descriptor || {
  60. writable: true,
  61. enumerable: false,
  62. configurable: true
  63. };
  64. var deprecatedDescriptor = {
  65. enumerable: descriptor.enumerable,
  66. configurable: descriptor.configurable
  67. };
  68. if (descriptor.get || descriptor.set) {
  69. if (descriptor.get) {
  70. deprecatedDescriptor.get = function () {
  71. warner();
  72. return descriptor.get.call(this);
  73. };
  74. }
  75. if (descriptor.set) {
  76. deprecatedDescriptor.set = function (value) {
  77. warner();
  78. return descriptor.set.call(this, value);
  79. };
  80. }
  81. }
  82. else {
  83. var propertyValue_1 = descriptor.value;
  84. deprecatedDescriptor.get = function () {
  85. warner();
  86. return propertyValue_1;
  87. };
  88. if (descriptor.writable) {
  89. deprecatedDescriptor.set = function (value) {
  90. warner();
  91. propertyValue_1 = value;
  92. };
  93. }
  94. }
  95. return deprecatedDescriptor;
  96. }
  97. function decorateFunction(type, target, alternative, version, url) {
  98. var name = target.name;
  99. var warner = (exports.options.getWarner || createWarner)(type, name, alternative, version, url);
  100. var fn = function () {
  101. warner();
  102. return target.apply(this, arguments);
  103. };
  104. for (var _i = 0, _a = Object.getOwnPropertyNames(target); _i < _a.length; _i++) {
  105. var propertyName = _a[_i];
  106. var descriptor = Object.getOwnPropertyDescriptor(target, propertyName);
  107. if (descriptor.writable) {
  108. fn[propertyName] = target[propertyName];
  109. }
  110. else if (descriptor.configurable) {
  111. Object.defineProperty(fn, propertyName, descriptor);
  112. }
  113. }
  114. return fn;
  115. }
  116. function deprecated() {
  117. var args = [];
  118. for (var _i = 0; _i < arguments.length; _i++) {
  119. args[_i - 0] = arguments[_i];
  120. }
  121. var fn = args[args.length - 1];
  122. if (typeof fn === 'function') {
  123. fn = args.pop();
  124. }
  125. else {
  126. fn = undefined;
  127. }
  128. var options = args[0];
  129. var alternative;
  130. var version;
  131. var url;
  132. if (typeof options === 'string') {
  133. alternative = options;
  134. version = args[1];
  135. url = args[2];
  136. }
  137. else if (options) {
  138. (alternative = options.alternative, version = options.version, url = options.url, options);
  139. }
  140. if (fn) {
  141. return decorateFunction('function', fn, alternative, version, url);
  142. }
  143. return function (target, name, descriptor) {
  144. if (typeof name === 'string') {
  145. var type = descriptor && typeof descriptor.value === 'function' ?
  146. 'method' : 'property';
  147. return decorateProperty(type, name, descriptor, alternative, version, url);
  148. }
  149. else if (typeof target === 'function') {
  150. var constructor = decorateFunction('class', target, alternative, version, url);
  151. var className = target.name;
  152. for (var _i = 0, _a = Object.getOwnPropertyNames(constructor); _i < _a.length; _i++) {
  153. var propertyName = _a[_i];
  154. var descriptor_1 = Object.getOwnPropertyDescriptor(constructor, propertyName);
  155. descriptor_1 = decorateProperty('class', className, descriptor_1, alternative, version, url);
  156. if (descriptor_1.writable) {
  157. constructor[propertyName] = target[propertyName];
  158. }
  159. else if (descriptor_1.configurable) {
  160. Object.defineProperty(constructor, propertyName, descriptor_1);
  161. }
  162. }
  163. return constructor;
  164. }
  165. };
  166. }
  167. exports.deprecated = deprecated;
  168. Object.defineProperty(exports, "__esModule", { value: true });
  169. exports.default = deprecated;
  170. //# sourceMappingURL=index.js.map