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