-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
127 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import log from './log.js'; | ||
|
||
/** | ||
* Object containing the default behaviors for available handler methods. | ||
* | ||
* @private | ||
* @type {Object} | ||
*/ | ||
const defaultBehaviors = { | ||
get(obj, key) { | ||
return obj[key]; | ||
}, | ||
set(obj, key, value) { | ||
obj[key] = value; | ||
return true; | ||
} | ||
}; | ||
|
||
/** | ||
* Expose private objects publicly using a Proxy to log deprecation warnings. | ||
* | ||
* Browsers that do not support Proxy objects will simply return the `target` | ||
* object, so it can be directly exposed. | ||
* | ||
* @param {Object} target The target object. | ||
* @param {Object} messages Messages to display from a Proxy. Only operations | ||
* with an associated message will be proxied. | ||
* @param {String} [messages.get] | ||
* @param {String} [messages.set] | ||
* @return {Object} A Proxy if supported or the `target` argument. | ||
*/ | ||
export default (target, messages={}) => { | ||
if (typeof Proxy === 'function') { | ||
let handler = {}; | ||
|
||
// Build a handler object based on those keys that have both messages | ||
// and default behaviors. | ||
Object.keys(messages).forEach(key => { | ||
if (defaultBehaviors.hasOwnProperty(key)) { | ||
handler[key] = function() { | ||
log.warn(messages[key]); | ||
return defaultBehaviors[key].apply(this, arguments); | ||
}; | ||
} | ||
}); | ||
|
||
return new Proxy(target, handler); | ||
} | ||
return target; | ||
}; |
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,45 @@ | ||
import createDeprecationProxy from '../../../src/js/utils/create-deprecation-proxy.js'; | ||
import log from '../../../src/js/utils/log.js'; | ||
|
||
const proxySupported = typeof Proxy === 'function'; | ||
|
||
test('should return a Proxy object when supported or the target object by reference', function() { | ||
let target = {foo: 1}; | ||
let subject = createDeprecationProxy(target, { | ||
get: 'get message', | ||
set: 'set message' | ||
}); | ||
|
||
// Testing for a Proxy is really difficult because Proxy objects by their | ||
// nature disguise the fact that they are in fact Proxy objects. So, this | ||
// tests that the log.warn method gets called on property get/set operations | ||
// to detect the Proxy. | ||
if (proxySupported) { | ||
sinon.stub(log, 'warn'); | ||
|
||
subject.foo; // Triggers a "get" | ||
subject.foo = 2; // Triggers a "set" | ||
|
||
equal(log.warn.callCount, 2, 'proxied operations cause deprecation warnings'); | ||
ok(log.warn.calledWith('get message'), 'proxied get logs expected message'); | ||
ok(log.warn.calledWith('set message'), 'proxied set logs expected message'); | ||
|
||
log.warn.restore(); | ||
} else { | ||
strictEqual(target, subject, 'identical to target'); | ||
} | ||
}); | ||
|
||
// Tests run only in Proxy-supporting environments. | ||
if (proxySupported) { | ||
test('no deprecation warning is logged for operations without a message', function() { | ||
let subject = createDeprecationProxy({}, { | ||
get: 'get message' | ||
}); | ||
|
||
sinon.stub(log, 'warn'); | ||
subject.foo = 'bar'; // Triggers a "set," but not a "get" | ||
equal(log.warn.callCount, 0, 'no deprecation warning expected'); | ||
log.warn.restore(); | ||
}); | ||
} |
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