Skip to content

Commit

Permalink
Merge pull request #11766 from emberjs/override-assertions
Browse files Browse the repository at this point in the history
Allow assertions to be overridden without mutating globals
  • Loading branch information
rwjblue committed Jul 18, 2015
2 parents f247f6e + 6582558 commit 5a02548
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 26 deletions.
32 changes: 20 additions & 12 deletions packages/ember-debug/lib/main.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*global __fail__*/

import Ember from 'ember-metal/core';
import { registerDebugFunction } from 'ember-metal/assert';
import isEnabled, { FEATURES } from 'ember-metal/features';
import EmberError from 'ember-metal/error';
import Logger from 'ember-metal/logger';
Expand Down Expand Up @@ -43,7 +44,7 @@ function isPlainFunction(test) {
its return value will be used as condition.
@public
*/
Ember.assert = function(desc, test) {
function assert(desc, test) {
var throwAssertion;

if (isPlainFunction(test)) {
Expand All @@ -55,7 +56,7 @@ Ember.assert = function(desc, test) {
if (throwAssertion) {
throw new EmberError('Assertion Failed: ' + desc);
}
};
}


/**
Expand All @@ -68,14 +69,14 @@ Ember.assert = function(desc, test) {
will be displayed.
@public
*/
Ember.warn = function(message, test) {
function warn(message, test) {
if (!test) {
Logger.warn('WARNING: ' + message);
if ('trace' in Logger) {
Logger.trace();
}
}
};
}

/**
Display a debug notice. Ember build tools will remove any calls to
Expand All @@ -89,9 +90,9 @@ Ember.warn = function(message, test) {
@param {String} message A debug message to display.
@public
*/
Ember.debug = function(message) {
function debug(message) {
Logger.debug('DEBUG: ' + message);
};
}

/**
Display a deprecation warning with the provided message and a stack trace
Expand All @@ -110,7 +111,7 @@ Ember.debug = function(message) {
The `id` should be namespaced by dots, e.g. "view.helper.select".
@public
*/
Ember.deprecate = function(message, test, options) {
function deprecate(message, test, options) {
if (Ember.ENV.RAISE_ON_DEPRECATION) {
deprecationManager.setDefaultLevel(deprecationLevels.RAISE);
}
Expand Down Expand Up @@ -169,7 +170,7 @@ Ember.deprecate = function(message, test, options) {
}

Logger.warn('DEPRECATION: ' + message);
};
}



Expand All @@ -193,7 +194,7 @@ Ember.deprecate = function(message, test, options) {
@return {Function} a new function that wrapped the original function with a deprecation warning
@private
*/
Ember.deprecateFunc = function(...args) {
function deprecateFunc(...args) {
if (args.length === 3) {
let [message, options, func] = args;
return function() {
Expand All @@ -207,7 +208,7 @@ Ember.deprecateFunc = function(...args) {
return func.apply(this, arguments);
};
}
};
}


/**
Expand All @@ -229,9 +230,16 @@ Ember.deprecateFunc = function(...args) {
@since 1.5.0
@public
*/
Ember.runInDebug = function(func) {
function runInDebug(func) {
func();
};
}

registerDebugFunction('assert', assert);
registerDebugFunction('warn', warn);
registerDebugFunction('debug', debug);
registerDebugFunction('deprecate', deprecate);
registerDebugFunction('deprecateFunc', deprecateFunc);
registerDebugFunction('runInDebug', runInDebug);

/**
Will call `Ember.warn()` if ENABLE_ALL_FEATURES, ENABLE_OPTIONAL_FEATURES, or
Expand Down
36 changes: 36 additions & 0 deletions packages/ember-metal/lib/assert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
export let debugFunctions = {
assert() {},
warn() {},
debug() {},
deprecate() {},
deprecateFunc(...args) { return args[args.length - 1]; },
runInDebug() {}
};

export function registerDebugFunction(name, fn) {
debugFunctions[name] = fn;
}

export function assert() {
return debugFunctions.assert.apply(undefined, arguments);
}

export function warn() {
return debugFunctions.warn.apply(undefined, arguments);
}

export function debug() {
return debugFunctions.debug.apply(undefined, arguments);
}

export function deprecate() {
return debugFunctions.deprecate.apply(undefined, arguments);
}

export function deprecateFunc() {
return debugFunctions.deprecateFunc.apply(undefined, arguments);
}

export function runInDebug() {
return debugFunctions.runInDebug.apply(undefined, arguments);
}
8 changes: 5 additions & 3 deletions packages/ember-metal/lib/chains.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { get, normalizeTuple } from 'ember-metal/property_get';
import { meta as metaFor } from 'ember-metal/utils';
import { watchKey, unwatchKey } from 'ember-metal/watch_key';

var warn = Ember.warn;
var FIRST_KEY = /^([^\.]+)/;

function firstKey(path) {
Expand Down Expand Up @@ -37,8 +36,11 @@ export function flushPendingChains() {

queue.forEach((q) => q[0].add(q[1]));

warn('Watching an undefined global, Ember expects watched globals to be' +
' setup by the time the run loop is flushed, check for typos', pendingQueue.length === 0);
Ember.warn(
'Watching an undefined global, Ember expects watched globals to be ' +
'setup by the time the run loop is flushed, check for typos',
pendingQueue.length === 0
);
}

function addChainWatcher(obj, keyName, node) {
Expand Down
11 changes: 0 additions & 11 deletions packages/ember-metal/lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,4 @@ export { K };
Ember.K = K;
//TODO: ES6 GLOBAL TODO

// Stub out the methods defined by the ember-debug package in case it's not loaded

if ('undefined' === typeof Ember.assert) { Ember.assert = K; }
if ('undefined' === typeof Ember.warn) { Ember.warn = K; }
if ('undefined' === typeof Ember.debug) { Ember.debug = K; }
if ('undefined' === typeof Ember.runInDebug) { Ember.runInDebug = K; }
if ('undefined' === typeof Ember.deprecate) { Ember.deprecate = K; }
if ('undefined' === typeof Ember.deprecateFunc) {
Ember.deprecateFunc = function(...args) { return args[args.length - 1]; };
}

export default Ember;
16 changes: 16 additions & 0 deletions packages/ember-metal/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,22 @@ Ember.FEATURES.isEnabled = isEnabled;
Ember.onerror = null;
// END EXPORTS

import {
assert,
warn,
debug,
deprecate,
deprecateFunc,
runInDebug
} from 'ember-metal/assert';

Ember.assert = assert;
Ember.warn = warn;
Ember.debug = debug;
Ember.deprecate = deprecate;
Ember.deprecateFunc = deprecateFunc;
Ember.runInDebug = runInDebug;

// do this for side-effects of updating Ember.assert, warn, etc when
// ember-debug is present
// This needs to be called before any deprecateFunc
Expand Down

0 comments on commit 5a02548

Please sign in to comment.