diff --git a/lib/util.js b/lib/util.js index a8d9a356480a35..7922846fee0426 100644 --- a/lib/util.js +++ b/lib/util.js @@ -47,7 +47,6 @@ const { const { codes: { ERR_FALSY_VALUE_REJECTION, - ERR_INVALID_ARG_TYPE, ERR_OUT_OF_RANGE }, errnoException, @@ -63,6 +62,7 @@ const { debuglog } = require('internal/util/debuglog'); const { validateFunction, validateNumber, + validateObject, } = require('internal/validators'); const { TextDecoder, TextEncoder } = require('internal/encoding'); const { isBuffer } = require('buffer').Buffer; @@ -229,17 +229,10 @@ function log(...args) { * the super constructor lacks a prototype. */ function inherits(ctor, superCtor) { + validateFunction(ctor, 'ctor'); + validateFunction(superCtor, 'superCtor'); + validateObject(superCtor.prototype, 'superCtor.prototype'); - if (ctor === undefined || ctor === null) - throw new ERR_INVALID_ARG_TYPE('ctor', 'Function', ctor); - - if (superCtor === undefined || superCtor === null) - throw new ERR_INVALID_ARG_TYPE('superCtor', 'Function', superCtor); - - if (superCtor.prototype === undefined) { - throw new ERR_INVALID_ARG_TYPE('superCtor.prototype', - 'Object', superCtor.prototype); - } ObjectDefineProperty(ctor, 'super_', { value: superCtor, writable: true, diff --git a/test/parallel/test-util-inherits.js b/test/parallel/test-util-inherits.js index 1729b1734d03db..ac835852fcb326 100644 --- a/test/parallel/test-util-inherits.js +++ b/test/parallel/test-util-inherits.js @@ -82,9 +82,15 @@ assert.strictEqual(e.d(), 'd'); assert.strictEqual(e.e(), 'e'); assert.strictEqual(e.constructor, E); +function F() { + this.test = 'test'; +} + +F.prototype = undefined; + // Should throw with invalid arguments assert.throws(() => { - inherits(A, {}); + inherits(A, F); }, { code: 'ERR_INVALID_ARG_TYPE', name: 'TypeError',