| 
									
										
										
										
											2018-09-06 22:57:32 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const util = require("util"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-12 19:07:36 +08:00
										 |  |  | /** @type {Map<string, Function>} */ | 
					
						
							|  |  |  | const deprecationCache = new Map(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * @param {string} message deprecation message | 
					
						
							|  |  |  |  * @returns {Function} function to trigger deprecation | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | const createDeprecation = message => { | 
					
						
							|  |  |  | 	const cached = deprecationCache.get(message); | 
					
						
							|  |  |  | 	if (cached !== undefined) return cached; | 
					
						
							|  |  |  | 	const fn = util.deprecate(() => {}, message); | 
					
						
							|  |  |  | 	deprecationCache.set(message, fn); | 
					
						
							|  |  |  | 	return fn; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-06 22:57:32 +08:00
										 |  |  | const COPY_METHODS = [ | 
					
						
							|  |  |  | 	"concat", | 
					
						
							|  |  |  | 	"entry", | 
					
						
							|  |  |  | 	"filter", | 
					
						
							|  |  |  | 	"find", | 
					
						
							|  |  |  | 	"findIndex", | 
					
						
							|  |  |  | 	"includes", | 
					
						
							|  |  |  | 	"indexOf", | 
					
						
							|  |  |  | 	"join", | 
					
						
							|  |  |  | 	"lastIndexOf", | 
					
						
							|  |  |  | 	"map", | 
					
						
							|  |  |  | 	"reduce", | 
					
						
							|  |  |  | 	"reduceRight", | 
					
						
							|  |  |  | 	"slice", | 
					
						
							|  |  |  | 	"some" | 
					
						
							|  |  |  | ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const DISABLED_METHODS = [ | 
					
						
							|  |  |  | 	"copyWithin", | 
					
						
							|  |  |  | 	"entries", | 
					
						
							|  |  |  | 	"fill", | 
					
						
							|  |  |  | 	"keys", | 
					
						
							|  |  |  | 	"pop", | 
					
						
							|  |  |  | 	"reverse", | 
					
						
							|  |  |  | 	"shift", | 
					
						
							|  |  |  | 	"splice", | 
					
						
							|  |  |  | 	"sort", | 
					
						
							|  |  |  | 	"unshift" | 
					
						
							|  |  |  | ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * @param {any} set new set | 
					
						
							|  |  |  |  * @param {string} name property name | 
					
						
							|  |  |  |  * @returns {void} | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | exports.arrayToSetDeprecation = (set, name) => { | 
					
						
							|  |  |  | 	for (const method of COPY_METHODS) { | 
					
						
							|  |  |  | 		if (set[method]) continue; | 
					
						
							| 
									
										
										
										
											2018-09-12 19:07:36 +08:00
										 |  |  | 		const d = createDeprecation( | 
					
						
							|  |  |  | 			`${name} was changed from Array to Set (using Array method '${method}' is deprecated)` | 
					
						
							| 
									
										
										
										
											2018-09-06 22:57:32 +08:00
										 |  |  | 		); | 
					
						
							|  |  |  | 		/** | 
					
						
							|  |  |  | 		 * @deprecated | 
					
						
							|  |  |  | 		 * @this {Set} | 
					
						
							|  |  |  | 		 * @returns {number} count | 
					
						
							|  |  |  | 		 */ | 
					
						
							| 
									
										
										
										
											2018-09-12 19:07:36 +08:00
										 |  |  | 		set[method] = function() { | 
					
						
							|  |  |  | 			d(); | 
					
						
							|  |  |  | 			const array = Array.from(this); | 
					
						
							|  |  |  | 			return Array.prototype[method].apply(array, arguments); | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	const dPush = createDeprecation( | 
					
						
							|  |  |  | 		`${name} was changed from Array to Set (using Array method 'push' is deprecated)` | 
					
						
							| 
									
										
										
										
											2018-09-06 22:57:32 +08:00
										 |  |  | 	); | 
					
						
							| 
									
										
										
										
											2018-09-12 19:07:36 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @deprecated | 
					
						
							|  |  |  | 	 * @this {Set} | 
					
						
							|  |  |  | 	 * @returns {number} count | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	set.push = function() { | 
					
						
							|  |  |  | 		dPush(); | 
					
						
							|  |  |  | 		for (const item of Array.from(arguments)) { | 
					
						
							|  |  |  | 			this.add(item); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return this.size; | 
					
						
							|  |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2018-09-06 22:57:32 +08:00
										 |  |  | 	for (const method of DISABLED_METHODS) { | 
					
						
							|  |  |  | 		if (set[method]) continue; | 
					
						
							|  |  |  | 		set[method] = () => { | 
					
						
							|  |  |  | 			throw new Error( | 
					
						
							| 
									
										
										
										
											2018-09-12 19:07:36 +08:00
										 |  |  | 				`${name} was changed from Array to Set (using Array method '${method}' is not possible)` | 
					
						
							| 
									
										
										
										
											2018-09-06 22:57:32 +08:00
										 |  |  | 			); | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	Object.defineProperty(set, "length", { | 
					
						
							|  |  |  | 		get() { | 
					
						
							|  |  |  | 			return set.size; | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		set(value) { | 
					
						
							|  |  |  | 			throw new Error( | 
					
						
							| 
									
										
										
										
											2018-09-12 19:07:36 +08:00
										 |  |  | 				`${name} was changed from Array to Set (writing to Array property 'length' is not possible)` | 
					
						
							| 
									
										
										
										
											2018-09-06 22:57:32 +08:00
										 |  |  | 			); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | }; |