From 2e095b4140724b11118f2ed458a34e3d8ceddfad Mon Sep 17 00:00:00 2001 From: Brian Vaughn Date: Thu, 9 Feb 2017 14:10:45 -1000 Subject: [PATCH] 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. --- .../shared/utils/validateCallback.js | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/renderers/shared/utils/validateCallback.js b/src/renderers/shared/utils/validateCallback.js index 64da06082c..65c0260db9 100644 --- a/src/renderers/shared/utils/validateCallback.js +++ b/src/renderers/shared/utils/validateCallback.js @@ -28,13 +28,19 @@ function formatUnexpectedArgument(arg: any) { } function validateCallback(callback: ?Function, callerName: string) { - invariant( - !callback || typeof callback === 'function', - '%s(...): Expected the last optional `callback` argument to be a ' + - 'function. Instead received: %s.', - callerName, - formatUnexpectedArgument(callback) - ); + if ( + callback !== null && + callback !== undefined && + typeof callback !== 'function' + ) { + invariant( + false, + '%s(...): Expected the last optional `callback` argument to be a ' + + 'function. Instead received: %s.', + callerName, + formatUnexpectedArgument(callback) + ); + } } module.exports = validateCallback;