source: imaps-frontend/node_modules/webpack/lib/util/deprecation.js

main
Last change on this file was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 8.1 KB
Line 
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const util = require("util");
9
10/** @type {Map<string, Function>} */
11const deprecationCache = new Map();
12
13/**
14 * @typedef {object} FakeHookMarker
15 * @property {true} _fakeHook it's a fake hook
16 */
17
18/**
19 * @template T
20 * @typedef {T & FakeHookMarker} FakeHook<T>
21 */
22
23/**
24 * @param {string} message deprecation message
25 * @param {string} code deprecation code
26 * @returns {Function} function to trigger deprecation
27 */
28const createDeprecation = (message, code) => {
29 const cached = deprecationCache.get(message);
30 if (cached !== undefined) return cached;
31 const fn = util.deprecate(
32 () => {},
33 message,
34 `DEP_WEBPACK_DEPRECATION_${code}`
35 );
36 deprecationCache.set(message, fn);
37 return fn;
38};
39
40const COPY_METHODS = [
41 "concat",
42 "entry",
43 "filter",
44 "find",
45 "findIndex",
46 "includes",
47 "indexOf",
48 "join",
49 "lastIndexOf",
50 "map",
51 "reduce",
52 "reduceRight",
53 "slice",
54 "some"
55];
56
57const DISABLED_METHODS = [
58 "copyWithin",
59 "entries",
60 "fill",
61 "keys",
62 "pop",
63 "reverse",
64 "shift",
65 "splice",
66 "sort",
67 "unshift"
68];
69
70/**
71 * @param {any} set new set
72 * @param {string} name property name
73 * @returns {void}
74 */
75module.exports.arrayToSetDeprecation = (set, name) => {
76 for (const method of COPY_METHODS) {
77 if (set[method]) continue;
78 const d = createDeprecation(
79 `${name} was changed from Array to Set (using Array method '${method}' is deprecated)`,
80 "ARRAY_TO_SET"
81 );
82 /**
83 * @deprecated
84 * @this {Set<any>}
85 * @returns {number} count
86 */
87 set[method] = function () {
88 d();
89 const array = Array.from(this);
90 return Array.prototype[/** @type {keyof COPY_METHODS} */ (method)].apply(
91 array,
92 // eslint-disable-next-line prefer-rest-params
93 arguments
94 );
95 };
96 }
97 const dPush = createDeprecation(
98 `${name} was changed from Array to Set (using Array method 'push' is deprecated)`,
99 "ARRAY_TO_SET_PUSH"
100 );
101 const dLength = createDeprecation(
102 `${name} was changed from Array to Set (using Array property 'length' is deprecated)`,
103 "ARRAY_TO_SET_LENGTH"
104 );
105 const dIndexer = createDeprecation(
106 `${name} was changed from Array to Set (indexing Array is deprecated)`,
107 "ARRAY_TO_SET_INDEXER"
108 );
109 /**
110 * @deprecated
111 * @this {Set<any>}
112 * @returns {number} count
113 */
114 set.push = function () {
115 dPush();
116 // eslint-disable-next-line prefer-rest-params
117 for (const item of Array.from(arguments)) {
118 this.add(item);
119 }
120 return this.size;
121 };
122 for (const method of DISABLED_METHODS) {
123 if (set[method]) continue;
124 set[method] = () => {
125 throw new Error(
126 `${name} was changed from Array to Set (using Array method '${method}' is not possible)`
127 );
128 };
129 }
130 /**
131 * @param {number} index index
132 * @returns {any} value
133 */
134 const createIndexGetter = index => {
135 /**
136 * @this {Set<any>} a Set
137 * @returns {any} the value at this location
138 */
139 // eslint-disable-next-line func-style
140 const fn = function () {
141 dIndexer();
142 let i = 0;
143 for (const item of this) {
144 if (i++ === index) return item;
145 }
146 };
147 return fn;
148 };
149 /**
150 * @param {number} index index
151 */
152 const defineIndexGetter = index => {
153 Object.defineProperty(set, index, {
154 get: createIndexGetter(index),
155 set(value) {
156 throw new Error(
157 `${name} was changed from Array to Set (indexing Array with write is not possible)`
158 );
159 }
160 });
161 };
162 defineIndexGetter(0);
163 let indexerDefined = 1;
164 Object.defineProperty(set, "length", {
165 get() {
166 dLength();
167 const length = this.size;
168 for (indexerDefined; indexerDefined < length + 1; indexerDefined++) {
169 defineIndexGetter(indexerDefined);
170 }
171 return length;
172 },
173 set(value) {
174 throw new Error(
175 `${name} was changed from Array to Set (writing to Array property 'length' is not possible)`
176 );
177 }
178 });
179 set[Symbol.isConcatSpreadable] = true;
180};
181
182/**
183 * @template T
184 * @param {string} name name
185 * @returns {{ new <T = any>(values?: readonly T[] | null): SetDeprecatedArray<T> }} SetDeprecatedArray
186 */
187module.exports.createArrayToSetDeprecationSet = name => {
188 let initialized = false;
189
190 /**
191 * @template T
192 */
193 class SetDeprecatedArray extends Set {
194 /**
195 * @param {readonly T[] | null=} items items
196 */
197 constructor(items) {
198 super(items);
199 if (!initialized) {
200 initialized = true;
201 module.exports.arrayToSetDeprecation(
202 SetDeprecatedArray.prototype,
203 name
204 );
205 }
206 }
207 }
208 return SetDeprecatedArray;
209};
210
211/**
212 * @template {object} T
213 * @param {T} obj object
214 * @param {string} name property name
215 * @param {string} code deprecation code
216 * @param {string} note additional note
217 * @returns {Proxy<T>} frozen object with deprecation when modifying
218 */
219module.exports.soonFrozenObjectDeprecation = (obj, name, code, note = "") => {
220 const message = `${name} will be frozen in future, all modifications are deprecated.${
221 note && `\n${note}`
222 }`;
223 return /** @type {Proxy<T>} */ (
224 new Proxy(/** @type {object} */ (obj), {
225 set: util.deprecate(
226 /**
227 * @param {T} target target
228 * @param {string | symbol} property property
229 * @param {any} value value
230 * @param {any} receiver receiver
231 * @returns {boolean} result
232 */
233 (target, property, value, receiver) =>
234 Reflect.set(
235 /** @type {object} */ (target),
236 property,
237 value,
238 receiver
239 ),
240 message,
241 code
242 ),
243 defineProperty: util.deprecate(
244 /**
245 * @param {T} target target
246 * @param {string | symbol} property property
247 * @param {PropertyDescriptor} descriptor descriptor
248 * @returns {boolean} result
249 */
250 (target, property, descriptor) =>
251 Reflect.defineProperty(
252 /** @type {object} */ (target),
253 property,
254 descriptor
255 ),
256 message,
257 code
258 ),
259 deleteProperty: util.deprecate(
260 /**
261 * @param {T} target target
262 * @param {string | symbol} property property
263 * @returns {boolean} result
264 */
265 (target, property) =>
266 Reflect.deleteProperty(/** @type {object} */ (target), property),
267 message,
268 code
269 ),
270 setPrototypeOf: util.deprecate(
271 /**
272 * @param {T} target target
273 * @param {object | null} proto proto
274 * @returns {boolean} result
275 */
276 (target, proto) =>
277 Reflect.setPrototypeOf(/** @type {object} */ (target), proto),
278 message,
279 code
280 )
281 })
282 );
283};
284
285/**
286 * @template T
287 * @param {T} obj object
288 * @param {string} message deprecation message
289 * @param {string} code deprecation code
290 * @returns {T} object with property access deprecated
291 */
292const deprecateAllProperties = (obj, message, code) => {
293 const newObj = {};
294 const descriptors = Object.getOwnPropertyDescriptors(obj);
295 for (const name of Object.keys(descriptors)) {
296 const descriptor = descriptors[name];
297 if (typeof descriptor.value === "function") {
298 Object.defineProperty(newObj, name, {
299 ...descriptor,
300 value: util.deprecate(descriptor.value, message, code)
301 });
302 } else if (descriptor.get || descriptor.set) {
303 Object.defineProperty(newObj, name, {
304 ...descriptor,
305 get: descriptor.get && util.deprecate(descriptor.get, message, code),
306 set: descriptor.set && util.deprecate(descriptor.set, message, code)
307 });
308 } else {
309 let value = descriptor.value;
310 Object.defineProperty(newObj, name, {
311 configurable: descriptor.configurable,
312 enumerable: descriptor.enumerable,
313 get: util.deprecate(() => value, message, code),
314 set: descriptor.writable
315 ? util.deprecate(
316 /**
317 * @template T
318 * @param {T} v value
319 * @returns {T} result
320 */
321 v => (value = v),
322 message,
323 code
324 )
325 : undefined
326 });
327 }
328 }
329 return /** @type {T} */ (newObj);
330};
331module.exports.deprecateAllProperties = deprecateAllProperties;
332
333/**
334 * @template {object} T
335 * @param {T} fakeHook fake hook implementation
336 * @param {string=} message deprecation message (not deprecated when unset)
337 * @param {string=} code deprecation code (not deprecated when unset)
338 * @returns {FakeHook<T>} fake hook which redirects
339 */
340module.exports.createFakeHook = (fakeHook, message, code) => {
341 if (message && code) {
342 fakeHook = deprecateAllProperties(fakeHook, message, code);
343 }
344 return Object.freeze(
345 Object.assign(fakeHook, { _fakeHook: /** @type {true} */ (true) })
346 );
347};
Note: See TracBrowser for help on using the repository browser.