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: allow deprecate on classes #7690

Closed
wants to merge 1 commit into from
Closed

util: allow deprecate on classes #7690

wants to merge 1 commit into from

Conversation

vdeturckheim
Copy link
Member

@vdeturckheim vdeturckheim commented Jul 12, 2016

Checklist
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines
Affected core subsystem(s)
  • util
Description of change

Classes objects cannot be called without new. util.deprecate executes 'fn.apply'. Classes cannot be deprecated with util.deprecate.

This uses 'new.target' to detect (when process is defined) calls to constructor to allow deprecate on class.

@nodejs-github-bot nodejs-github-bot added the util Issues and PRs related to the built-in util module. label Jul 12, 2016
class deprecatedClass {
}

var depreciated = util.deprecate(deprecatedClass, 'deprecatedClass is deprecated.');
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be deprecated? Also, const please.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ack. Sorry, French-English mixin in my brain

@vdeturckheim
Copy link
Member Author

@cjihrig Thanks a lot for the review !
I think I have fixed what you pointed. Do you think I should also remove console statements and enforce strictEqual for the other tests (already present) in test/sequential/test-deprecation-flags.js ?

@cjihrig
Copy link
Contributor

cjihrig commented Jul 13, 2016

@vdeturckheim no need to change the existing test. Thanks though.

@@ -60,6 +60,9 @@ exports._deprecate = function(fn, msg) {
var warned = false;
function deprecated() {
warned = exports.printDeprecationMessage(msg, warned, deprecated);
if (new.target) {
return new (Function.prototype.bind.apply(fn, arguments));
Copy link
Member

Choose a reason for hiding this comment

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

I think it's important to make the deprecated class correctly subclassable. Can you change this line to return Reflect.construct(fn, arguments, new.target) and add a test that verifies that ?

Copy link
Member Author

@vdeturckheim vdeturckheim Jul 13, 2016

Choose a reason for hiding this comment

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

Ack.
Seems fair to me.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think a test should be added with a subclass as well to make confirm the functionality

@Fishrock123
Copy link
Contributor

Seems reasonable to me, I bet core will need it at some point in the future.

CI: https://ci.nodejs.org/job/node-test-pull-request/3282/

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

class subclass extends deprecated {

Copy link
Contributor

Choose a reason for hiding this comment

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

No need for this blank line.

@cjihrig
Copy link
Contributor

cjihrig commented Jul 14, 2016

Can you remove all of the additional commits from this PR?

@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the backticks around "function" here, refer to the named parameter. If that's the case, I'm not sure it makes sense to put "class" in backticks too.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ack.

@cjihrig
Copy link
Contributor

cjihrig commented Jul 14, 2016

One small comment, but LGTM.

@targos
Copy link
Member

targos commented Jul 14, 2016

Looking at it again, I am not sure it's doing what we expect it to.

Example:

class A {
  hello() {
    console.log('world');
  }
}

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

var da = new DA(); // logs deprecation message
da.hello(); // throws

To fix this, we need to pass B as a third argument to Reflect.construct. But then comes the problem of inheritance.

@targos
Copy link
Member

targos commented Jul 14, 2016

Maybe we should change the prototype chain of the returned function to include A.prototype ?

Something like:

Object.setPrototypeOf(deprecated, A);
Object.setPrototypeOf(deprecated.prototype, A.prototype);

@vdeturckheim
Copy link
Member Author

@targos your solution seems to work:

const deprecate = function(fn, msg) {

    var warned = false;
    function deprecated() {
        warned = console.log(msg);
        if (new.target) {
            return Reflect.construct(fn, arguments, new.target);
        }
        return fn.apply(this, arguments);
    }

    Object.setPrototypeOf(deprecated, fn);
    Object.setPrototypeOf(deprecated.prototype, fn.prototype);

    return deprecated;
};

class A{};
const DA = deprecate(A, 'A is deprecated');

class B extends DA{
    constructor() {
        super();
    }
}

const b = new B();

console.log(b instanceof B); // true
console.log(b instanceof A); // true
console.log(b instanceof DA); // true


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

new deprecated();
Copy link
Member

Choose a reason for hiding this comment

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

can you add a few instanceof checks ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Ack.

@targos
Copy link
Member

targos commented Jul 14, 2016

Classes objects cannot be called without new. util.deprecate executes
'fn.apply'.

Therefore, classes cannot be deprecated with util.deprecate.

This uses 'new.target' to detect (when process is defined) calls to
constructor to allow deprecate on class.
@vdeturckheim
Copy link
Member Author

vdeturckheim commented Jul 14, 2016

I added a check on the presence of fn.prototype before assignation

CI: https://ci.nodejs.org/job/node-test-pull-request/3294/

@targos
Copy link
Member

targos commented Jul 14, 2016

Oh yes, I forgot about arrow functions.

new CI: https://ci.nodejs.org/job/node-test-pull-request/3296/

@vdeturckheim
Copy link
Member Author

@targos Is there anything else I need to do before merge ? :)

@targos
Copy link
Member

targos commented Jul 19, 2016

@vdeturckheim No, everything's fine. Waiting for other collaborators to review :)

@@ -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

@jasnell
Copy link
Member

jasnell commented Jul 29, 2016

LGTM. Good stuff.

@jasnell
Copy link
Member

jasnell commented Aug 1, 2016

It's been a while since the last CI run: https://ci.nodejs.org/job/node-test-pull-request/3489/

@vdeturckheim
Copy link
Member Author

@jasnell I don't really understand why the build fails now :/

@addaleax
Copy link
Member

addaleax commented Aug 2, 2016

@vdeturckheim That’s an unrelated build problem, nothing to worry about.

New CI attempt: https://ci.nodejs.org/job/node-test-commit/4369/

@cjihrig
Copy link
Contributor

cjihrig commented Aug 2, 2016

Looks like more unrelated failures.

@vdeturckheim
Copy link
Member Author

vdeturckheim commented Aug 2, 2016

:( Is there anything I can/should do here ?

@addaleax
Copy link
Member

addaleax commented Aug 2, 2016

Probably no… the last two CI runs together are green, though, and one of the flaky tests from the last run is just being fixed.

@vdeturckheim
Copy link
Member Author

Ok, thanks :)

@cjihrig
Copy link
Contributor

cjihrig commented Aug 4, 2016

Landed in 320f433. Thanks, and sorry for the delay.

@cjihrig cjihrig closed this Aug 4, 2016
cjihrig pushed a commit that referenced this pull request Aug 4, 2016
Classes cannot be instantiated without new, but util.deprecate()
uses Function.prototype.apply(). This commit uses new.target to
detect constructor calls, allowing classes to be deprecated.

PR-URL: #7690
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Michaël Zasso <mic.besace@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
@cjihrig cjihrig mentioned this pull request Aug 8, 2016
cjihrig pushed a commit that referenced this pull request Aug 10, 2016
Classes cannot be instantiated without new, but util.deprecate()
uses Function.prototype.apply(). This commit uses new.target to
detect constructor calls, allowing classes to be deprecated.

PR-URL: #7690
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Michaël Zasso <mic.besace@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
@cjihrig cjihrig mentioned this pull request Aug 11, 2016
@MylesBorins
Copy link
Contributor

this is not landing cleanly on v4.x, would someone be willing to backport?

@MylesBorins
Copy link
Contributor

this will need to be re-implemented without Reflect to work on v4.x

please feel free to submit a backport if you are able to

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
util Issues and PRs related to the built-in util module.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants