Skip to content
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 doc/api/util.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ environment variable. For example: `NODE_DEBUG=fs,net,tls`.

## util.deprecate(function, string)

The `util.deprecate()` method wraps the given `function` in such a way that
The `util.deprecate()` method wraps the given `function` or class in such a way that
it is marked as deprecated.

```js
Expand Down
9 changes: 9 additions & 0 deletions lib/internal/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,18 @@ exports._deprecate = function(fn, msg) {
var warned = false;
function deprecated() {
warned = exports.printDeprecationMessage(msg, warned, deprecated);
if (new.target) {
return Reflect.construct(fn, arguments, new.target);
}
return fn.apply(this, arguments);
}

// the wrapper will keep the same prototype as fn to maintain prototype chain
Object.setPrototypeOf(deprecated, fn);
if (fn.prototype) {
Object.setPrototypeOf(deprecated.prototype, fn.prototype);
}

return deprecated;
};

Expand Down
12 changes: 12 additions & 0 deletions test/fixtures/deprecated-userland-class.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const util = require('util');
const assert = require('assert');

class deprecatedClass {
}

const deprecated = util.deprecate(deprecatedClass, 'deprecatedClass is deprecated.');

const instance = new deprecated();

assert(instance instanceof deprecated);
assert(instance instanceof deprecatedClass);
19 changes: 19 additions & 0 deletions test/fixtures/deprecated-userland-subclass.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const util = require('util');
const assert = require('assert');

class deprecatedClass {
}

const deprecated = util.deprecate(deprecatedClass, 'deprecatedClass is deprecated.');

class subclass extends deprecated {
constructor() {
super();
}
}

const instance = new subclass();

assert(instance instanceof subclass);
assert(instance instanceof deprecated);
assert(instance instanceof deprecatedClass);
24 changes: 21 additions & 3 deletions test/sequential/test-deprecation-flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@ const execFile = require('child_process').execFile;
const depmod = require.resolve(common.fixturesDir + '/deprecated.js');
const node = process.execPath;

const depUserland =
require.resolve(common.fixturesDir + '/deprecated-userland-function.js');
const depUserlandFunction =
require.resolve(common.fixturesDir + '/deprecated-userland-function.js');

const depUserlandClass =
require.resolve(common.fixturesDir + '/deprecated-userland-class.js');

const depUserlandSubClass =
require.resolve(common.fixturesDir + '/deprecated-userland-subclass.js');

const normal = [depmod];
const noDep = ['--no-deprecation', depmod];
Expand Down Expand Up @@ -39,10 +45,22 @@ execFile(node, traceDep, function(er, stdout, stderr) {
console.log('trace ok');
});

execFile(node, [depUserland], function(er, stdout, stderr) {
execFile(node, [depUserlandFunction], function(er, stdout, stderr) {
console.error('normal: testing deprecated userland function');
assert.equal(er, null);
assert.equal(stdout, '');
assert(/deprecatedFunction is deprecated/.test(stderr));
Copy link
Member

Choose a reason for hiding this comment

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

FWIW, an easier way to test for the deprecation warning would be to listen for warning events...

process.on('warning', (warning) => {
  if (warning.name === 'DeprecationWarning') {
    // ...
  }
});

But that doesn't need to be fixed here at all.. just pointing it out

console.error('normal: ok');
});

execFile(node, [depUserlandClass], function(er, stdout, stderr) {
assert.strictEqual(er, null);
assert.strictEqual(stdout, '');
assert(/deprecatedClass is deprecated/.test(stderr));
});

execFile(node, [depUserlandSubClass], function(er, stdout, stderr) {
assert.strictEqual(er, null);
assert.strictEqual(stdout, '');
assert(/deprecatedClass is deprecated/.test(stderr));
});