@samitouri / QOS-React / commits / 2e095b4140

Hardened validateCallback to better handle null values (#8973)

Previously, calls to validateCallback() with a null callback value resulted in runtime errors if a certain transform was not applied prior to running. This commit wraps the invariant() with the condition check so as to avoid calling formatUnexpectedArgument() unless necessary. It also replaces the truthy/falsy callback check with an explicit check for improved performance.

Brian Vaughn committed Feb 9, 2017 at 14:10 UTC 2e095b4140724b11118f2ed458a34e3d8ceddfad
1 file changed +13 -7
src/renderers/shared/utils/validateCallback.js
+13 -7
@@ -28,13 +28,19 @@ function formatUnexpectedArgument(arg: any) {
28 }
29
30 function validateCallback(callback: ?Function, callerName: string) {
31 - invariant(
32 - !callback || typeof callback === 'function',
33 - '%s(...): Expected the last optional `callback` argument to be a ' +
34 - 'function. Instead received: %s.',
35 - callerName,
36 - formatUnexpectedArgument(callback)
37 - );
31 + if (
32 + callback !== null &&
33 + callback !== undefined &&
34 + typeof callback !== 'function'
35 + ) {
36 + invariant(
37 + false,
38 + '%s(...): Expected the last optional `callback` argument to be a ' +
39 + 'function. Instead received: %s.',
40 + callerName,
41 + formatUnexpectedArgument(callback)
42 + );
43 + }
44 }
45
46 module.exports = validateCallback;