-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
Copy pathdeprecate.ts
270 lines (229 loc) · 9.11 KB
/
deprecate.ts
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
import { ENV } from '@ember/-internals/environment';
import { DEBUG } from '@glimmer/env';
import { assert } from './assert';
import type { HandlerCallback } from './handlers';
import { invoke, registerHandler as genericRegisterHandler } from './handlers';
// This is a "global", but instead of declaring it as `declare global`, which
// will expose it to all other modules, declare it *locally* (and don't export
// it) so that it has the desired "private global" semantics -- however odd that
// particular notion is.
declare const __fail__: {
fail(): void;
};
interface Available {
available: string;
}
interface Enabled extends Available {
enabled: string;
}
export interface DeprecationOptions {
id: string;
until: string;
url?: string;
for: string;
since: Available | Enabled;
}
export type DeprecateFunc = (message: string, test?: boolean, options?: DeprecationOptions) => void;
export type MissingOptionDeprecateFunc = (id: string, missingOption: string) => string;
/**
@module @ember/debug
@public
*/
/**
Allows for runtime registration of handler functions that override the default deprecation behavior.
Deprecations are invoked by calls to [@ember/debug/deprecate](/ember/release/classes/@ember%2Fdebug/methods/deprecate?anchor=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
import { registerDeprecationHandler } from '@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: (handler: HandlerCallback<DeprecationOptions>) => void = () => {};
let missingOptionsDeprecation: string;
let missingOptionsIdDeprecation: string;
let missingOptionDeprecation: MissingOptionDeprecateFunc = () => '';
let deprecate: DeprecateFunc = () => {};
if (DEBUG) {
registerHandler = function registerHandler(handler: HandlerCallback<DeprecationOptions>): void {
genericRegisterHandler('deprecate', handler);
};
let formatMessage = function formatMessage(_message: string, options?: DeprecationOptions) {
let message = _message;
if (options?.id) {
message = message + ` [deprecation id: ${options.id}]`;
}
if (options?.until) {
message = message + ` This will be removed in ${options.for} ${options.until}.`;
}
if (options?.url) {
message += ` See ${options.url} for more details.`;
}
return message;
};
registerHandler(function logDeprecationToConsole(message, options) {
let updatedMessage = formatMessage(message, options);
console.warn(`DEPRECATION: ${updatedMessage}`); // eslint-disable-line no-console
});
let captureErrorForStack: () => unknown;
if (new Error().stack) {
captureErrorForStack = () => new Error();
} else {
captureErrorForStack = () => {
try {
__fail__.fail();
return;
} 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 instanceof Error) {
if (error.stack) {
if ((error as any)['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);
console.warn(`DEPRECATION: ${updatedMessage}${stackStr}`); // eslint-disable-line no-console
} else {
next(message, options);
}
});
registerHandler(function raiseOnDeprecation(message, options, next) {
if (ENV.RAISE_ON_DEPRECATION) {
let updatedMessage = formatMessage(message);
throw new Error(updatedMessage);
} else {
next(message, options);
}
});
missingOptionsDeprecation =
'When calling `deprecate` you ' +
'must provide an `options` hash as the third parameter. ' +
'`options` should include `id` and `until` properties.';
missingOptionsIdDeprecation = 'When calling `deprecate` you must provide `id` in options.';
missingOptionDeprecation = (id: string, missingOption: string): string => {
return `When calling \`deprecate\` you must provide \`${missingOption}\` in options. Missing options.${missingOption} in "${id}" deprecation`;
};
/**
@module @ember/debug
@public
*/
/**
Display a deprecation warning with the provided message and a stack trace
(Chrome and Firefox only).
Ember itself leverages [Semantic Versioning](https://semver.org) to aid
projects in keeping up with changes to the framework. Before any
functionality or API is removed, it first flows linearly through a
deprecation staging process. The staging process currently contains two
stages: available and enabled.
Deprecations are initially released into the 'available' stage.
Deprecations will stay in this stage until the replacement API has been
marked as a recommended practice via the RFC process and the addon
ecosystem has generally adopted the change.
Once a deprecation meets the above criteria, it will move into the
'enabled' stage where it will remain until the functionality or API is
eventually removed.
For application and addon developers, "available" deprecations are not
urgent and "enabled" deprecations require action.
* 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.
```javascript
import { deprecate } from '@ember/debug';
deprecate(
'Use of `assign` has been deprecated. Please use `Object.assign` or the spread operator instead.',
false,
{
id: 'ember-polyfills.deprecate-assign',
until: '5.0.0',
url: 'https://deprecations.emberjs.com/v4.x/#toc_ember-polyfills-deprecate-assign',
for: 'ember-source',
since: {
available: '4.0.0',
enabled: '4.0.0',
},
}
);
```
@method deprecate
@for @ember/debug
@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.for A namespace for the deprecation, usually the package name
@param {Object} options.since Describes when the deprecation became available and enabled.
@param {String} [options.url] An optional url to the transition guide on the
emberjs.com website.
@static
@public
@since 1.0.0
*/
deprecate = function deprecate(
message: string,
test?: boolean,
options?: DeprecationOptions
): void {
assert(missingOptionsDeprecation, Boolean(options && (options.id || options.until)));
assert(missingOptionsIdDeprecation, Boolean(options!.id));
assert(missingOptionDeprecation(options!.id, 'until'), Boolean(options!.until));
assert(missingOptionDeprecation(options!.id, 'for'), Boolean(options!.for));
assert(missingOptionDeprecation(options!.id, 'since'), Boolean(options!.since));
invoke('deprecate', message, test, options);
};
}
export default deprecate;
export {
registerHandler,
missingOptionsDeprecation,
missingOptionsIdDeprecation,
missingOptionDeprecation,
};