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.
This commit is contained in:
Brian Vaughn
2017-02-09 14:10:45 -10:00
committed by GitHub
parent 869c779861
commit 2e095b4140
+13 -7
View File
@@ -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;