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

Fail verbosely on invalid use of util.inherits #1240

Closed
wants to merge 4 commits into from
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
15 changes: 15 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -616,8 +616,23 @@ exports.log = function() {
* @param {function} ctor Constructor function which needs to inherit the
* prototype.
* @param {function} superCtor Constructor function to inherit prototype from.
* @throws {TypeError} Will error if either constructor is null, or if
* the super constructor lacks a prototype.
*/
exports.inherits = function(ctor, superCtor) {

if (ctor === undefined || ctor === null)
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you have any opinions on loose equality here and below, i.e. ctor == null? I think it would behave the same way, but that's your call to use or not

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That in turn triggers a jshint error. Wasn't feeling ballsy enough to add a jshint ignore line in my first PR :P

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes == null is exactly the same as === null || === undefined

Copy link
Contributor

Choose a reason for hiding this comment

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

io.js uses the closure linter, so a jshint ignore line wouldn't be necessary, unless it also does the same.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It does. I originally used == null, but that triggered an error.

throw new TypeError('The constructor to `inherits` must not be ' +
'null or undefined.');

if (superCtor === undefined || superCtor === null)
throw new TypeError('The super constructor to `inherits` must not ' +
'be null or undefined.');

if (superCtor.prototype === undefined)
throw new TypeError('The super constructor to `inherits` must ' +
'have a prototype.');

ctor.super_ = superCtor;
ctor.prototype = Object.create(superCtor.prototype, {
constructor: {
Expand Down
7 changes: 7 additions & 0 deletions test/parallel/test-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,10 @@ assert.deepEqual(util._extend({a:1}, true), {a:1});
assert.deepEqual(util._extend({a:1}, false), {a:1});
assert.deepEqual(util._extend({a:1}, {b:2}), {a:1, b:2});
assert.deepEqual(util._extend({a:1, b:2}, {b:3}), {a:1, b:3});

// inherits
var ctor = function() {};
assert.throws(function() { util.inherits(ctor, {}) }, TypeError);
assert.throws(function() { util.inherits(ctor, null) }, TypeError);
assert.throws(function() { util.inherits(null, ctor) }, TypeError);
assert.doesNotThrow(function() { util.inherits(ctor, ctor) }, TypeError);