deprecation.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const util = require("util");
  7. /** @type {Map<string, Function>} */
  8. const deprecationCache = new Map();
  9. /**
  10. * @typedef {Object} FakeHookMarker
  11. * @property {true} _fakeHook it's a fake hook
  12. */
  13. /** @template T @typedef {T & FakeHookMarker} FakeHook<T> */
  14. /**
  15. * @param {string} message deprecation message
  16. * @param {string} code deprecation code
  17. * @returns {Function} function to trigger deprecation
  18. */
  19. const createDeprecation = (message, code) => {
  20. const cached = deprecationCache.get(message);
  21. if (cached !== undefined) return cached;
  22. const fn = util.deprecate(
  23. () => {},
  24. message,
  25. "DEP_WEBPACK_DEPRECATION_" + code
  26. );
  27. deprecationCache.set(message, fn);
  28. return fn;
  29. };
  30. const COPY_METHODS = [
  31. "concat",
  32. "entry",
  33. "filter",
  34. "find",
  35. "findIndex",
  36. "includes",
  37. "indexOf",
  38. "join",
  39. "lastIndexOf",
  40. "map",
  41. "reduce",
  42. "reduceRight",
  43. "slice",
  44. "some"
  45. ];
  46. const DISABLED_METHODS = [
  47. "copyWithin",
  48. "entries",
  49. "fill",
  50. "keys",
  51. "pop",
  52. "reverse",
  53. "shift",
  54. "splice",
  55. "sort",
  56. "unshift"
  57. ];
  58. /**
  59. * @param {any} set new set
  60. * @param {string} name property name
  61. * @returns {void}
  62. */
  63. exports.arrayToSetDeprecation = (set, name) => {
  64. for (const method of COPY_METHODS) {
  65. if (set[method]) continue;
  66. const d = createDeprecation(
  67. `${name} was changed from Array to Set (using Array method '${method}' is deprecated)`,
  68. "ARRAY_TO_SET"
  69. );
  70. /**
  71. * @deprecated
  72. * @this {Set}
  73. * @returns {number} count
  74. */
  75. set[method] = function () {
  76. d();
  77. const array = Array.from(this);
  78. return Array.prototype[method].apply(array, arguments);
  79. };
  80. }
  81. const dPush = createDeprecation(
  82. `${name} was changed from Array to Set (using Array method 'push' is deprecated)`,
  83. "ARRAY_TO_SET_PUSH"
  84. );
  85. const dLength = createDeprecation(
  86. `${name} was changed from Array to Set (using Array property 'length' is deprecated)`,
  87. "ARRAY_TO_SET_LENGTH"
  88. );
  89. const dIndexer = createDeprecation(
  90. `${name} was changed from Array to Set (indexing Array is deprecated)`,
  91. "ARRAY_TO_SET_INDEXER"
  92. );
  93. /**
  94. * @deprecated
  95. * @this {Set}
  96. * @returns {number} count
  97. */
  98. set.push = function () {
  99. dPush();
  100. for (const item of Array.from(arguments)) {
  101. this.add(item);
  102. }
  103. return this.size;
  104. };
  105. for (const method of DISABLED_METHODS) {
  106. if (set[method]) continue;
  107. set[method] = () => {
  108. throw new Error(
  109. `${name} was changed from Array to Set (using Array method '${method}' is not possible)`
  110. );
  111. };
  112. }
  113. const createIndexGetter = index => {
  114. /**
  115. * @this {Set} a Set
  116. * @returns {any} the value at this location
  117. */
  118. const fn = function () {
  119. dIndexer();
  120. let i = 0;
  121. for (const item of this) {
  122. if (i++ === index) return item;
  123. }
  124. return undefined;
  125. };
  126. return fn;
  127. };
  128. const defineIndexGetter = index => {
  129. Object.defineProperty(set, index, {
  130. get: createIndexGetter(index),
  131. set(value) {
  132. throw new Error(
  133. `${name} was changed from Array to Set (indexing Array with write is not possible)`
  134. );
  135. }
  136. });
  137. };
  138. defineIndexGetter(0);
  139. let indexerDefined = 1;
  140. Object.defineProperty(set, "length", {
  141. get() {
  142. dLength();
  143. const length = this.size;
  144. for (indexerDefined; indexerDefined < length + 1; indexerDefined++) {
  145. defineIndexGetter(indexerDefined);
  146. }
  147. return length;
  148. },
  149. set(value) {
  150. throw new Error(
  151. `${name} was changed from Array to Set (writing to Array property 'length' is not possible)`
  152. );
  153. }
  154. });
  155. set[Symbol.isConcatSpreadable] = true;
  156. };
  157. exports.createArrayToSetDeprecationSet = name => {
  158. let initialized = false;
  159. class SetDeprecatedArray extends Set {
  160. constructor(items) {
  161. super(items);
  162. if (!initialized) {
  163. initialized = true;
  164. exports.arrayToSetDeprecation(SetDeprecatedArray.prototype, name);
  165. }
  166. }
  167. }
  168. return SetDeprecatedArray;
  169. };
  170. exports.soonFrozenObjectDeprecation = (obj, name, code, note = "") => {
  171. const message = `${name} will be frozen in future, all modifications are deprecated.${
  172. note && `\n${note}`
  173. }`;
  174. return new Proxy(obj, {
  175. set: util.deprecate(
  176. (target, property, value, receiver) =>
  177. Reflect.set(target, property, value, receiver),
  178. message,
  179. code
  180. ),
  181. defineProperty: util.deprecate(
  182. (target, property, descriptor) =>
  183. Reflect.defineProperty(target, property, descriptor),
  184. message,
  185. code
  186. ),
  187. deleteProperty: util.deprecate(
  188. (target, property) => Reflect.deleteProperty(target, property),
  189. message,
  190. code
  191. ),
  192. setPrototypeOf: util.deprecate(
  193. (target, proto) => Reflect.setPrototypeOf(target, proto),
  194. message,
  195. code
  196. )
  197. });
  198. };
  199. /**
  200. * @template T
  201. * @param {T} obj object
  202. * @param {string} message deprecation message
  203. * @param {string} code deprecation code
  204. * @returns {T} object with property access deprecated
  205. */
  206. const deprecateAllProperties = (obj, message, code) => {
  207. const newObj = {};
  208. const descriptors = Object.getOwnPropertyDescriptors(obj);
  209. for (const name of Object.keys(descriptors)) {
  210. const descriptor = descriptors[name];
  211. if (typeof descriptor.value === "function") {
  212. Object.defineProperty(newObj, name, {
  213. ...descriptor,
  214. value: util.deprecate(descriptor.value, message, code)
  215. });
  216. } else if (descriptor.get || descriptor.set) {
  217. Object.defineProperty(newObj, name, {
  218. ...descriptor,
  219. get: descriptor.get && util.deprecate(descriptor.get, message, code),
  220. set: descriptor.set && util.deprecate(descriptor.set, message, code)
  221. });
  222. } else {
  223. let value = descriptor.value;
  224. Object.defineProperty(newObj, name, {
  225. configurable: descriptor.configurable,
  226. enumerable: descriptor.enumerable,
  227. get: util.deprecate(() => value, message, code),
  228. set: descriptor.writable
  229. ? util.deprecate(v => (value = v), message, code)
  230. : undefined
  231. });
  232. }
  233. }
  234. return /** @type {T} */ (newObj);
  235. };
  236. exports.deprecateAllProperties = deprecateAllProperties;
  237. /**
  238. * @template T
  239. * @param {T} fakeHook fake hook implementation
  240. * @param {string=} message deprecation message (not deprecated when unset)
  241. * @param {string=} code deprecation code (not deprecated when unset)
  242. * @returns {FakeHook<T>} fake hook which redirects
  243. */
  244. exports.createFakeHook = (fakeHook, message, code) => {
  245. if (message && code) {
  246. fakeHook = deprecateAllProperties(fakeHook, message, code);
  247. }
  248. return Object.freeze(
  249. Object.assign(fakeHook, { _fakeHook: /** @type {true} */ (true) })
  250. );
  251. };