-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
src: deprecate root and GLOBAL variables #1838
Conversation
I think these should be pretty low risk to remove. If someone has an idea on how to print a deprecation warning when accessing the global or its properties, we could also go that route. |
Use a Getter? |
There shouldn't be much code relying on this but there may be some so we do need a deprecation message. edit: btw the commit log should be |
Won't work for usage like |
@silverwind isn't that still accessing |
@Fishrock123 right, something like this should work: Object.defineProperty(global, 'GLOBAL', {get: function() {
util.deprecate(function() {}, "'GLOBAL' is deprecated, use 'global'");
return global;
}}); @JacksonTian could you try incorporating such a print to both globals instead of removing them? |
Better one: Object.defineProperty(global, 'GLOBAL', {
get: util.deprecate(function() { return global; }, "'GLOBAL' is deprecated, use 'global'")
}); |
maybe also make the property writable ? |
Right, this is getting interesting now. Below code should only print once on first read/write access: var g = r = global;
var accessGlobal = util.deprecate(function(val) {
if (val) g = val;
return g;
}, "'GLOBAL' is deprecated, use 'global'");
var accessRoot = util.deprecate(function(val) {
if (val) r = val;
return r;
}, "'root' is deprecated, use 'global'");
Object.defineProperty(global, 'GLOBAL', {get: accessGlobal, set: accessGlobal});
Object.defineProperty(global, 'root', {get: accessRoot, set: accessRoot}); |
One more revision: const holders = {GLOBAL: global, root: global};
const accessGlobal = util.deprecate(function(val) {
if (val) holders.GLOBAL = val;
return holders.GLOBAL;
}, "'GLOBAL' is deprecated, use 'global'");
const accessRoot = util.deprecate(function(val) {
if (val) holders.root = val;
return holders.root;
}, "'root' is deprecated, use 'global'");
Object.defineProperty(global, 'GLOBAL', {get: accessGlobal, set: accessGlobal});
Object.defineProperty(global, 'root', {get: accessRoot, set: accessRoot}); @JacksonTian feel free to use something like that. Otherwise, I could do a PR that succeeds this one :) |
acbdbcf
to
233d4f8
Compare
@silverwind The PR is updated, please review it again. |
Apropos assignment, I'd replace the getter/setter inside the setter, e.g.: Object.defineProperty(global, 'GLOBAL', {
configurable: true, // but not enumerable
get: util.deprecate(function() { return this; },
`'GLOBAL' is deprecated, use 'global'`),
set: function(value) {
const desc = { configurable: true, enumerable: true, value: value };
Object.defineProperty(this, 'GLOBAL', desc);
},
}); Ditto for get: util.deprecate(function() {
const desc = { configurable: true, enumerable: true, value: this };
Object.defineProperty(this, 'GLOBAL', desc);
return this;
}, `'GLOBAL' is deprecated, use 'global'`) That prints a warning on the first access and then gets out of the way. It won't regress code that uses |
+1 for using @bnoordhuis I have a bit of a hard time understanding why running |
@silverwind The getter calls |
@bnoordhuis oh right, thats quite genius! |
@bnoordhuis 👍 very clever The setter can be simplified using shorthand property: set(value) {
const desc = { configurable: true, enumerable: true, value: value };
Object.defineProperty(this, 'GLOBAL', desc);
} |
and maybe it should be |
Good point, it should be writable when setting the value (but not when setting the getter/setter pair.) |
Hi everyone, I updated the PR by your comments, thanks. |
// getter | ||
const get = NativeModule.require('util').deprecate(function() { | ||
const desc = { configurable: true, enumerable: true, value: this }; | ||
Object.defineProperty(this, 'GLOBAL', desc); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be Object.defineProperty(this, name, desc);
// Deprecate GLOBAL and root | ||
['GLOBAL', 'root'].forEach(function(name) { | ||
// getter | ||
const get = NativeModule.require('util').deprecate(function() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tiny suggestion: the code might look a little better if you pulled the NativeModule.require('util')
out into a variable. Feel free to ignore if you want.
LGTM with the same suggestions. And +1 to this being semver-major. |
|
LGTM as semver-major |
ping @JacksonTian |
@bnoordhuis back then, |
7a449d9
to
2b3340a
Compare
The `root` and `GLOBAL` never be documented.
2b3340a
to
22e123b
Compare
Updated. Thanks all. |
LGTM |
LGTM (if passes CI) |
The `root` and `GLOBAL` were never documented. PR-URL: #1838 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Сковорода Никита Андреевич <[email protected]> Reviewed-By: Roman Reiss <[email protected]> Reviewed By: Sakthipriyan Vairamani <[email protected]>
Unrelated lint error addressed by #5161. Landed in 4e46931, thanks @JacksonTian! |
is removed from node nodejs/node#1838
The
root
andGLOBAL
never be documented.