-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement [emberjs/rfcs#65](https://github.com/emberjs/rfcs/blob/master/text/0065-deprecation-warning-handlers.md).
- Loading branch information
Showing
12 changed files
with
359 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/*global __fail__*/ | ||
|
||
import Ember from 'ember-metal/core'; | ||
import EmberError from 'ember-metal/error'; | ||
import Logger from 'ember-metal/logger'; | ||
import { registerHandler as genericRegisterHandler, invoke } from 'ember-debug/handlers'; | ||
|
||
export function registerHandler(handler) { | ||
genericRegisterHandler('deprecate', handler); | ||
} | ||
|
||
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); | ||
}); | ||
|
||
registerHandler(function logDeprecationStackTrace(message, options, next) { | ||
if (Ember.LOG_STACKTRACE_ON_DEPRECATION) { | ||
let stackStr = ''; | ||
let error, stack; | ||
|
||
// When using new Error, we can't do the arguments check for Chrome. Alternatives are welcome | ||
try { __fail__.fail(); } catch (e) { error = e; } | ||
|
||
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 (Ember.ENV.RAISE_ON_DEPRECATION) { | ||
let updatedMessage = formatMessage(message); | ||
|
||
throw new EmberError(updatedMessage); | ||
} else { | ||
next(...arguments); | ||
} | ||
}); | ||
|
||
/** | ||
Display a deprecation warning with the provided message and a stack trace | ||
(Chrome and Firefox only). Ember build tools will remove any calls to | ||
`Ember.deprecate()` when doing a production build. | ||
@method deprecate | ||
@param {String} message A description of the deprecation. | ||
@param {Boolean|Function} test An optional boolean. If falsy, the deprecation | ||
will be displayed. If this is a function, it will be executed and its return | ||
value will be used as condition. | ||
@param {Object} options An optional object that can be used to pass | ||
in a `url` to the transition guide on the emberjs.com website, and 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". | ||
@public | ||
*/ | ||
export default function() { | ||
invoke('deprecate', ...arguments); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import isPlainFunction from 'ember-debug/is-plain-function'; | ||
|
||
export let HANDLERS = { }; | ||
|
||
function normalizeTest(test) { | ||
return isPlainFunction(test) ? test() : test; | ||
} | ||
|
||
export function registerHandler(type, callback) { | ||
let nextHandler = HANDLERS[type] || function() { }; | ||
|
||
HANDLERS[type] = function(message, options) { | ||
callback(message, options, nextHandler); | ||
}; | ||
} | ||
|
||
export function invoke(type, message, test, options) { | ||
if (normalizeTest(test)) { return; } | ||
|
||
let handlerForType = HANDLERS[type]; | ||
|
||
if (!handlerForType) { return; } | ||
|
||
if (handlerForType) { | ||
handlerForType(message, options); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default function isPlainFunction(test) { | ||
return typeof test === 'function' && test.PrototypeMixin === undefined; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import Logger from 'ember-metal/logger'; | ||
import { registerHandler as genericRegisterHandler, invoke } from 'ember-debug/handlers'; | ||
|
||
export function registerHandler(handler) { | ||
genericRegisterHandler('warn', handler); | ||
} | ||
|
||
registerHandler(function logWarning(message, options) { | ||
Logger.warn('WARNING: ' + message); | ||
if ('trace' in Logger) { | ||
Logger.trace(); | ||
} | ||
}); | ||
|
||
/** | ||
Display a warning with the provided message. Ember build tools will | ||
remove any calls to `Ember.warn()` when doing a production build. | ||
@method warn | ||
@param {String} message A warning to display. | ||
@param {Boolean} test An optional boolean. If falsy, the warning | ||
will be displayed. | ||
@public | ||
*/ | ||
export default function warn() { | ||
invoke('warn', ...arguments); | ||
} |
Oops, something went wrong.