-
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
tools: don't use global Buffer and add linter rule for it #1794
Conversation
@Fishrock123 what's the plan exactly with Buffer usage mentioned in #1770? Should it error as soon as |
@@ -106,16 +106,12 @@ function checkResults(expected_results, results) { | |||
var actual = results[k], | |||
expected = expected_results[k]; | |||
|
|||
if (typeof expected === 'function') { | |||
expected(r[k]); |
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.
Same as above
Probably? How else would it work? |
@@ -1,3 +1,4 @@ | |||
/* eslint-disable no-undef */ |
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.
Perhaps disable for all tests?
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.
Yeah, I think it's better that way, less noisy comments all over the tests, will work something out.
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.
no-undef
is one of the few rules that do something good (as opposed to the most eslint rules that do nothing except for enforcing a religion code style). I don't think it should be disabled for all tests, because it could catch real errors.
What about explicitly declaring globals in every file?
/* globals: undefined_reference_error_maker */
I don't remember the exact syntax, but I think you can add globals on per-file basis. And most linters (jshint, eslint) respect that.
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.
I agree generally to using no-undef
, but it is pretty common to use undefined globals in tests to trigger an error. With the rule enabled, one would have to research how to configure the linter when writing a test, which I'd like to avoid.
Using a global
comment is pretty equivalent to disabling the rule, both require additional ugly comments. I think we're fine with just disabling no-undef
in the test directory.
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.
maybe ask people to trigger an error differently?
my favorite way is []()
@Fishrock123 I'll just comment out the Buffer global in |
@silverwind correct. |
Rebased and disabled @Fishrock123 There are 83 occasions where |
@Fishrock123 I'll follow up with a port of nodejs/node-v0.x-archive@523929c in another PR. Does this one LGTY? |
@silverwind I'd prefer if you did the commits in this order: fix tests, apply full remove-global-buffer patch, add linting. That being said, this still doesn't seem to catch global Buffer use with |
Well I suppose I can split these up into multiple commits and add the Buffer thing too in its own commit. |
002a770
to
c38e3cb
Compare
Alright, split up in 3 commits now. The circular dependency described in nodejs/node-v0.x-archive#8603 (comment) didn't happen, so no workaround was needed. |
Updated and cleaned up the list of globals, which is now sorted in categories. @Fishrock123 PTAL |
__dirname : false | ||
global : false | ||
GLOBAL : false | ||
root : false |
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.
GLOBAL
and root
are going to be deprecated (#1838). Are they still being used in the code base ? If not they do not need to be here.
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.
But they ll still be there till removal, right?
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.
They're not used and no one sane would use them, so good call on removing them.
Filed sindresorhus/globals#33 for the missing |
Also filed eslint/eslint#2657 for a simpler way to undefine globals. |
Don't review yet, I'll try creating a custom eslint rule for the |
Here's my current custom rule, which @xjamundx graciously provided: 'use strict';
module.exports = function(context) {
var required = false;
return {
CallExpression: function(node) {
if (node.callee.type === 'Identifier' &&
node.callee.name === 'require') {
if (node.arguments.length && node.arguments[0].value === 'buffer') {
required = true;
}
}
},
Identifier: function(node) {
if (!required && node.name === 'Buffer') {
context.report(node, "Make sure to require('buffer').Buffer before using Buffer");
}
}
}
} It's almost working, except that it does detect an error on the require line itself: const Buffer = require('buffer').Buffer;
// ^--- errors here Any suggestions? |
I think your suggestion in eslint/eslint#2657 is much better than this headache with custom rules. Maybe re-open it? |
Up for review again! |
I'm realizing right now that unless we make the exports Buffer property read only it can still be overridden just as easily by users. So what's the point of forcing it to be required in all files? |
I don't see writable Buffer as an problem really. It could allow for a custom Buffer implementation and a lot of things in node can be overridden right now anyways. As for the reason of this change, there's a series of issues/prs that lead to it: nodejs/node-v0.x-archive#8588 |
Ah yes. I forgot about the REPL issue. Thanks for the reminder. I don't see the same change here for |
Yes, I think so. Tests were passing so I assumed it was fixed. |
@trevnorris I looked at that while attempting to port the original patch... I'm not sure why that was done in the first place? Oh, found it here: nodejs/node-v0.x-archive#8603 (comment) Looks like there was a circular dependancy issue? |
b154935
to
ac12c6c
Compare
@Fishrock123 @trevnorris @vkurchatkin I don't understand why this race condition happened last time, but it seems to be gone now, all tests are passing so I assume it must've been fixed in the meanwhile. I rebased and did a minor correction to the |
Enables the following rules: - no-undef: Valuable rule to error on usage of undefined variables - require-buffer: Custom rule that forbids usage of the global Buffer inside lib/ because of REPL issues.
ac12c6c
to
06135cd
Compare
Did another small correction to the printed message. Please review. |
@@ -1,3 +1,4 @@ | |||
/* eslint-disable require-buffer */ | |||
'use strict'; |
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.
I thought 'use strict'
had to go on the first line of either the file or function. Does that exclude comments?
/cc @domenic
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.
nm. see it doesn't matter.
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.
I'm pretty sure it does exclude comments, considering the amount of files with copyright hats at the very top.
Have one nit, but LGTM. |
|
||
module.exports = function(context) { | ||
return { | ||
"Program:exit": 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.
Same as above ^^
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.
I'll fix that.
Great thanks. I'm cool with this change. |
Fixes all cases of undeclared variable access as uncovered by the no-undef rule of eslint. PR-URL: #1794 Reviewed-By: Trevor Norris <[email protected]>
Port of nodejs/node-v0.x-archive#8603 The race condition present in the original PR didn't occur, so no workaround was needed. PR-URL: #1794 Reviewed-By: Trevor Norris <[email protected]>
Enables the following rules: - no-undef: Valuable rule to error on usage of undefined variables - require-buffer: Custom rule that forbids usage of the global Buffer inside lib/ because of REPL issues. PR-URL: #1794 Reviewed-By: Trevor Norris <[email protected]>
This does a few things to the linter configuration:
node
environment, specify all our globals manually and as read-only.no-undef
to error on undefined variable access.globalReturn
which allows to return from the top scope. This was implied by thenode
option before.A number of tests intentionally use undefined globals to trigger an error or set globals (in the
vm
tests). I had to exclude those tests with aneslint-disable
comment. If these comments are too noisy, we could alternatively disable that rule for all tests.I might have missed a few globals from the v8 environment.
Object.keys(global)
doesn't list many of them, presumably because they are unenumerable. Any better ideas on how to discover globals?cc: @Fishrock123 @domenic