From 75c7adb7fbd1ddea2b6a9555dcbdcbe0a53f08aa Mon Sep 17 00:00:00 2001 From: Stephen Cresswell <229672+cressie176@users.noreply.github.com> Date: Thu, 4 Sep 2025 16:40:29 +0100 Subject: [PATCH 1/2] Add block scoping to switch statement cases MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add block scoping to switch cases in lib/codec.js, lib/connection.js, lib/channel.js - Add block scoping to switch cases in bin/generate-defs.js and test/codec.js - Enable noSwitchDeclarations lint rule to prevent future violations - Update changelog with switch statement scoping improvements 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- CHANGELOG.md | 1 + bin/generate-defs.js | 6 ++++-- biome.json | 2 +- lib/channel.js | 7 +++++-- lib/codec.js | 8 ++++++-- lib/connection.js | 3 ++- test/codec.js | 3 ++- 7 files changed, 21 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6eee57a8..8297563c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ - Replace all var declarations with let/const for modern JavaScript standards - Ensure parseInt calls use explicit radix parameter for clarity and reliability - Fix precision loss in test data generators by using JavaScript safe integer limits +- Add block scoping to switch statement cases to prevent variable declaration issues ## v0.10.9 - Add support for IPv6 urls diff --git a/bin/generate-defs.js b/bin/generate-defs.js index eae0c447..a9c528b4 100644 --- a/bin/generate-defs.js +++ b/bin/generate-defs.js @@ -495,7 +495,7 @@ function decoderFn(method) { case 'timestamp': println('val = ints.readUInt64BE(buffer, offset); offset += 8;'); break; - case 'bit': + case 'bit': { const bit = 1 << bitsInARow; println('val = !!(buffer[offset] & %d);', bit); if (bitsInARow === 7) { @@ -503,6 +503,7 @@ function decoderFn(method) { bitsInARow = 0; } else bitsInARow++; break; + } case 'longstr': println('len = buffer.readUInt32BE(offset); offset += 4;'); println('val = buffer.subarray(offset, offset + len);'); @@ -644,12 +645,13 @@ function encodePropsFn(props) { println('ints.writeUInt64BE(buffer, val, offset);'); println('offset += 8;'); break; - case 'shortstr': + case 'shortstr': { const v = stringLenVar(p); println('buffer[offset] = %s; offset++;', v); println("buffer.write(val, offset, 'utf8');"); println('offset += %s;', v); break; + } case 'longstr': println('buffer.writeUInt32BE(val.length, offset);'); println('offset += 4;'); diff --git a/biome.json b/biome.json index 826cd997..b906467a 100644 --- a/biome.json +++ b/biome.json @@ -24,7 +24,7 @@ "noUnusedVariables": "error", "noInnerDeclarations": "error", "useParseIntRadix": "error", - "noSwitchDeclarations": "off", + "noSwitchDeclarations": "error", "noInvalidUseBeforeDeclaration": "error", "noPrecisionLoss": "error" }, diff --git a/lib/channel.js b/lib/channel.js index 7e04f9e1..2727a66d 100644 --- a/lib/channel.js +++ b/lib/channel.js @@ -287,7 +287,7 @@ class Channel extends EventEmitter { // The broker can send this if e.g., the queue is deleted. return this.emit('cancel', f.fields); - case defs.ChannelClose: + case defs.ChannelClose: { // Any remote closure is an error to us. Reject the pending reply // with the close frame, so it can see whether it was that // operation that caused it to close. @@ -308,12 +308,14 @@ class Channel extends EventEmitter { const s = stackCapture(emsg); this.toClosed(s); return; + } case defs.BasicFlow: // RabbitMQ doesn't send this, it just blocks the TCP socket return this.closeWithError(f.id, 'Flow not implemented', defs.constants.NOT_IMPLEMENTED, new Error('Flow not implemented')); - default: // assume all other things are replies + default: { + // assume all other things are replies // Resolving the reply may lead to another RPC; to make sure we // don't hold that up, clear this.reply const reply = this.reply; @@ -328,6 +330,7 @@ class Channel extends EventEmitter { this.sendImmediately(send.method, send.fields); } return reply(null, f); + } } } } diff --git a/lib/codec.js b/lib/codec.js index 83fb6593..181fac2c 100644 --- a/lib/codec.js +++ b/lib/codec.js @@ -148,7 +148,8 @@ function encodeFieldValue(buffer, value, offset) { } switch (type) { - case 'string': // no shortstr in field tables + case 'string': { + // no shortstr in field tables const len = Buffer.byteLength(val, 'utf8'); tag('S'); buffer.writeUInt32BE(len, offset); @@ -156,6 +157,7 @@ function encodeFieldValue(buffer, value, offset) { buffer.write(val, offset, 'utf8'); offset += len; break; + } case 'object': if (val === null) { tag('V'); @@ -290,13 +292,15 @@ function decodeFields(slice) { val = slice.readUInt32BE(offset); offset += 4; break; - case 'D': // only positive decimals, apparently. + case 'D': { + // only positive decimals, apparently. const places = slice[offset]; offset++; const digits = slice.readUInt32BE(offset); offset += 4; val = {'!': 'decimal', value: {places: places, digits: digits}}; break; + } case 'T': val = ints.readUInt64BE(slice, offset); offset += 8; diff --git a/lib/connection.js b/lib/connection.js index f0f73216..032f6bf2 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -159,7 +159,7 @@ class Connection extends EventEmitter { case defs.ConnectionClose: bail(new Error(fmt('Handshake terminated by server: %s', closeMsg(reply)))); break; - case defs.ConnectionTune: + case defs.ConnectionTune: { const fields = reply.fields; tunedOptions.frameMax = negotiate(fields.frameMax, allFields.frameMax); tunedOptions.channelMax = negotiate(fields.channelMax, allFields.channelMax); @@ -173,6 +173,7 @@ class Connection extends EventEmitter { } expect(defs.ConnectionOpenOk, onOpenOk); break; + } default: bail( new Error( diff --git a/test/codec.js b/test/codec.js index b54c1074..805ee8d1 100644 --- a/test/codec.js +++ b/test/codec.js @@ -165,12 +165,13 @@ function removeExplicitTypes(input) { case 'decimal': case 'float': return input; - case undefined: + case undefined: { const newObj = {}; for (const k in input) { newObj[k] = removeExplicitTypes(input[k]); } return newObj; + } default: return input.value; } From f26906db101ddf5925aa2c7fdbfb1957b9b8a1d2 Mon Sep 17 00:00:00 2001 From: Stephen Cresswell <229672+cressie176@users.noreply.github.com> Date: Thu, 4 Sep 2025 16:53:03 +0100 Subject: [PATCH 2/2] Simplify biome.json by removing default correctness rules MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit All correctness rules that were explicitly set to "error" are enabled by default in Biome's recommended ruleset. Removing explicit configuration for cleaner, more maintainable config file while preserving same behavior. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- biome.json | 9 --------- 1 file changed, 9 deletions(-) diff --git a/biome.json b/biome.json index b906467a..28fd64f3 100644 --- a/biome.json +++ b/biome.json @@ -19,15 +19,6 @@ "noArguments": "off", "useLiteralKeys": "off" }, - "correctness": { - "noUnusedFunctionParameters": "error", - "noUnusedVariables": "error", - "noInnerDeclarations": "error", - "useParseIntRadix": "error", - "noSwitchDeclarations": "error", - "noInvalidUseBeforeDeclaration": "error", - "noPrecisionLoss": "error" - }, "style": { "useConst": "off", "useNodejsImportProtocol": "off",