-
Notifications
You must be signed in to change notification settings - Fork 73
/
deprecate.js
52 lines (50 loc) · 2.25 KB
/
deprecate.js
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
/**
* @module deprecate
*/
/**
* Prints out a deprecation warning to the console.warn with the format:
* `name` is deprecated, use `alternative` instead.
* It can also print out a stack trace with the line numbers.
* @param {String} name - Name of the thing that is deprecated.
* @param {String} alternative - Name of alternative that should be used instead.
* @param {Number} [stackTraceLimit] - depth of the stack trace to print out. Set to falsy value to disable stack.
*/
exports.deprecationWarning = function deprecationWarning(name, alternative, stackTraceLimit) {
var depth;
if (stackTraceLimit) {
depth = Error.stackTraceLimit;
Error.stackTraceLimit = stackTraceLimit;
}
if (typeof console !== "undefined" && typeof console.warn === "function") {
var stack = (stackTraceLimit ? new Error("").stack : "") ;
if(alternative) {
console.warn(name + " is deprecated, use " + alternative + " instead.", stack);
} else {
//name is a complete message
console.warn(name, stack);
}
}
if (stackTraceLimit) {
Error.stackTraceLimit = depth;
}
}
/**
* Provides a function that can replace a method that has been deprecated.
* Prints out a deprecation warning to the console.warn with the format:
* `name` is deprecated, use `alternative` instead.
* It will also print out a stack trace with the line numbers.
* @param {Object} scope - The object that will be used as the `this` when the `deprecatedFunction` is applied.
* @param {Function} deprecatedFunction - The function object that is deprecated.
* @param {String} name - Name of the method that is deprecated.
* @param {String} alternative - Name of alternative method that should be used instead.
* @returns {Function} deprecationWrapper
*/
exports.deprecateMethod = function deprecate(scope, deprecatedFunction, name, alternative) {
var deprecationWrapper = function () {
// stackTraceLimit = 3 // deprecationWarning + deprecate + caller of the deprecated method
exports.deprecationWarning(name, alternative, 3);
return deprecatedFunction.apply(scope ? scope : this, arguments);
};
deprecationWrapper.deprecatedFunction = deprecatedFunction;
return deprecationWrapper;
}