-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
Copy pathdeprecate.js
207 lines (173 loc) · 6.49 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
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
/*global __fail__*/
import { DEBUG } from 'ember-env-flags';
import EmberError from './error';
import Logger from 'ember-console';
import { ENV } from 'ember-environment';
import { registerHandler as genericRegisterHandler, invoke } from './handlers';
/**
@module ember
@submodule ember-debug
*/
/**
Allows for runtime registration of handler functions that override the default deprecation behavior.
Deprecations are invoked by calls to [Ember.deprecate](https://emberjs.com/api/classes/Ember.html#method_deprecate).
The following example demonstrates its usage by registering a handler that throws an error if the
message contains the word "should", otherwise defers to the default handler.
```javascript
Ember.Debug.registerDeprecationHandler((message, options, next) => {
if (message.indexOf('should') !== -1) {
throw new Error(`Deprecation message with should: ${message}`);
} else {
// defer to whatever handler was registered before this one
next(message, options);
}
});
```
The handler function takes the following arguments:
<ul>
<li> <code>message</code> - The message received from the deprecation call.</li>
<li> <code>options</code> - An object passed in with the deprecation call containing additional information including:</li>
<ul>
<li> <code>id</code> - An id of the deprecation in the form of <code>package-name.specific-deprecation</code>.</li>
<li> <code>until</code> - The Ember version number the feature and deprecation will be removed in.</li>
</ul>
<li> <code>next</code> - A function that calls into the previously registered handler.</li>
</ul>
@public
@static
@method registerDeprecationHandler
@for Ember.Debug
@param handler {Function} A function to handle deprecation calls.
@since 2.1.0
*/
let registerHandler = () => {};
let missingOptionsDeprecation, missingOptionsIdDeprecation, missingOptionsUntilDeprecation, deprecate;
if (DEBUG) {
registerHandler = function registerHandler(handler) {
genericRegisterHandler('deprecate', handler);
}
let formatMessage = function formatMessage(_message, options) {
let message = _message;
if (options && options.id) {
message = message + ` [deprecation id: ${options.id}]`;
}
if (options && options.url) {
message += ` See ${options.url} for more details.`;
}
return message;
}
registerHandler(function logDeprecationToConsole(message, options) {
let updatedMessage = formatMessage(message, options);
Logger.warn(`DEPRECATION: ${updatedMessage}`);
});
let captureErrorForStack;
if (new Error().stack) {
captureErrorForStack = () => new Error();
} else {
captureErrorForStack = () => {
try { __fail__.fail(); } catch (e) { return e; }
};
}
registerHandler(function logDeprecationStackTrace(message, options, next) {
if (ENV.LOG_STACKTRACE_ON_DEPRECATION) {
let stackStr = '';
let error = captureErrorForStack();
let stack;
if (error.stack) {
if (error['arguments']) {
// Chrome
stack = error.stack.replace(/^\s+at\s+/gm, '').
replace(/^([^\(]+?)([\n$])/gm, '{anonymous}($1)$2').
replace(/^Object.<anonymous>\s*\(([^\)]+)\)/gm, '{anonymous}($1)').split('\n');
stack.shift();
} else {
// Firefox
stack = error.stack.replace(/(?:\n@:0)?\s+$/m, '').
replace(/^\(/gm, '{anonymous}(').split('\n');
}
stackStr = `\n ${stack.slice(2).join('\n ')}`;
}
let updatedMessage = formatMessage(message, options);
Logger.warn(`DEPRECATION: ${updatedMessage}${stackStr}`);
} else {
next(...arguments);
}
});
registerHandler(function raiseOnDeprecation(message, options, next) {
if (ENV.RAISE_ON_DEPRECATION) {
let updatedMessage = formatMessage(message);
throw new EmberError(updatedMessage);
} else {
next(...arguments);
}
});
missingOptionsDeprecation = 'When calling `Ember.deprecate` you ' +
'must provide an `options` hash as the third parameter. ' +
'`options` should include `id` and `until` properties.';
missingOptionsIdDeprecation = 'When calling `Ember.deprecate` you must provide `id` in options.';
missingOptionsUntilDeprecation = 'When calling `Ember.deprecate` you must provide `until` in options.';
/**
Display a deprecation warning with the provided message and a stack trace
(Chrome and Firefox only).
* In a production build, this method is defined as an empty function (NOP).
Uses of this method in Ember itself are stripped from the ember.prod.js build.
@method deprecate
@param {String} message A description of the deprecation.
@param {Boolean} test A boolean. If falsy, the deprecation will be displayed.
@param {Object} options
@param {String} options.id A unique id for this deprecation. The id can be
used by Ember debugging tools to change the behavior (raise, log or silence)
for that specific deprecation. The id should be namespaced by dots, e.g.
"view.helper.select".
@param {string} options.until The version of Ember when this deprecation
warning will be removed.
@param {String} [options.url] An optional url to the transition guide on the
emberjs.com website.
@for Ember
@public
@since 1.0.0
*/
deprecate = function deprecate(message, test, options) {
if (!options || (!options.id && !options.until)) {
deprecate(
missingOptionsDeprecation,
false,
{
id: 'ember-debug.deprecate-options-missing',
until: '3.0.0',
url: 'https://emberjs.com/deprecations/v2.x/#toc_ember-debug-function-options'
}
);
}
if (options && !options.id) {
deprecate(
missingOptionsIdDeprecation,
false,
{
id: 'ember-debug.deprecate-id-missing',
until: '3.0.0',
url: 'https://emberjs.com/deprecations/v2.x/#toc_ember-debug-function-options'
}
);
}
if (options && !options.until) {
deprecate(
missingOptionsUntilDeprecation,
options && options.until,
{
id: 'ember-debug.deprecate-until-missing',
until: '3.0.0',
url: 'https://emberjs.com/deprecations/v2.x/#toc_ember-debug-function-options'
}
);
}
invoke('deprecate', ...arguments);
}
}
export default deprecate;
export {
registerHandler,
missingOptionsDeprecation,
missingOptionsIdDeprecation,
missingOptionsUntilDeprecation
}