Skip to content

Commit 9e5d691

Browse files
ZYSzysBridgeAR
authored andcommitted
lib: introduce no-mixed-operators eslint rule to lib
PR-URL: #29834 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]>
1 parent 3f15378 commit 9e5d691

20 files changed

+61
-58
lines changed

lib/.eslintrc.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ env:
44
rules:
55
prefer-object-spread: error
66
no-buffer-constructor: error
7+
no-mixed-operators:
8+
- error
9+
- groups: [[ "&&", "||" ]]
710
no-restricted-globals:
811
- error
912
- name: JSON

lib/_http_client.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ function ClientRequest(input, options, cb) {
144144
}
145145

146146
const defaultPort = options.defaultPort ||
147-
this.agent && this.agent.defaultPort;
147+
(this.agent && this.agent.defaultPort);
148148

149149
const port = options.port = options.port || defaultPort || 80;
150150
const host = options.host = validateHost(options.hostname, 'hostname') ||

lib/_stream_readable.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ function readableAddChunk(stream, chunk, encoding, addToFront) {
260260
er = chunkInvalid(state, chunk);
261261
if (er) {
262262
errorOrDestroy(stream, er);
263-
} else if (state.objectMode || chunk && chunk.length > 0) {
263+
} else if (state.objectMode || (chunk && chunk.length > 0)) {
264264
if (typeof chunk !== 'string' &&
265265
!state.objectMode &&
266266
// Do not use Object.getPrototypeOf as it is slower since V8 7.3.

lib/_tls_wrap.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ function loadSession(hello) {
149149

150150
if (hello.sessionId.length <= 0 ||
151151
hello.tlsTicket ||
152-
owner.server &&
153-
!owner.server.emit('resumeSession', hello.sessionId, onSession)) {
152+
(owner.server &&
153+
!owner.server.emit('resumeSession', hello.sessionId, onSession))) {
154154
// Sessions without identifiers can't be resumed.
155155
// Sessions with tickets can be resumed directly from the ticket, no server
156156
// session storage is necessary.

lib/assert.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -647,9 +647,9 @@ function checkIsPromise(obj) {
647647
// way. Do not accept thenables that use a function as `obj` and that have no
648648
// `catch` handler.
649649
return isPromise(obj) ||
650-
obj !== null && typeof obj === 'object' &&
650+
(obj !== null && typeof obj === 'object' &&
651651
typeof obj.then === 'function' &&
652-
typeof obj.catch === 'function';
652+
typeof obj.catch === 'function');
653653
}
654654

655655
async function waitForActual(promiseFn) {

lib/internal/assert/assertion_error.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ class AssertionError extends Error {
349349
// In case "actual" is an object or a function, it should not be
350350
// reference equal.
351351
if (operator === 'notStrictEqual' &&
352-
(typeof actual === 'object' && actual !== null ||
352+
((typeof actual === 'object' && actual !== null) ||
353353
typeof actual === 'function')) {
354354
base = kReadableOperator.notStrictEqualObject;
355355
}

lib/internal/child_process.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -934,7 +934,7 @@ function getValidStdio(stdio, sync) {
934934

935935
if (stdio === 'ignore') {
936936
acc.push({ type: 'ignore' });
937-
} else if (stdio === 'pipe' || typeof stdio === 'number' && stdio < 0) {
937+
} else if (stdio === 'pipe' || (typeof stdio === 'number' && stdio < 0)) {
938938
var a = {
939939
type: 'pipe',
940940
readable: i === 0,

lib/internal/cluster/child.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ cluster._getServer = function(obj, options, cb) {
100100
cluster.worker.state = 'listening';
101101
const address = obj.address();
102102
message.act = 'listening';
103-
message.port = address && address.port || options.port;
103+
message.port = (address && address.port) || options.port;
104104
send(message);
105105
});
106106
};

lib/internal/errors.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -656,8 +656,8 @@ const fatalExceptionStackEnhancers = {
656656
colors: defaultColors
657657
}
658658
} = lazyInternalUtilInspect();
659-
const colors = internalBinding('util').guessHandleType(2) === 'TTY' &&
660-
require('internal/tty').hasColors() ||
659+
const colors = (internalBinding('util').guessHandleType(2) === 'TTY' &&
660+
require('internal/tty').hasColors()) ||
661661
defaultColors;
662662
try {
663663
return inspect(error, { colors });

lib/internal/fs/utils.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,9 @@ const nullCheck = hideStackFrames((path, propName, throwError = true) => {
204204
const pathIsUint8Array = isUint8Array(path);
205205

206206
// We can only perform meaningful checks on strings and Uint8Arrays.
207-
if (!pathIsString && !pathIsUint8Array ||
208-
pathIsString && !path.includes('\u0000') ||
209-
pathIsUint8Array && !path.includes(0)) {
207+
if ((!pathIsString && !pathIsUint8Array) ||
208+
(pathIsString && !path.includes('\u0000')) ||
209+
(pathIsUint8Array && !path.includes(0))) {
210210
return;
211211
}
212212

lib/internal/modules/cjs/loader.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,7 @@ Module._resolveFilename = function(request, parent, isMain, options) {
733733
if (Array.isArray(options.paths)) {
734734
const isRelative = request.startsWith('./') ||
735735
request.startsWith('../') ||
736-
(isWindows && request.startsWith('.\\') ||
736+
((isWindows && request.startsWith('.\\')) ||
737737
request.startsWith('..\\'));
738738

739739
if (isRelative) {

lib/internal/readline/utils.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -108,30 +108,30 @@ if (internalBinding('config').hasIntl) {
108108
code === 0x2329 || // LEFT-POINTING ANGLE BRACKET
109109
code === 0x232a || // RIGHT-POINTING ANGLE BRACKET
110110
// CJK Radicals Supplement .. Enclosed CJK Letters and Months
111-
code >= 0x2e80 && code <= 0x3247 && code !== 0x303f ||
111+
(code >= 0x2e80 && code <= 0x3247 && code !== 0x303f) ||
112112
// Enclosed CJK Letters and Months .. CJK Unified Ideographs Extension A
113-
code >= 0x3250 && code <= 0x4dbf ||
113+
(code >= 0x3250 && code <= 0x4dbf) ||
114114
// CJK Unified Ideographs .. Yi Radicals
115-
code >= 0x4e00 && code <= 0xa4c6 ||
115+
(code >= 0x4e00 && code <= 0xa4c6) ||
116116
// Hangul Jamo Extended-A
117-
code >= 0xa960 && code <= 0xa97c ||
117+
(code >= 0xa960 && code <= 0xa97c) ||
118118
// Hangul Syllables
119-
code >= 0xac00 && code <= 0xd7a3 ||
119+
(code >= 0xac00 && code <= 0xd7a3) ||
120120
// CJK Compatibility Ideographs
121-
code >= 0xf900 && code <= 0xfaff ||
121+
(code >= 0xf900 && code <= 0xfaff) ||
122122
// Vertical Forms
123-
code >= 0xfe10 && code <= 0xfe19 ||
123+
(code >= 0xfe10 && code <= 0xfe19) ||
124124
// CJK Compatibility Forms .. Small Form Variants
125-
code >= 0xfe30 && code <= 0xfe6b ||
125+
(code >= 0xfe30 && code <= 0xfe6b) ||
126126
// Halfwidth and Fullwidth Forms
127-
code >= 0xff01 && code <= 0xff60 ||
128-
code >= 0xffe0 && code <= 0xffe6 ||
127+
(code >= 0xff01 && code <= 0xff60) ||
128+
(code >= 0xffe0 && code <= 0xffe6) ||
129129
// Kana Supplement
130-
code >= 0x1b000 && code <= 0x1b001 ||
130+
(code >= 0x1b000 && code <= 0x1b001) ||
131131
// Enclosed Ideographic Supplement
132-
code >= 0x1f200 && code <= 0x1f251 ||
132+
(code >= 0x1f200 && code <= 0x1f251) ||
133133
// CJK Unified Ideographs Extension B .. Tertiary Ideographic Plane
134-
code >= 0x20000 && code <= 0x3fffd
134+
(code >= 0x20000 && code <= 0x3fffd)
135135
);
136136
};
137137
}

lib/internal/tty.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ function getColorDepth(env = process.env) {
200200

201201
function hasColors(count, env) {
202202
if (env === undefined &&
203-
(count === undefined || typeof count === 'object' && count !== null)) {
203+
(count === undefined || (typeof count === 'object' && count !== null))) {
204204
env = count;
205205
count = 16;
206206
} else {

lib/internal/url.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1363,7 +1363,7 @@ function pathToFileURL(filepath) {
13631363
// path.resolve strips trailing slashes so we must add them back
13641364
const filePathLast = filepath.charCodeAt(filepath.length - 1);
13651365
if ((filePathLast === CHAR_FORWARD_SLASH ||
1366-
isWindows && filePathLast === CHAR_BACKWARD_SLASH) &&
1366+
(isWindows && filePathLast === CHAR_BACKWARD_SLASH)) &&
13671367
resolved[resolved.length - 1] !== path.sep)
13681368
resolved += '/';
13691369
const outURL = new URL('file://');

lib/internal/util/comparisons.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ function keyCheck(val1, val2, strict, memos, iterationType, aKeys) {
294294

295295
if (aKeys.length === 0 &&
296296
(iterationType === kNoIterator ||
297-
iterationType === kIsArray && val1.length === 0 ||
297+
(iterationType === kIsArray && val1.length === 0) ||
298298
val1.size === 0)) {
299299
return true;
300300
}
@@ -383,7 +383,7 @@ function mapMightHaveLoosePrim(a, b, prim, item, memo) {
383383
return altValue;
384384
}
385385
const curB = b.get(altValue);
386-
if (curB === undefined && !b.has(altValue) ||
386+
if ((curB === undefined && !b.has(altValue)) ||
387387
!innerDeepEqual(item, curB, false, memo)) {
388388
return false;
389389
}
@@ -470,7 +470,7 @@ function mapEquiv(a, b, strict, memo) {
470470
// By directly retrieving the value we prevent another b.has(key) check in
471471
// almost all possible cases.
472472
const item2 = b.get(key);
473-
if ((item2 === undefined && !b.has(key) ||
473+
if (((item2 === undefined && !b.has(key)) ||
474474
!innerDeepEqual(item1, item2, strict, memo))) {
475475
if (strict)
476476
return false;

lib/internal/util/inspect.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -599,10 +599,10 @@ function formatRaw(ctx, value, recurseTimes, typedArray) {
599599
// Only list the tag in case it's non-enumerable / not an own property.
600600
// Otherwise we'd print this twice.
601601
if (typeof tag !== 'string' ||
602-
tag !== '' &&
602+
(tag !== '' &&
603603
(ctx.showHidden ? hasOwnProperty : propertyIsEnumerable)(
604604
value, Symbol.toStringTag
605-
)) {
605+
))) {
606606
tag = '';
607607
}
608608
let base = '';
@@ -686,7 +686,7 @@ function formatRaw(ctx, value, recurseTimes, typedArray) {
686686
const prefix = getPrefix(constructor, tag, 'RegExp');
687687
if (prefix !== 'RegExp ')
688688
base = `${prefix}${base}`;
689-
if (keys.length === 0 || recurseTimes > ctx.depth && ctx.depth !== null)
689+
if (keys.length === 0 || (recurseTimes > ctx.depth && ctx.depth !== null))
690690
return ctx.stylize(base, 'regexp');
691691
} else if (isDate(value)) {
692692
// Make dates with properties first say the date
@@ -911,14 +911,14 @@ function formatError(err, constructor, tag, ctx) {
911911
const name = err.name || 'Error';
912912
let len = name.length;
913913
if (constructor === null ||
914-
name.endsWith('Error') &&
914+
(name.endsWith('Error') &&
915915
stack.startsWith(name) &&
916-
(stack.length === len || stack[len] === ':' || stack[len] === '\n')) {
916+
(stack.length === len || stack[len] === ':' || stack[len] === '\n'))) {
917917
let fallback = 'Error';
918918
if (constructor === null) {
919919
const start = stack.match(/^([A-Z][a-z_ A-Z0-9[\]()-]+)(?::|\n {4}at)/) ||
920920
stack.match(/^([a-z_A-Z0-9-]*Error)$/);
921-
fallback = start && start[1] || '';
921+
fallback = (start && start[1]) || '';
922922
len = fallback.length;
923923
fallback = fallback || 'Error';
924924
}
@@ -936,7 +936,7 @@ function formatError(err, constructor, tag, ctx) {
936936
}
937937
}
938938
// Ignore the error message if it's contained in the stack.
939-
let pos = err.message && stack.indexOf(err.message) || -1;
939+
let pos = (err.message && stack.indexOf(err.message)) || -1;
940940
if (pos !== -1)
941941
pos += err.message.length;
942942
// Wrap the error in brackets in case it has no stack trace.
@@ -1416,8 +1416,8 @@ function formatProperty(ctx, value, recurseTimes, key, type) {
14161416
const s = ctx.stylize;
14171417
const sp = 'special';
14181418
if (ctx.getters && (ctx.getters === true ||
1419-
ctx.getters === 'get' && desc.set === undefined ||
1420-
ctx.getters === 'set' && desc.set !== undefined)) {
1419+
(ctx.getters === 'get' && desc.set === undefined) ||
1420+
(ctx.getters === 'set' && desc.set !== undefined))) {
14211421
try {
14221422
const tmp = value[key];
14231423
ctx.indentationLvl += 2;
@@ -1594,15 +1594,15 @@ function formatWithOptions(inspectOptions, ...args) {
15941594
let constr;
15951595
if (typeof tempArg !== 'object' ||
15961596
tempArg === null ||
1597-
typeof tempArg.toString === 'function' &&
1597+
(typeof tempArg.toString === 'function' &&
15981598
// A direct own property.
15991599
(hasOwnProperty(tempArg, 'toString') ||
16001600
// A direct own property on the constructor prototype in
16011601
// case the constructor is not an built-in object.
1602-
(constr = tempArg.constructor) &&
1602+
((constr = tempArg.constructor) &&
16031603
!builtInObjects.has(constr.name) &&
16041604
constr.prototype &&
1605-
hasOwnProperty(constr.prototype, 'toString'))) {
1605+
hasOwnProperty(constr.prototype, 'toString'))))) {
16061606
tempStr = String(tempArg);
16071607
} else {
16081608
tempStr = inspect(tempArg, {

lib/path.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ function isPosixPathSeparator(code) {
4444
}
4545

4646
function isWindowsDeviceRoot(code) {
47-
return code >= CHAR_UPPERCASE_A && code <= CHAR_UPPERCASE_Z ||
48-
code >= CHAR_LOWERCASE_A && code <= CHAR_LOWERCASE_Z;
47+
return (code >= CHAR_UPPERCASE_A && code <= CHAR_UPPERCASE_Z) ||
48+
(code >= CHAR_LOWERCASE_A && code <= CHAR_LOWERCASE_Z);
4949
}
5050

5151
// Resolves . and .. elements in a path with directory names
@@ -155,8 +155,8 @@ const win32 = {
155155
// Verify that a cwd was found and that it actually points
156156
// to our drive. If not, default to the drive's root.
157157
if (path === undefined ||
158-
path.slice(0, 2).toLowerCase() !== resolvedDevice.toLowerCase() &&
159-
path.charCodeAt(2) === CHAR_BACKWARD_SLASH) {
158+
(path.slice(0, 2).toLowerCase() !== resolvedDevice.toLowerCase() &&
159+
path.charCodeAt(2) === CHAR_BACKWARD_SLASH)) {
160160
path = `${resolvedDevice}\\`;
161161
}
162162
}
@@ -358,10 +358,10 @@ const win32 = {
358358
const code = path.charCodeAt(0);
359359
return isPathSeparator(code) ||
360360
// Possible device root
361-
len > 2 &&
361+
(len > 2 &&
362362
isWindowsDeviceRoot(code) &&
363363
path.charCodeAt(1) === CHAR_COLON &&
364-
isPathSeparator(path.charCodeAt(2));
364+
isPathSeparator(path.charCodeAt(2)));
365365
},
366366

367367
join(...args) {

lib/readline.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ Interface.prototype._wordRight = function() {
602602
function charLengthLeft(str, i) {
603603
if (i <= 0)
604604
return 0;
605-
if (i > 1 && str.codePointAt(i - 2) >= kUTF16SurrogateThreshold ||
605+
if ((i > 1 && str.codePointAt(i - 2) >= kUTF16SurrogateThreshold) ||
606606
str.codePointAt(i - 1) >= kUTF16SurrogateThreshold) {
607607
return 2;
608608
}

lib/url.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -765,13 +765,13 @@ Url.prototype.resolveObject = function resolveObject(relative) {
765765

766766
const isSourceAbs = (result.pathname && result.pathname.charAt(0) === '/');
767767
const isRelAbs = (
768-
relative.host || relative.pathname && relative.pathname.charAt(0) === '/'
768+
relative.host || (relative.pathname && relative.pathname.charAt(0) === '/')
769769
);
770770
var mustEndAbs = (isRelAbs || isSourceAbs ||
771771
(result.host && relative.pathname));
772772
const removeAllDots = mustEndAbs;
773-
var srcPath = result.pathname && result.pathname.split('/') || [];
774-
const relPath = relative.pathname && relative.pathname.split('/') || [];
773+
var srcPath = (result.pathname && result.pathname.split('/')) || [];
774+
const relPath = (relative.pathname && relative.pathname.split('/')) || [];
775775
const noLeadingSlashes = result.protocol &&
776776
!slashedProtocol.has(result.protocol);
777777

@@ -869,8 +869,8 @@ Url.prototype.resolveObject = function resolveObject(relative) {
869869
// then it must NOT get a trailing slash.
870870
var last = srcPath.slice(-1)[0];
871871
const hasTrailingSlash = (
872-
(result.host || relative.host || srcPath.length > 1) &&
873-
(last === '.' || last === '..') || last === '');
872+
((result.host || relative.host || srcPath.length > 1) &&
873+
(last === '.' || last === '..')) || last === '');
874874

875875
// Strip single dots, resolve double dots to parent dir
876876
// if the path tries to go above the root, `up` ends up > 0

lib/util.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ function isFunction(arg) {
9494

9595
function isPrimitive(arg) {
9696
return arg === null ||
97-
typeof arg !== 'object' && typeof arg !== 'function';
97+
(typeof arg !== 'object' && typeof arg !== 'function');
9898
}
9999

100100
function pad(n) {

0 commit comments

Comments
 (0)