Skip to content

Commit 096508d

Browse files
Trottjasnell
authored andcommitted
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: #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>
1 parent 2e5188d commit 096508d

File tree

8 files changed

+13
-4
lines changed

8 files changed

+13
-4
lines changed

.eslintrc.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ rules:
3434
# Best Practices
3535
# http://eslint.org/docs/rules/#best-practices
3636
dot-location: [2, property]
37+
eqeqeq: [2, smart]
3738
no-fallthrough: 2
3839
no-global-assign: 2
3940
no-multi-spaces: 2

lib/_debugger.js

+1
Original file line numberDiff line numberDiff line change
@@ -1391,6 +1391,7 @@ Interface.prototype.setBreakpoint = function(script, line,
13911391
};
13921392
} else {
13931393
// setBreakpoint('scriptname')
1394+
// eslint-disable-next-line eqeqeq
13941395
if (script != +script && !this.client.scripts[script]) {
13951396
var scripts = this.client.scripts;
13961397
for (var id in scripts) {

lib/assert.js

+3
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ assert.ok = ok;
112112
// assert.equal(actual, expected, message_opt);
113113
/* eslint-disable no-restricted-properties */
114114
assert.equal = function equal(actual, expected, message) {
115+
// eslint-disable-next-line eqeqeq
115116
if (actual != expected) fail(actual, expected, message, '==', assert.equal);
116117
};
117118

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

122123
assert.notEqual = function notEqual(actual, expected, message) {
124+
// eslint-disable-next-line eqeqeq
123125
if (actual == expected) {
124126
fail(actual, expected, message, '!=', assert.notEqual);
125127
}
@@ -176,6 +178,7 @@ function _deepEqual(actual, expected, strict, memos) {
176178
// (determined by typeof value !== 'object'),
177179
// or null, equivalence is determined by === or ==.
178180
if (isNullOrNonObj(actual) && isNullOrNonObj(expected)) {
181+
// eslint-disable-next-line eqeqeq
179182
return strict ? actual === expected : actual == expected;
180183
}
181184

lib/buffer.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ Buffer.allocUnsafeSlow = function(size) {
192192
// If --zero-fill-buffers command line argument is set, a zero-filled
193193
// buffer is returned.
194194
function SlowBuffer(length) {
195+
// eslint-disable-next-line eqeqeq
195196
if (+length != length)
196197
length = 0;
197198
assertSize(+length);
@@ -306,7 +307,7 @@ function fromObject(obj) {
306307
return b;
307308
}
308309

309-
if (obj != undefined) {
310+
if (obj != null) {
310311
if (obj.length !== undefined || isAnyArrayBuffer(obj.buffer)) {
311312
if (typeof obj.length !== 'number' || obj.length !== obj.length) {
312313
return new FastBuffer();

lib/fs.js

+1
Original file line numberDiff line numberDiff line change
@@ -1161,6 +1161,7 @@ fs.chownSync = function(path, uid, gid) {
11611161

11621162
// converts Date or number to a fractional UNIX timestamp
11631163
function toUnixTimestamp(time) {
1164+
// eslint-disable-next-line eqeqeq
11641165
if (typeof time === 'string' && +time == time) {
11651166
return +time;
11661167
}

lib/internal/process.js

+1
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ function setupKillAndExit() {
169169
process.kill = function(pid, sig) {
170170
var err;
171171

172+
// eslint-disable-next-line eqeqeq
172173
if (pid != (pid | 0)) {
173174
throw new TypeError('invalid pid');
174175
}

lib/internal/url.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1365,7 +1365,7 @@ function getPathFromURLPosix(url) {
13651365
}
13661366

13671367
function getPathFromURL(path) {
1368-
if (path == undefined || !path[searchParams] ||
1368+
if (path == null || !path[searchParams] ||
13691369
!path[searchParams][searchParams]) {
13701370
return path;
13711371
}

lib/net.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,10 @@ function Socket(options) {
179179
} else if (options.fd !== undefined) {
180180
this._handle = createHandle(options.fd);
181181
this._handle.open(options.fd);
182-
// options.fd can be string (since it user-defined),
182+
// options.fd can be string (since it is user-defined),
183183
// so changing this to === would be semver-major
184184
// See: https://github.com/nodejs/node/pull/11513
185+
// eslint-disable-next-line eqeqeq
185186
if ((options.fd == 1 || options.fd == 2) &&
186187
(this._handle instanceof Pipe) &&
187188
process.platform === 'win32') {
@@ -748,7 +749,7 @@ Socket.prototype._writeGeneric = function(writev, data, encoding, cb) {
748749

749750
// If it was entirely flushed, we can write some more right now.
750751
// However, if more is left in the queue, then wait until that clears.
751-
if (req.async && this._handle.writeQueueSize != 0)
752+
if (req.async && this._handle.writeQueueSize !== 0)
752753
req.cb = cb;
753754
else
754755
cb();

0 commit comments

Comments
 (0)