-
Notifications
You must be signed in to change notification settings - Fork 63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Return null when passed a non-function argument #20
Return null when passed a non-function argument #20
Conversation
var match = toString.call(aFunc).match(functionNameMatch); | ||
if (match) { | ||
name = match[1]; | ||
var isFunction = aFunc instanceof Function; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would rather typeof aFunc == 'function'
. Function#toString
expects an object to have some internal methods only functions have, typeof
is the most accurate way to check for them.
Current solution will throw on edge cases like { __proto__: function() {} }
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
However, even typeof == 'function'
does not guarantee Function#toString
won't throw. typeof == 'function'
checks for [[Call]]
, but Function#toString
requires [[ECMAScriptCode]]
. let proxy = new Proxy(() => {}, {})
sets [[Call]]
, but not [[ECMAScriptCode]]
, so proxied functions does not work with toString
.
if (match) { | ||
name = match[1]; | ||
var isFunction = aFunc instanceof Function; | ||
if (isFunction) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we should early return for non-functions? Like if (typeof aFunc != 'function') return null
. Otherwise nesting is rather deep.
I have just updated this. |
Thanks for going forward on the I have a feeling that
|
@shvaikalesh hmmm, I'm not sure about that change. Thanks for your feedback, I'm not strongly against it, but I feel like using |
LGTM. I also prefer |
OK then, I have no strong opinion on this. LGTM. |
@@ -22,6 +23,10 @@ var toString = Function.prototype.toString; | |||
var functionNameMatch = /\s*function(?:\s|\s*\/\*[^(?:*\/)]+\*\/\s*)*([^\s\(\/]+)/; | |||
function getFuncName(aFunc) { | |||
var name = ''; | |||
if (typeof aFunc !== 'function') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe just nitpicking, but we could avoid unnecessary initializations of name
here:
var name;
if (typeof aFunc !== 'function') {
return null;
}
name = '';
or
if (typeof aFunc !== 'function') {
return null;
}
var name = '';
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any byte counts.
Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RIP
Err... I mean, LGTM!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Thanks for your approval, friends :) |
This PR aims to solve the issue described at chaijs/chai#893.
I just added a simple
instanceof
check to the main function in this module in order to detect when something that is not an instance of a function is passed. When that happens we returnnull
.Also, this should be enough to cover all cases since the
@@hasInstance
symbol needs to be implemented on the "father" functions in order for it to recognize other instances as their children, so if anyone implemented@@hasInstance
on theFunction
constructor they really expect whatever that symbol returns to be the final verdict whether something should be considered afunction
or not and therefore we shall not worry about those cases.I also added tests for that and updated the docs.
I will also submit a PR to the main repo later.