Skip to content
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

util: fixes type in argument type validation error #25103

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ function inherits(ctor, superCtor) {

if (superCtor.prototype === undefined) {
throw new ERR_INVALID_ARG_TYPE('superCtor.prototype',
'Function', superCtor.prototype);
'Object', superCtor.prototype);
Copy link
Member

@BridgeAR BridgeAR Dec 28, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'Object', superCtor.prototype);
['Object', 'Function'], superCtor.prototype);

ECMAScript does not distinguish objects and functions but users do. Both will be accepted and all other inputs (besides null) throw.

I personally would either say we should remove the check to align the error message with the one for primitives or we should check for those as well. However, that would be semver-major and this function is already deprecated, so there's probably not much point in doing this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@BridgeAR thanks for the review! i'm excited to land a commit 😄

I don't agree with the suggested change because the value of superCtor.prototype must be an object. Sure, a function is an object so that works, but its not the function-ness of this value that is important, it is the object-ness (the intention is that this value's properties become available in the prototype chain, that's an object-y aspect of this value). I could apply your reasoning here and say that the suggested change should include Array, RegExp, etc, since they are also all objects and are equally meaningful here as Function, but I think you see that it wouldn't make the error any more meaningful to include those.

Also, I'm not sure which function you mean is deprecated. From the public docs, I don't see any deprecation for util.inherits(). Do you mean that ERR_INVALID_ARG_TYPE() is deprecated? If so, what is the more favorable alternative?

Thanks for your help!

}
Object.defineProperty(ctor, 'super_', {
value: superCtor,
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-util-inherits.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ common.expectsError(function() {
}, {
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "superCtor.prototype" property must be of type Function. ' +
message: 'The "superCtor.prototype" property must be of type Object. ' +
'Received type undefined'
});

Expand Down