Uh oh!
There was an error while loading. Please reload this page.
tools: don't use global Buffer and add linter rule for it - #1794
tools: don't use global Buffer and add linter rule for it#1794silverwind wants to merge 4 commits into
Conversation
silverwind
commented
May 25, 2015
@Fishrock123 what's the plan exactly with Buffer usage mentioned in #1770? Should it error as soon as |
Fishrock123
commented
May 26, 2015
Probably? How else would it work? |
There was a problem hiding this comment.
Perhaps disable for all tests?
There was a problem hiding this comment.
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.
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.
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.
maybe ask people to trigger an error differently?
my favorite way is []()
silverwind
commented
May 26, 2015
@Fishrock123 I'll just comment out the Buffer global in |
Fishrock123
commented
May 26, 2015
@silverwind correct. |
silverwind
commented
May 29, 2015
Rebased and disabled @Fishrock123 There are 83 occasions where |
silverwind
commented
May 29, 2015
@Fishrock123 I'll follow up with a port of nodejs/node-v0.x-archive@523929c in another PR. Does this one LGTY? |
Fishrock123
commented
May 29, 2015
@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 |
silverwind
commented
May 29, 2015
Well I suppose I can split these up into multiple commits and add the Buffer thing too in its own commit. |
002a770 to
c38e3cbComparesilverwind
commented
May 29, 2015
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. |
silverwind
commented
May 29, 2015
silverwind
commented
Jun 1, 2015
Updated and cleaned up the list of globals, which is now sorted in categories. @Fishrock123 PTAL |
There was a problem hiding this comment.
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.
But they ll still be there till removal, right?
There was a problem hiding this comment.
They're not used and no one sane would use them, so good call on removing them.
silverwind
commented
Jun 1, 2015
Filed sindresorhus/globals#33 for the missing |
silverwind
commented
Jun 1, 2015
Also filed eslint/eslint#2657 for a simpler way to undefine globals. |
silverwind
commented
Jun 1, 2015
Don't review yet, I'll try creating a custom eslint rule for the |
silverwind
commented
Jun 7, 2015
Here's my current custom rule, which @xjamundx graciously provided: 'use strict';module.exports=function(context){varrequired=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: constBuffer=require('buffer').Buffer;// ^--- errors hereAny suggestions? |
rlidwka
commented
Jun 7, 2015
I think your suggestion in eslint/eslint#2657 is much better than this headache with custom rules. Maybe re-open it? |
silverwind
commented
Jun 7, 2015
Up for review again! |
trevnorris
commented
Jun 8, 2015
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? |
silverwind
commented
Jun 8, 2015
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 |
trevnorris
commented
Jun 8, 2015
Ah yes. I forgot about the REPL issue. Thanks for the reminder. I don't see the same change here for |
silverwind
commented
Jun 8, 2015
Yes, I think so. Tests were passing so I assumed it was fixed. |
Fishrock123
commented
Jun 9, 2015
@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? |
silverwind
commented
Jun 11, 2015
@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.
silverwind
commented
Jun 11, 2015
Did another small correction to the printed message. Please review. |
There was a problem hiding this comment.
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.
I'm pretty sure it does exclude comments, considering the amount of files with copyright hats at the very top.
trevnorris
commented
Jun 11, 2015
Have one nit, but LGTM. |
trevnorris
commented
Jun 11, 2015
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 <trev.norris@gmail.com>
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 <trev.norris@gmail.com>
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 <trev.norris@gmail.com>
This does a few things to the linter configuration:
nodeenvironment, specify all our globals manually and as read-only.no-undefto error on undefined variable access.globalReturnwhich allows to return from the top scope. This was implied by thenodeoption before.A number of tests intentionally use undefined globals to trigger an error or set globals (in the
vmtests). I had to exclude those tests with aneslint-disablecomment. 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