-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
Copy pathdeprecate.ts
249 lines (212 loc) · 8.25 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
import { ENV } from '@ember/-internals/environment';
import { DEBUG } from '@glimmer/env';
import { assert } from '../index';
import { HandlerCallback, invoke, registerHandler as genericRegisterHandler } from './handlers';
declare global {
const __fail__: {
fail(): void;
};
}
export type DeprecationStages = 'available' | 'enabled';
export interface DeprecationOptions {
id: string;
until: string;
url?: string;
for: string;
since: Partial<Record<DeprecationStages, string>>;
}
export type DeprecateFunc = (message: string, test?: boolean, options?: DeprecationOptions) => void;
export type MissingOptionDeprecateFunc = (id: 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) => void = () => {};
let missingOptionsDeprecation: string;
let missingOptionsIdDeprecation: string;
let missingOptionsUntilDeprecation: string;
let missingOptionsForDeprecation: MissingOptionDeprecateFunc = () => '';
let missingOptionsSinceDeprecation: MissingOptionDeprecateFunc = () => '';
let deprecate: DeprecateFunc = () => {};
let FOR_MISSING_DEPRECATIONS = new Set();
let SINCE_MISSING_DEPRECATIONS = new Set();
if (DEBUG) {
registerHandler = function registerHandler(handler: HandlerCallback) {
genericRegisterHandler('deprecate', handler);
};
let formatMessage = function formatMessage(_message: string, options?: DeprecationOptions) {
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);
console.warn(`DEPRECATION: ${updatedMessage}`); // eslint-disable-line no-console
});
let captureErrorForStack: () => Error;
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);
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.';
missingOptionsUntilDeprecation = 'When calling `deprecate` you must provide `until` in options.';
missingOptionsForDeprecation = (id: string) => {
return `When calling \`deprecate\` you must provide \`for\` in options. Missing options.for in "${id}" deprecation`;
};
missingOptionsSinceDeprecation = (id: string) => {
return `When calling \`deprecate\` you must provide \`since\` in options. Missing options.since in "${id}" deprecation`;
};
/**
@module @ember/debug
@public
*/
/**
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
@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, test, options) {
assert(missingOptionsDeprecation, Boolean(options && (options.id || options.until)));
assert(missingOptionsIdDeprecation, Boolean(options!.id));
assert(missingOptionsUntilDeprecation, Boolean(options!.until));
if (!options!.for && !FOR_MISSING_DEPRECATIONS.has(options!.id)) {
FOR_MISSING_DEPRECATIONS.add(options!.id);
deprecate(missingOptionsForDeprecation(options!.id), Boolean(options!.for), {
id: 'ember-source.deprecation-without-for',
until: '4.0.0',
for: 'ember-source',
since: {
enabled: '3.24.0',
},
});
}
if (!options!.since && !SINCE_MISSING_DEPRECATIONS.has(options!.id)) {
SINCE_MISSING_DEPRECATIONS.add(options!.id);
deprecate(missingOptionsSinceDeprecation(options!.id), Boolean(options!.since), {
id: 'ember-source.deprecation-without-since',
until: '4.0.0',
for: 'ember-source',
since: {
enabled: '3.24.0',
},
});
}
invoke('deprecate', message, test, options);
};
}
export default deprecate;
export {
registerHandler,
missingOptionsDeprecation,
missingOptionsIdDeprecation,
missingOptionsUntilDeprecation,
missingOptionsForDeprecation,
missingOptionsSinceDeprecation,
FOR_MISSING_DEPRECATIONS,
SINCE_MISSING_DEPRECATIONS,
};