tools,lib: enable strict equality lint rule

Enablie a lint rule to require `===` and `!==` instead of `==` and `!=`
except in some well-defined cases:

* comparing against `null` as a shorthand for also checking for
  `undefined`
* comparing the result of `typeof`
* comparing literal values

In cases where `==` or `!=` are being used as optimizations, use an
ESLint comment to disable the `eqeqeq` rule for that line explicitly. I
rather like this because it's a signal that the usage is intentional and
not a mistake.

PR-URL: https://github.com/nodejs/node/pull/12446
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Rich Trott 2017-04-16 11:29:35 -07:00 committed by James M Snell
parent 2e5188de92
commit 096508dfa9
8 changed files with 13 additions and 4 deletions

View File

@ -34,6 +34,7 @@ rules:
# Best Practices # Best Practices
# http://eslint.org/docs/rules/#best-practices # http://eslint.org/docs/rules/#best-practices
dot-location: [2, property] dot-location: [2, property]
eqeqeq: [2, smart]
no-fallthrough: 2 no-fallthrough: 2
no-global-assign: 2 no-global-assign: 2
no-multi-spaces: 2 no-multi-spaces: 2

View File

@ -1391,6 +1391,7 @@ Interface.prototype.setBreakpoint = function(script, line,
}; };
} else { } else {
// setBreakpoint('scriptname') // setBreakpoint('scriptname')
// eslint-disable-next-line eqeqeq
if (script != +script && !this.client.scripts[script]) { if (script != +script && !this.client.scripts[script]) {
var scripts = this.client.scripts; var scripts = this.client.scripts;
for (var id in scripts) { for (var id in scripts) {

View File

@ -112,6 +112,7 @@ assert.ok = ok;
// assert.equal(actual, expected, message_opt); // assert.equal(actual, expected, message_opt);
/* eslint-disable no-restricted-properties */ /* eslint-disable no-restricted-properties */
assert.equal = function equal(actual, expected, message) { assert.equal = function equal(actual, expected, message) {
// eslint-disable-next-line eqeqeq
if (actual != expected) fail(actual, expected, message, '==', assert.equal); if (actual != expected) fail(actual, expected, message, '==', assert.equal);
}; };
@ -120,6 +121,7 @@ assert.equal = function equal(actual, expected, message) {
// assert.notEqual(actual, expected, message_opt); // assert.notEqual(actual, expected, message_opt);
assert.notEqual = function notEqual(actual, expected, message) { assert.notEqual = function notEqual(actual, expected, message) {
// eslint-disable-next-line eqeqeq
if (actual == expected) { if (actual == expected) {
fail(actual, expected, message, '!=', assert.notEqual); fail(actual, expected, message, '!=', assert.notEqual);
} }
@ -176,6 +178,7 @@ function _deepEqual(actual, expected, strict, memos) {
// (determined by typeof value !== 'object'), // (determined by typeof value !== 'object'),
// or null, equivalence is determined by === or ==. // or null, equivalence is determined by === or ==.
if (isNullOrNonObj(actual) && isNullOrNonObj(expected)) { if (isNullOrNonObj(actual) && isNullOrNonObj(expected)) {
// eslint-disable-next-line eqeqeq
return strict ? actual === expected : actual == expected; return strict ? actual === expected : actual == expected;
} }

View File

@ -192,6 +192,7 @@ Buffer.allocUnsafeSlow = function(size) {
// If --zero-fill-buffers command line argument is set, a zero-filled // If --zero-fill-buffers command line argument is set, a zero-filled
// buffer is returned. // buffer is returned.
function SlowBuffer(length) { function SlowBuffer(length) {
// eslint-disable-next-line eqeqeq
if (+length != length) if (+length != length)
length = 0; length = 0;
assertSize(+length); assertSize(+length);
@ -306,7 +307,7 @@ function fromObject(obj) {
return b; return b;
} }
if (obj != undefined) { if (obj != null) {
if (obj.length !== undefined || isAnyArrayBuffer(obj.buffer)) { if (obj.length !== undefined || isAnyArrayBuffer(obj.buffer)) {
if (typeof obj.length !== 'number' || obj.length !== obj.length) { if (typeof obj.length !== 'number' || obj.length !== obj.length) {
return new FastBuffer(); return new FastBuffer();

View File

@ -1161,6 +1161,7 @@ fs.chownSync = function(path, uid, gid) {
// converts Date or number to a fractional UNIX timestamp // converts Date or number to a fractional UNIX timestamp
function toUnixTimestamp(time) { function toUnixTimestamp(time) {
// eslint-disable-next-line eqeqeq
if (typeof time === 'string' && +time == time) { if (typeof time === 'string' && +time == time) {
return +time; return +time;
} }

View File

@ -169,6 +169,7 @@ function setupKillAndExit() {
process.kill = function(pid, sig) { process.kill = function(pid, sig) {
var err; var err;
// eslint-disable-next-line eqeqeq
if (pid != (pid | 0)) { if (pid != (pid | 0)) {
throw new TypeError('invalid pid'); throw new TypeError('invalid pid');
} }

View File

@ -1365,7 +1365,7 @@ function getPathFromURLPosix(url) {
} }
function getPathFromURL(path) { function getPathFromURL(path) {
if (path == undefined || !path[searchParams] || if (path == null || !path[searchParams] ||
!path[searchParams][searchParams]) { !path[searchParams][searchParams]) {
return path; return path;
} }

View File

@ -179,9 +179,10 @@ function Socket(options) {
} else if (options.fd !== undefined) { } else if (options.fd !== undefined) {
this._handle = createHandle(options.fd); this._handle = createHandle(options.fd);
this._handle.open(options.fd); this._handle.open(options.fd);
// options.fd can be string (since it user-defined), // options.fd can be string (since it is user-defined),
// so changing this to === would be semver-major // so changing this to === would be semver-major
// See: https://github.com/nodejs/node/pull/11513 // See: https://github.com/nodejs/node/pull/11513
// eslint-disable-next-line eqeqeq
if ((options.fd == 1 || options.fd == 2) && if ((options.fd == 1 || options.fd == 2) &&
(this._handle instanceof Pipe) && (this._handle instanceof Pipe) &&
process.platform === 'win32') { process.platform === 'win32') {
@ -748,7 +749,7 @@ Socket.prototype._writeGeneric = function(writev, data, encoding, cb) {
// If it was entirely flushed, we can write some more right now. // If it was entirely flushed, we can write some more right now.
// However, if more is left in the queue, then wait until that clears. // However, if more is left in the queue, then wait until that clears.
if (req.async && this._handle.writeQueueSize != 0) if (req.async && this._handle.writeQueueSize !== 0)
req.cb = cb; req.cb = cb;
else else
cb(); cb();