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

tools: don't use global Buffer and add linter rule for it #1794

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ test/addons/doc-*/
test/fixtures
test/**/node_modules
test/parallel/test-fs-non-number-arguments-throw.js
test/disabled
64 changes: 32 additions & 32 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,9 @@ rules:
# Stylistic Issues
# list: https://github.com/eslint/eslint/tree/master/docs/rules#stylistic-issues
## use single quote, we can use double quote when escape chars
quotes:
- 2
- "single"
- "avoid-escape"
quotes: [2, "single", "avoid-escape"]
## 2 space indentation
indent:
- 2
- 2
indent: [2, 2]
## add space after comma
## set to 'warn' because of https://github.com/eslint/eslint/issues/2408
comma-spacing: 1
Expand All @@ -63,35 +58,40 @@ rules:
## require parens for Constructor
new-parens: 2
## max 80 length
max-len:
- 2
- 80
- 2
max-len: [2, 80, 2]

# Strict Mode
# list: https://github.com/eslint/eslint/tree/master/docs/rules#strict-mode
## 'use strict' on top
strict:
- 2
- "global"
strict: [2, "global"]

# Variables
# list: https://github.com/eslint/eslint/tree/master/docs/rules#variables
## disallow use of undefined variables (globals)
no-undef: 2

# Custom rules in tools/eslint-rules
require-buffer: 2

# Global scoped method and vars
globals:
DTRACE_HTTP_CLIENT_REQUEST: true
LTTNG_HTTP_CLIENT_REQUEST: true
COUNTER_HTTP_CLIENT_REQUEST: true
DTRACE_HTTP_CLIENT_RESPONSE: true
LTTNG_HTTP_CLIENT_RESPONSE: true
COUNTER_HTTP_CLIENT_RESPONSE: true
DTRACE_HTTP_SERVER_REQUEST: true
LTTNG_HTTP_SERVER_REQUEST: true
COUNTER_HTTP_SERVER_REQUEST: true
DTRACE_HTTP_SERVER_RESPONSE: true
LTTNG_HTTP_SERVER_RESPONSE: true
COUNTER_HTTP_SERVER_RESPONSE: true
DTRACE_NET_STREAM_END: true
LTTNG_NET_STREAM_END: true
COUNTER_NET_SERVER_CONNECTION_CLOSE: true
DTRACE_NET_SERVER_CONNECTION: true
LTTNG_NET_SERVER_CONNECTION: true
COUNTER_NET_SERVER_CONNECTION: true
DTRACE_HTTP_CLIENT_REQUEST : false
LTTNG_HTTP_CLIENT_REQUEST : false
COUNTER_HTTP_CLIENT_REQUEST : false
DTRACE_HTTP_CLIENT_RESPONSE : false
LTTNG_HTTP_CLIENT_RESPONSE : false
COUNTER_HTTP_CLIENT_RESPONSE : false
DTRACE_HTTP_SERVER_REQUEST : false
LTTNG_HTTP_SERVER_REQUEST : false
COUNTER_HTTP_SERVER_REQUEST : false
DTRACE_HTTP_SERVER_RESPONSE : false
LTTNG_HTTP_SERVER_RESPONSE : false
COUNTER_HTTP_SERVER_RESPONSE : false
DTRACE_NET_STREAM_END : false
LTTNG_NET_STREAM_END : false
COUNTER_NET_SERVER_CONNECTION_CLOSE : false
DTRACE_NET_SERVER_CONNECTION : false
LTTNG_NET_SERVER_CONNECTION : false
COUNTER_NET_SERVER_CONNECTION : false
escape : false
unescape : false
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ bench-idle:
./$(NODE_EXE) benchmark/idle_clients.js &

jslint:
./$(NODE_EXE) tools/eslint/bin/eslint.js src lib test --reset --quiet
./$(NODE_EXE) tools/eslint/bin/eslint.js src lib test --rulesdir tools/eslint-rules --reset --quiet

CPPLINT_EXCLUDE ?=
CPPLINT_EXCLUDE += src/node_lttng.cc
Expand Down
1 change: 1 addition & 0 deletions lib/_debugger.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const repl = Module.requireRepl();
const inherits = util.inherits;
const assert = require('assert');
const spawn = require('child_process').spawn;
const Buffer = require('buffer').Buffer;

exports.start = function(argv, stdin, stdout) {
argv || (argv = process.argv.slice(2));
Expand Down
1 change: 1 addition & 0 deletions lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const freeParser = common.freeParser;
const debug = common.debug;
const OutgoingMessage = require('_http_outgoing').OutgoingMessage;
const Agent = require('_http_agent');
const Buffer = require('buffer').Buffer;


function ClientRequest(options, cb) {
Expand Down
1 change: 1 addition & 0 deletions lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const assert = require('assert').ok;
const Stream = require('stream');
const timers = require('timers');
const util = require('util');
const Buffer = require('buffer').Buffer;
const common = require('_http_common');

const CRLF = common.CRLF;
Expand Down
1 change: 1 addition & 0 deletions lib/_stream_readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Readable.ReadableState = ReadableState;

const EE = require('events').EventEmitter;
const Stream = require('stream');
const Buffer = require('buffer').Buffer;
const util = require('util');
const debug = util.debuglog('stream');
var StringDecoder;
Expand Down
1 change: 1 addition & 0 deletions lib/_stream_writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Writable.WritableState = WritableState;

const util = require('util');
const Stream = require('stream');
const Buffer = require('buffer').Buffer;

util.inherits(Writable, Stream);

Expand Down
1 change: 1 addition & 0 deletions lib/_tls_legacy.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const tls = require('tls');
const util = require('util');
const common = require('_tls_common');
const debug = util.debuglog('tls-legacy');
const Buffer = require('buffer').Buffer;
const Timer = process.binding('timer_wrap').Timer;
var Connection = null;
try {
Expand Down
1 change: 1 addition & 0 deletions lib/_tls_wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const util = require('util');
const listenerCount = require('events').listenerCount;
const common = require('_tls_common');
const StreamWrap = require('_stream_wrap').StreamWrap;
const Buffer = require('buffer').Buffer;
const Duplex = require('stream').Duplex;
const debug = util.debuglog('tls');
const Timer = process.binding('timer_wrap').Timer;
Expand Down
1 change: 1 addition & 0 deletions lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
// UTILITY
const compare = process.binding('buffer').compare;
const util = require('util');
const Buffer = require('buffer').Buffer;
const pSlice = Array.prototype.slice;

// 1. The assert module provides functions that throw
Expand Down
1 change: 1 addition & 0 deletions lib/buffer.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable require-buffer */
'use strict';
Copy link
Contributor

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

Copy link
Contributor

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.

Copy link
Contributor

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.


const binding = process.binding('buffer');
Expand Down
1 change: 1 addition & 0 deletions lib/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const constants = require('constants');

const uv = process.binding('uv');
const spawn_sync = process.binding('spawn_sync');
const Buffer = require('buffer').Buffer;
const Pipe = process.binding('pipe_wrap').Pipe;
const child_process = require('internal/child_process');

Expand Down
1 change: 1 addition & 0 deletions lib/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ try {
throw new Error('node.js not compiled with openssl crypto support.');
}

const Buffer = require('buffer').Buffer;
const constants = require('constants');
const stream = require('stream');
const util = require('util');
Expand Down
1 change: 1 addition & 0 deletions lib/dgram.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const assert = require('assert');
const Buffer = require('buffer').Buffer;
const util = require('util');
const events = require('events');
const constants = require('constants');
Expand Down
1 change: 1 addition & 0 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const pathModule = require('path');
const binding = process.binding('fs');
const constants = require('constants');
const fs = exports;
const Buffer = require('buffer').Buffer;
const Stream = require('stream').Stream;
const EventEmitter = require('events').EventEmitter;
const FSReqWrap = binding.FSReqWrap;
Expand Down
1 change: 1 addition & 0 deletions lib/internal/child_process.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const StringDecoder = require('string_decoder').StringDecoder;
const Buffer = require('buffer').Buffer;
const EventEmitter = require('events').EventEmitter;
const net = require('net');
const dgram = require('dgram');
Expand Down
1 change: 1 addition & 0 deletions lib/internal/smalloc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const smalloc = process.binding('smalloc');
const Buffer = require('buffer').Buffer;
const kMaxLength = smalloc.kMaxLength;
const kMinType = smalloc.kMinType;
const kMaxType = smalloc.kMaxType;
Expand Down
1 change: 1 addition & 0 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const assert = require('assert');
const cares = process.binding('cares_wrap');
const uv = process.binding('uv');

const Buffer = require('buffer').Buffer;
const TTYWrap = process.binding('tty_wrap');
const TCP = process.binding('tcp_wrap').TCP;
const Pipe = process.binding('pipe_wrap').Pipe;
Expand Down
1 change: 1 addition & 0 deletions lib/querystring.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
'use strict';

const QueryString = exports;
const Buffer = require('buffer').Buffer;


function charCode(c) {
Expand Down
1 change: 1 addition & 0 deletions lib/readline.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const kHistorySize = 30;

const util = require('util');
const inherits = util.inherits;
const Buffer = require('buffer').Buffer;
const EventEmitter = require('events').EventEmitter;


Expand Down
2 changes: 2 additions & 0 deletions lib/string_decoder.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const Buffer = require('buffer').Buffer;

function assertEncoding(encoding) {
// Do not cache `Buffer.isEncoding`, some modules monkey-patch it to support
// additional encodings
Expand Down
1 change: 1 addition & 0 deletions lib/tls.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const net = require('net');
const url = require('url');
const util = require('util');
const binding = process.binding('crypto');
const Buffer = require('buffer').Buffer;

// Allow {CLIENT_RENEG_LIMIT} client-initiated session renegotiations
// every {CLIENT_RENEG_WINDOW} seconds. An error event is emitted if more
Expand Down
1 change: 1 addition & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const uv = process.binding('uv');
const Buffer = require('buffer').Buffer;
const Debug = require('vm').runInDebugContext('Debug');
const internalUtil = require('internal/util');

Expand Down
1 change: 1 addition & 0 deletions lib/zlib.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

const Buffer = require('buffer').Buffer;
const Transform = require('_stream_transform');
const binding = process.binding('zlib');
const util = require('util');
Expand Down
7 changes: 7 additions & 0 deletions test/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,10 @@
rules:
## allow unreachable code
no-unreachable: 0
## allow undeclared variables
no-undef: 0
## allow global Buffer usage
require-buffer: 0

globals:
gc: false
16 changes: 6 additions & 10 deletions test/parallel/test-cluster-worker-exit.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,13 @@ function checkResults(expected_results, results) {
var actual = results[k],
expected = expected_results[k];

if (typeof expected === 'function') {
expected(r[k]);
} else {
var msg = (expected[1] || '') +
(' [expected: ' + expected[0] + ' / actual: ' + actual + ']');
var msg = (expected[1] || '') +
(' [expected: ' + expected[0] + ' / actual: ' + actual + ']');
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is an actual error with the undefined r variable here. Because the condition was never met, I removed the whole if block.


if (expected && expected.length) {
assert.equal(actual, expected[0], msg);
} else {
assert.equal(actual, expected, msg);
}
if (expected && expected.length) {
assert.equal(actual, expected[0], msg);
} else {
assert.equal(actual, expected, msg);
}
}
}
Expand Down
14 changes: 5 additions & 9 deletions test/parallel/test-cluster-worker-kill.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,12 @@ function checkResults(expected_results, results) {
var actual = results[k],
expected = expected_results[k];

if (typeof expected === 'function') {
expected(r[k]);
var msg = (expected[1] || '') +
(' [expected: ' + expected[0] + ' / actual: ' + actual + ']');
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same as above

if (expected && expected.length) {
assert.equal(actual, expected[0], msg);
} else {
var msg = (expected[1] || '') +
(' [expected: ' + expected[0] + ' / actual: ' + actual + ']');
if (expected && expected.length) {
assert.equal(actual, expected[0], msg);
} else {
assert.equal(actual, expected, msg);
}
assert.equal(actual, expected, msg);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/pummel/test-net-timeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ process.on('exit', function() {
assert.ok(starttime != null);
assert.ok(timeouttime != null);

diff = timeouttime - starttime;
var diff = timeouttime - starttime;
Copy link
Contributor

Choose a reason for hiding this comment

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

Strange that both common and 'use strict' didn't throw an error for this variable that wasn't previously declared. Glad it's fixed, but bothers me that wasn't caught before. Any thoughts as to why?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Strange indeed. If I run this test manually on master without the testing framework, I see a ReferenceError. Not too familar with pummel tests, how are they executed?

Copy link
Contributor

Choose a reason for hiding this comment

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

Same way all the other tests are run. test.py pummel

Copy link
Contributor Author

Choose a reason for hiding this comment

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

make test-pummel shows this error (along with some others). It seems these tests aren't ran as part of make test at all.

Copy link
Contributor

Choose a reason for hiding this comment

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

ah yeah. forgot about that slight nuance. :P

Reason they're not run with all the others is because they have the potential to run a long time, or overload the systems resources.

console.log('diff = ' + diff);

assert.ok(timeout < diff);
Expand Down
15 changes: 15 additions & 0 deletions tools/eslint-rules/require-buffer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

const msg = "Use const Buffer = require('buffer').Buffer; on top of this file";
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: s/on top of/at the beginning of/

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, we use ' for strings, right? Is it okay here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To avoid escaping it's okay (There's a linter rule for it).


module.exports = function(context) {
return {
"Program:exit": function() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Same as above ^^

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll fix that.

context.getScope().through.forEach(function(ref) {
if (ref.identifier.name === 'Buffer') {
context.report(ref.identifier, msg);
}
});
}
}
}
2 changes: 1 addition & 1 deletion vcbuild.bat
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ goto jslint
:jslint
if not defined jslint goto exit
echo running jslint
%config%\iojs tools\eslint\bin\eslint.js src lib test --reset --quiet
%config%\iojs tools\eslint\bin\eslint.js src lib test --rulesdir tools\eslint-rules --reset --quiet
goto exit

:create-msvs-files-failed
Expand Down