From f902fb013741f806a58a230f39086a7c0f47cdff Mon Sep 17 00:00:00 2001 From: Stephen Cresswell <229672+cressie176@users.noreply.github.com> Date: Thu, 4 Sep 2025 19:01:50 +0100 Subject: [PATCH] Refactor assignment-in-expression patterns MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Remove assignments from conditional expressions to improve code clarity and readability. Patterns like while((x = y)) have been refactored to separate assignment from conditional logic using do...while loops or explicit assignments. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- CHANGELOG.md | 1 + biome.json | 1 - lib/channel.js | 16 ++++++++++------ lib/channel_model.js | 5 +++-- lib/connection.js | 11 +++++++---- lib/mux.js | 9 +++++---- test/frame.js | 13 +++++++------ test/mux.js | 4 +++- 8 files changed, 36 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 069141ff..2d640c6b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ - Use modern exponentiation operator (**) instead of Math.pow() - Replace string concatenation with modern template literals - Remove redundant 'use strict' directives as modules are automatically in strict mode +- Refactor assignment-in-expression patterns to improve code clarity and readability ## v0.10.9 - Add support for IPv6 urls diff --git a/biome.json b/biome.json index 8981412e..a567d328 100644 --- a/biome.json +++ b/biome.json @@ -21,7 +21,6 @@ }, "suspicious": { "noRedundantUseStrict": "error", - "noAssignInExpressions": "off", "noAsyncPromiseExecutor": "off", "noDoubleEquals": "off", "noGlobalIsNan": "off", diff --git a/lib/channel.js b/lib/channel.js index f510d7ad..06f90ae6 100644 --- a/lib/channel.js +++ b/lib/channel.js @@ -33,9 +33,10 @@ class Channel extends EventEmitter { }), ); this.on('close', function () { - let cb; - while ((cb = this.unconfirmed.shift())) { + let cb = this.unconfirmed.shift(); + while (cb) { if (cb) cb(new Error('channel closed')); + cb = this.unconfirmed.shift(); } }); // message frame state machine @@ -162,14 +163,17 @@ class Channel extends EventEmitter { } _rejectPending() { - function rej(r) { + function reject(r) { r(new Error('Channel ended, no reply will be forthcoming')); } - if (this.reply !== null) rej(this.reply); + if (this.reply !== null) reject(this.reply); this.reply = null; - let discard; - while ((discard = this.pending.shift())) rej(discard.reply); + let discard = this.pending.shift(); + while (discard) { + if (discard) reject(discard.reply); + discard = this.pending.shift(); + } this.pending = null; // so pushes will break } diff --git a/lib/channel_model.js b/lib/channel_model.js index 4a8a7d27..797bc8f2 100644 --- a/lib/channel_model.js +++ b/lib/channel_model.js @@ -249,9 +249,10 @@ class ConfirmChannel extends Channel { }); // Channel closed if (!this.pending) { - let cb; - while ((cb = this.unconfirmed.shift())) { + let cb = this.unconfirmed.shift(); + while (cb) { if (cb) cb(new Error('channel closed')); + cb = this.unconfirmed.shift(); } } return Promise.all(awaiting); diff --git a/lib/connection.js b/lib/connection.js index 04b862cb..424b8a1d 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -31,8 +31,8 @@ class Connection extends EventEmitter { constructor(underlying) { super(); - const stream = (this.stream = wrapStream(underlying)); - this.muxer = new Mux(stream); + this.stream = wrapStream(underlying); + this.muxer = new Mux(this.stream); // frames this.rest = Buffer.alloc(0); @@ -406,8 +406,11 @@ class Connection extends EventEmitter { function go() { try { - let f; - while ((f = self.recvFrame())) self.accept(f); + let frame = self.recvFrame(); + while (frame) { + self.accept(frame); + frame = self.recvFrame(); + } } catch (e) { self.emit('frameError', e); } diff --git a/lib/mux.js b/lib/mux.js index 0ece5aa9..f26ff525 100644 --- a/lib/mux.js +++ b/lib/mux.js @@ -41,13 +41,14 @@ class Mux { // Try to read a chunk from each stream in turn, until all streams // are empty, or we exhaust our ability to accept chunks. function roundrobin(streams) { - let s; - while (accepting && (s = streams.shift())) { - const chunk = s.read(); + let stream = streams.shift(); + while (accepting && stream) { + const chunk = stream.read(); if (chunk !== null) { accepting = out.write(chunk); - streams.push(s); + streams.push(stream); } + stream = streams.shift(); } } diff --git a/test/frame.js b/test/frame.js index 5535040c..59ac7c39 100644 --- a/test/frame.js +++ b/test/frame.js @@ -192,15 +192,16 @@ suite('Content framing', function () { const frames = new Frames(input); frames.frameMax = max; frames.sendMessage(0, defs.BasicDeliver, content.method, defs.BasicProperties, content.header, content.body); - let f, - _i = 0, - largest = 0; - while ((f = input.read())) { + let _i = 0; + let largest = 0; + let frame = input.read(); + while (frame) { _i++; - if (f.length > largest) largest = f.length; - if (f.length > max) { + if (frame.length > largest) largest = frame.length; + if (frame.length > max) { return false; } + frame = input.read(); } // The ratio of frames to 'contents' should always be >= 2 // (one properties frame and at least one content frame); > 2 diff --git a/test/mux.js b/test/mux.js index 2394f841..caf1a5c5 100644 --- a/test/mux.js +++ b/test/mux.js @@ -179,7 +179,9 @@ test('unpipe', function (done) { schedule(function () { // exhaust so that 'end' fires let v; - while ((v = input.read())); + do { + v = input.read(); + } while (v); }); }); });