Skip to content

Commit

Permalink
tools,lib: enable strict equality lint rule
Browse files Browse the repository at this point in the history
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: #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>
  • Loading branch information
Trott authored and jasnell committed Apr 18, 2017
1 parent 2e5188d commit 096508d
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 4 deletions.
1 change: 1 addition & 0 deletions .eslintrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ rules:
# Best Practices
# http://eslint.org/docs/rules/#best-practices
dot-location: [2, property]
eqeqeq: [2, smart]
no-fallthrough: 2
no-global-assign: 2
no-multi-spaces: 2
Expand Down
1 change: 1 addition & 0 deletions lib/_debugger.js
Original file line number Diff line number Diff line change
Expand Up @@ -1391,6 +1391,7 @@ Interface.prototype.setBreakpoint = function(script, line,
};
} else {
// setBreakpoint('scriptname')
// eslint-disable-next-line eqeqeq
if (script != +script && !this.client.scripts[script]) {
var scripts = this.client.scripts;
for (var id in scripts) {
Expand Down
3 changes: 3 additions & 0 deletions lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ assert.ok = ok;
// assert.equal(actual, expected, message_opt);
/* eslint-disable no-restricted-properties */
assert.equal = function equal(actual, expected, message) {
// eslint-disable-next-line eqeqeq
if (actual != expected) fail(actual, expected, message, '==', assert.equal);
};

Expand All @@ -120,6 +121,7 @@ assert.equal = function equal(actual, expected, message) {
// assert.notEqual(actual, expected, message_opt);

assert.notEqual = function notEqual(actual, expected, message) {
// eslint-disable-next-line eqeqeq
if (actual == expected) {
fail(actual, expected, message, '!=', assert.notEqual);
}
Expand Down Expand Up @@ -176,6 +178,7 @@ function _deepEqual(actual, expected, strict, memos) {
// (determined by typeof value !== 'object'),
// or null, equivalence is determined by === or ==.
if (isNullOrNonObj(actual) && isNullOrNonObj(expected)) {
// eslint-disable-next-line eqeqeq
return strict ? actual === expected : actual == expected;
}

Expand Down
3 changes: 2 additions & 1 deletion lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ Buffer.allocUnsafeSlow = function(size) {
// If --zero-fill-buffers command line argument is set, a zero-filled
// buffer is returned.
function SlowBuffer(length) {
// eslint-disable-next-line eqeqeq
if (+length != length)
length = 0;
assertSize(+length);
Expand Down Expand Up @@ -306,7 +307,7 @@ function fromObject(obj) {
return b;
}

if (obj != undefined) {
if (obj != null) {
if (obj.length !== undefined || isAnyArrayBuffer(obj.buffer)) {
if (typeof obj.length !== 'number' || obj.length !== obj.length) {
return new FastBuffer();
Expand Down
1 change: 1 addition & 0 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1161,6 +1161,7 @@ fs.chownSync = function(path, uid, gid) {

// converts Date or number to a fractional UNIX timestamp
function toUnixTimestamp(time) {
// eslint-disable-next-line eqeqeq
if (typeof time === 'string' && +time == time) {
return +time;
}
Expand Down
1 change: 1 addition & 0 deletions lib/internal/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ function setupKillAndExit() {
process.kill = function(pid, sig) {
var err;

// eslint-disable-next-line eqeqeq
if (pid != (pid | 0)) {
throw new TypeError('invalid pid');
}
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -1365,7 +1365,7 @@ function getPathFromURLPosix(url) {
}

function getPathFromURL(path) {
if (path == undefined || !path[searchParams] ||
if (path == null || !path[searchParams] ||
!path[searchParams][searchParams]) {
return path;
}
Expand Down
5 changes: 3 additions & 2 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,10 @@ function Socket(options) {
} else if (options.fd !== undefined) {
this._handle = createHandle(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
// See: https://github.com/nodejs/node/pull/11513
// eslint-disable-next-line eqeqeq
if ((options.fd == 1 || options.fd == 2) &&
(this._handle instanceof Pipe) &&
process.platform === 'win32') {
Expand Down Expand Up @@ -748,7 +749,7 @@ Socket.prototype._writeGeneric = function(writev, data, encoding, cb) {

// 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.
if (req.async && this._handle.writeQueueSize != 0)
if (req.async && this._handle.writeQueueSize !== 0)
req.cb = cb;
else
cb();
Expand Down

0 comments on commit 096508d

Please sign in to comment.