From f78246b5f432062063d4331a87a2a2b6e56d593d Mon Sep 17 00:00:00 2001 From: achingbrain Date: Fri, 18 Sep 2026 10:21:54 +0300 Subject: [PATCH 1/2] fix: only declare decode opts if used Decode options are only used for repeated/map fields so only declare the `opts` variable in decode/stream functions if the message has repeated/map or sub-message fields, otherwise the generated code can fail linting checks for unused variables. Fixes #130 --- packages/protons/src/types/message.ts | 10 ++++++-- packages/protons/test/fixtures/basic.ts | 8 +++---- packages/protons/test/fixtures/bitswap.ts | 12 +++++----- .../test/fixtures/custom-option-jstype.ts | 8 +++---- packages/protons/test/fixtures/daemon.ts | 24 +++++++++---------- packages/protons/test/fixtures/dht.ts | 4 ++-- packages/protons/test/fixtures/maps.ts | 16 ++++++------- packages/protons/test/fixtures/noise.ts | 8 +++---- packages/protons/test/fixtures/oneof.ts | 8 +++---- packages/protons/test/fixtures/optional.ts | 4 ++-- packages/protons/test/fixtures/peer.ts | 8 +++---- packages/protons/test/fixtures/proto2.ts | 4 ++-- .../protons/test/fixtures/protons-options.ts | 4 ++-- packages/protons/test/fixtures/singular.ts | 4 ++-- packages/protons/test/fixtures/streaming.ts | 8 +++---- packages/protons/test/fixtures/test.ts | 4 ++-- 16 files changed, 70 insertions(+), 64 deletions(-) diff --git a/packages/protons/src/types/message.ts b/packages/protons/src/types/message.ts index 66b7e43..c4f0e60 100644 --- a/packages/protons/src/types/message.ts +++ b/packages/protons/src/types/message.ts @@ -296,6 +296,12 @@ export interface ${this.pbType}Input { streamGeneratorEvents.push('{}') } + // the only decode options are repeated field limits, so only declare the + // variable if we have repeated fields or a sub message field to pass + // options to + const decodeOpts = this.fields.some(field => field instanceof ArrayField || field instanceof MapField || field instanceof MessageField) + ? ', opts = {}' : '' + interfaceCodecDef = ` let _codec: Codec<${this.pbType}, ${this.pbType}Input> @@ -309,7 +315,7 @@ export interface ${this.pbType}Input { if (opts.lengthDelimited !== false) { w.ldelim() } - }, (r, length, opts = {}) => { + }, (r, length${decodeOpts}) => { const obj: any = {${this.createDefaultObject()}} const end = length == null ? r.len : r.pos + length @@ -326,7 +332,7 @@ export interface ${this.pbType}Input { } ${enforceOneOfDecoding === '' ? '' : `${enforceOneOfDecoding}\n`} return obj - }, function * (r, length, prefix, opts = {}) { + }, function * (r, length, prefix${decodeOpts}) { ${this.createLimitObject()}const end = length == null ? r.len : r.pos + length if (prefix !== '.') { diff --git a/packages/protons/test/fixtures/basic.ts b/packages/protons/test/fixtures/basic.ts index e53c9a7..8037ca8 100644 --- a/packages/protons/test/fixtures/basic.ts +++ b/packages/protons/test/fixtures/basic.ts @@ -35,7 +35,7 @@ export namespace Basic { if (opts.lengthDelimited !== false) { w.ldelim() } - }, (r, length, opts = {}) => { + }, (r, length) => { const obj: any = { num: 0 } @@ -62,7 +62,7 @@ export namespace Basic { } return obj - }, function * (r, length, prefix, opts = {}) { + }, function * (r, length, prefix) { const end = length == null ? r.len : r.pos + length if (prefix !== '.') { @@ -151,7 +151,7 @@ export namespace Empty { if (opts.lengthDelimited !== false) { w.ldelim() } - }, (r, length, opts = {}) => { + }, (r, length) => { const obj: any = {} const end = length == null ? r.len : r.pos + length @@ -168,7 +168,7 @@ export namespace Empty { } return obj - }, function * (r, length, prefix, opts = {}) { + }, function * (r, length, prefix) { const end = length == null ? r.len : r.pos + length if (prefix !== '.') { diff --git a/packages/protons/test/fixtures/bitswap.ts b/packages/protons/test/fixtures/bitswap.ts index 89d434a..f891c22 100644 --- a/packages/protons/test/fixtures/bitswap.ts +++ b/packages/protons/test/fixtures/bitswap.ts @@ -101,7 +101,7 @@ export namespace Message { if (opts.lengthDelimited !== false) { w.ldelim() } - }, (r, length, opts = {}) => { + }, (r, length) => { const obj: any = { block: uint8ArrayAlloc(0), priority: 0, @@ -143,7 +143,7 @@ export namespace Message { } return obj - }, function * (r, length, prefix, opts = {}) { + }, function * (r, length, prefix) { const end = length == null ? r.len : r.pos + length if (prefix !== '.') { @@ -468,7 +468,7 @@ export namespace Message { if (opts.lengthDelimited !== false) { w.ldelim() } - }, (r, length, opts = {}) => { + }, (r, length) => { const obj: any = { prefix: uint8ArrayAlloc(0), data: uint8ArrayAlloc(0) @@ -496,7 +496,7 @@ export namespace Message { } return obj - }, function * (r, length, prefix, opts = {}) { + }, function * (r, length, prefix) { const end = length == null ? r.len : r.pos + length if (prefix !== '.') { @@ -617,7 +617,7 @@ export namespace Message { if (opts.lengthDelimited !== false) { w.ldelim() } - }, (r, length, opts = {}) => { + }, (r, length) => { const obj: any = { cid: uint8ArrayAlloc(0), type: BlockPresenceType.Have @@ -645,7 +645,7 @@ export namespace Message { } return obj - }, function * (r, length, prefix, opts = {}) { + }, function * (r, length, prefix) { const end = length == null ? r.len : r.pos + length if (prefix !== '.') { diff --git a/packages/protons/test/fixtures/custom-option-jstype.ts b/packages/protons/test/fixtures/custom-option-jstype.ts index 536f5c8..72147ec 100644 --- a/packages/protons/test/fixtures/custom-option-jstype.ts +++ b/packages/protons/test/fixtures/custom-option-jstype.ts @@ -58,7 +58,7 @@ export namespace CustomOptionNumber { if (opts.lengthDelimited !== false) { w.ldelim() } - }, (r, length, opts = {}) => { + }, (r, length) => { const obj: any = { key: 0, value: 0 @@ -86,7 +86,7 @@ export namespace CustomOptionNumber { } return obj - }, function * (r, length, prefix, opts = {}) { + }, function * (r, length, prefix) { const end = length == null ? r.len : r.pos + length if (prefix !== '.') { @@ -523,7 +523,7 @@ export namespace CustomOptionString { if (opts.lengthDelimited !== false) { w.ldelim() } - }, (r, length, opts = {}) => { + }, (r, length) => { const obj: any = { key: '', value: '' @@ -551,7 +551,7 @@ export namespace CustomOptionString { } return obj - }, function * (r, length, prefix, opts = {}) { + }, function * (r, length, prefix) { const end = length == null ? r.len : r.pos + length if (prefix !== '.') { diff --git a/packages/protons/test/fixtures/daemon.ts b/packages/protons/test/fixtures/daemon.ts index bf6e293..421f75d 100644 --- a/packages/protons/test/fixtures/daemon.ts +++ b/packages/protons/test/fixtures/daemon.ts @@ -1672,7 +1672,7 @@ export namespace ErrorResponse { if (opts.lengthDelimited !== false) { w.ldelim() } - }, (r, length, opts = {}) => { + }, (r, length) => { const obj: any = { msg: '' } @@ -1695,7 +1695,7 @@ export namespace ErrorResponse { } return obj - }, function * (r, length, prefix, opts = {}) { + }, function * (r, length, prefix) { const end = length == null ? r.len : r.pos + length if (prefix !== '.') { @@ -1795,7 +1795,7 @@ export namespace StreamInfo { if (opts.lengthDelimited !== false) { w.ldelim() } - }, (r, length, opts = {}) => { + }, (r, length) => { const obj: any = { peer: uint8ArrayAlloc(0), addr: uint8ArrayAlloc(0), @@ -1828,7 +1828,7 @@ export namespace StreamInfo { } return obj - }, function * (r, length, prefix, opts = {}) { + }, function * (r, length, prefix) { const end = length == null ? r.len : r.pos + length if (prefix !== '.') { @@ -2010,7 +2010,7 @@ export namespace DHTRequest { if (opts.lengthDelimited !== false) { w.ldelim() } - }, (r, length, opts = {}) => { + }, (r, length) => { const obj: any = { type: Type.FIND_PEER } @@ -2057,7 +2057,7 @@ export namespace DHTRequest { } return obj - }, function * (r, length, prefix, opts = {}) { + }, function * (r, length, prefix) { const end = length == null ? r.len : r.pos + length if (prefix !== '.') { @@ -2597,7 +2597,7 @@ export namespace ConnManagerRequest { if (opts.lengthDelimited !== false) { w.ldelim() } - }, (r, length, opts = {}) => { + }, (r, length) => { const obj: any = { type: Type.TAG_PEER } @@ -2632,7 +2632,7 @@ export namespace ConnManagerRequest { } return obj - }, function * (r, length, prefix, opts = {}) { + }, function * (r, length, prefix) { const end = length == null ? r.len : r.pos + length if (prefix !== '.') { @@ -2754,7 +2754,7 @@ export namespace DisconnectRequest { if (opts.lengthDelimited !== false) { w.ldelim() } - }, (r, length, opts = {}) => { + }, (r, length) => { const obj: any = { peer: uint8ArrayAlloc(0) } @@ -2777,7 +2777,7 @@ export namespace DisconnectRequest { } return obj - }, function * (r, length, prefix, opts = {}) { + }, function * (r, length, prefix) { const end = length == null ? r.len : r.pos + length if (prefix !== '.') { @@ -2897,7 +2897,7 @@ export namespace PSRequest { if (opts.lengthDelimited !== false) { w.ldelim() } - }, (r, length, opts = {}) => { + }, (r, length) => { const obj: any = { type: Type.GET_TOPICS } @@ -2928,7 +2928,7 @@ export namespace PSRequest { } return obj - }, function * (r, length, prefix, opts = {}) { + }, function * (r, length, prefix) { const end = length == null ? r.len : r.pos + length if (prefix !== '.') { diff --git a/packages/protons/test/fixtures/dht.ts b/packages/protons/test/fixtures/dht.ts index 2e8f4a6..f90b65d 100644 --- a/packages/protons/test/fixtures/dht.ts +++ b/packages/protons/test/fixtures/dht.ts @@ -56,7 +56,7 @@ export namespace Record { if (opts.lengthDelimited !== false) { w.ldelim() } - }, (r, length, opts = {}) => { + }, (r, length) => { const obj: any = {} const end = length == null ? r.len : r.pos + length @@ -93,7 +93,7 @@ export namespace Record { } return obj - }, function * (r, length, prefix, opts = {}) { + }, function * (r, length, prefix) { const end = length == null ? r.len : r.pos + length if (prefix !== '.') { diff --git a/packages/protons/test/fixtures/maps.ts b/packages/protons/test/fixtures/maps.ts index a4dd597..ea4215b 100644 --- a/packages/protons/test/fixtures/maps.ts +++ b/packages/protons/test/fixtures/maps.ts @@ -237,7 +237,7 @@ export namespace MapTypes { if (opts.lengthDelimited !== false) { w.ldelim() } - }, (r, length, opts = {}) => { + }, (r, length) => { const obj: any = { key: '', value: '' @@ -265,7 +265,7 @@ export namespace MapTypes { } return obj - }, function * (r, length, prefix, opts = {}) { + }, function * (r, length, prefix) { const end = length == null ? r.len : r.pos + length if (prefix !== '.') { @@ -370,7 +370,7 @@ export namespace MapTypes { if (opts.lengthDelimited !== false) { w.ldelim() } - }, (r, length, opts = {}) => { + }, (r, length) => { const obj: any = { key: 0, value: 0 @@ -398,7 +398,7 @@ export namespace MapTypes { } return obj - }, function * (r, length, prefix, opts = {}) { + }, function * (r, length, prefix) { const end = length == null ? r.len : r.pos + length if (prefix !== '.') { @@ -503,7 +503,7 @@ export namespace MapTypes { if (opts.lengthDelimited !== false) { w.ldelim() } - }, (r, length, opts = {}) => { + }, (r, length) => { const obj: any = { key: false, value: false @@ -531,7 +531,7 @@ export namespace MapTypes { } return obj - }, function * (r, length, prefix, opts = {}) { + }, function * (r, length, prefix) { const end = length == null ? r.len : r.pos + length if (prefix !== '.') { @@ -786,7 +786,7 @@ export namespace MapTypes { if (opts.lengthDelimited !== false) { w.ldelim() } - }, (r, length, opts = {}) => { + }, (r, length) => { const obj: any = { key: '', value: EnumValue.NO_VALUE @@ -814,7 +814,7 @@ export namespace MapTypes { } return obj - }, function * (r, length, prefix, opts = {}) { + }, function * (r, length, prefix) { const end = length == null ? r.len : r.pos + length if (prefix !== '.') { diff --git a/packages/protons/test/fixtures/noise.ts b/packages/protons/test/fixtures/noise.ts index 0810f54..c7ba031 100644 --- a/packages/protons/test/fixtures/noise.ts +++ b/packages/protons/test/fixtures/noise.ts @@ -48,7 +48,7 @@ export namespace pb { if (opts.lengthDelimited !== false) { w.ldelim() } - }, (r, length, opts = {}) => { + }, (r, length) => { const obj: any = { identityKey: uint8ArrayAlloc(0), identitySig: uint8ArrayAlloc(0), @@ -81,7 +81,7 @@ export namespace pb { } return obj - }, function * (r, length, prefix, opts = {}) { + }, function * (r, length, prefix) { const end = length == null ? r.len : r.pos + length if (prefix !== '.') { @@ -177,7 +177,7 @@ export namespace pb { if (opts.lengthDelimited !== false) { w.ldelim() } - }, (r, length, opts = {}) => { + }, (r, length) => { const obj: any = {} const end = length == null ? r.len : r.pos + length @@ -194,7 +194,7 @@ export namespace pb { } return obj - }, function * (r, length, prefix, opts = {}) { + }, function * (r, length, prefix) { const end = length == null ? r.len : r.pos + length if (prefix !== '.') { diff --git a/packages/protons/test/fixtures/oneof.ts b/packages/protons/test/fixtures/oneof.ts index 3bd355d..6368580 100644 --- a/packages/protons/test/fixtures/oneof.ts +++ b/packages/protons/test/fixtures/oneof.ts @@ -90,7 +90,7 @@ export namespace OneOfMessage { if (opts.lengthDelimited !== false) { w.ldelim() } - }, (r, length, opts = {}) => { + }, (r, length) => { const obj: any = { fieldFive: '' } @@ -145,7 +145,7 @@ export namespace OneOfMessage { } return obj - }, function * (r, length, prefix, opts = {}) { + }, function * (r, length, prefix) { const end = length == null ? r.len : r.pos + length if (prefix !== '.') { @@ -307,7 +307,7 @@ export namespace MessageWithoutOneOfs { if (opts.lengthDelimited !== false) { w.ldelim() } - }, (r, length, opts = {}) => { + }, (r, length) => { const obj: any = { fieldOne: '', fieldTwo: '', @@ -350,7 +350,7 @@ export namespace MessageWithoutOneOfs { } return obj - }, function * (r, length, prefix, opts = {}) { + }, function * (r, length, prefix) { const end = length == null ? r.len : r.pos + length if (prefix !== '.') { diff --git a/packages/protons/test/fixtures/optional.ts b/packages/protons/test/fixtures/optional.ts index d432e64..555354c 100644 --- a/packages/protons/test/fixtures/optional.ts +++ b/packages/protons/test/fixtures/optional.ts @@ -53,7 +53,7 @@ export namespace OptionalSubMessage { if (opts.lengthDelimited !== false) { w.ldelim() } - }, (r, length, opts = {}) => { + }, (r, length) => { const obj: any = {} const end = length == null ? r.len : r.pos + length @@ -78,7 +78,7 @@ export namespace OptionalSubMessage { } return obj - }, function * (r, length, prefix, opts = {}) { + }, function * (r, length, prefix) { const end = length == null ? r.len : r.pos + length if (prefix !== '.') { diff --git a/packages/protons/test/fixtures/peer.ts b/packages/protons/test/fixtures/peer.ts index b1c1a03..92e8060 100644 --- a/packages/protons/test/fixtures/peer.ts +++ b/packages/protons/test/fixtures/peer.ts @@ -340,7 +340,7 @@ export namespace Address { if (opts.lengthDelimited !== false) { w.ldelim() } - }, (r, length, opts = {}) => { + }, (r, length) => { const obj: any = { multiaddr: uint8ArrayAlloc(0) } @@ -367,7 +367,7 @@ export namespace Address { } return obj - }, function * (r, length, prefix, opts = {}) { + }, function * (r, length, prefix) { const end = length == null ? r.len : r.pos + length if (prefix !== '.') { @@ -472,7 +472,7 @@ export namespace Metadata { if (opts.lengthDelimited !== false) { w.ldelim() } - }, (r, length, opts = {}) => { + }, (r, length) => { const obj: any = { key: '', value: uint8ArrayAlloc(0) @@ -500,7 +500,7 @@ export namespace Metadata { } return obj - }, function * (r, length, prefix, opts = {}) { + }, function * (r, length, prefix) { const end = length == null ? r.len : r.pos + length if (prefix !== '.') { diff --git a/packages/protons/test/fixtures/proto2.ts b/packages/protons/test/fixtures/proto2.ts index 0dd746d..ead2b9d 100644 --- a/packages/protons/test/fixtures/proto2.ts +++ b/packages/protons/test/fixtures/proto2.ts @@ -26,7 +26,7 @@ export namespace MessageWithRequired { if (opts.lengthDelimited !== false) { w.ldelim() } - }, (r, length, opts = {}) => { + }, (r, length) => { const obj: any = { scalarField: 0 } @@ -49,7 +49,7 @@ export namespace MessageWithRequired { } return obj - }, function * (r, length, prefix, opts = {}) { + }, function * (r, length, prefix) { const end = length == null ? r.len : r.pos + length if (prefix !== '.') { diff --git a/packages/protons/test/fixtures/protons-options.ts b/packages/protons/test/fixtures/protons-options.ts index 0829f45..2143c82 100644 --- a/packages/protons/test/fixtures/protons-options.ts +++ b/packages/protons/test/fixtures/protons-options.ts @@ -180,7 +180,7 @@ export namespace MessageWithSizeLimitedMap { if (opts.lengthDelimited !== false) { w.ldelim() } - }, (r, length, opts = {}) => { + }, (r, length) => { const obj: any = { key: '', value: '' @@ -208,7 +208,7 @@ export namespace MessageWithSizeLimitedMap { } return obj - }, function * (r, length, prefix, opts = {}) { + }, function * (r, length, prefix) { const end = length == null ? r.len : r.pos + length if (prefix !== '.') { diff --git a/packages/protons/test/fixtures/singular.ts b/packages/protons/test/fixtures/singular.ts index 570092b..9d1b391 100644 --- a/packages/protons/test/fixtures/singular.ts +++ b/packages/protons/test/fixtures/singular.ts @@ -54,7 +54,7 @@ export namespace SingularSubMessage { if (opts.lengthDelimited !== false) { w.ldelim() } - }, (r, length, opts = {}) => { + }, (r, length) => { const obj: any = { foo: '', bar: 0 @@ -82,7 +82,7 @@ export namespace SingularSubMessage { } return obj - }, function * (r, length, prefix, opts = {}) { + }, function * (r, length, prefix) { const end = length == null ? r.len : r.pos + length if (prefix !== '.') { diff --git a/packages/protons/test/fixtures/streaming.ts b/packages/protons/test/fixtures/streaming.ts index 5d3be29..f8fdf9c 100644 --- a/packages/protons/test/fixtures/streaming.ts +++ b/packages/protons/test/fixtures/streaming.ts @@ -202,7 +202,7 @@ export namespace NestedMessage { if (opts.lengthDelimited !== false) { w.ldelim() } - }, (r, length, opts = {}) => { + }, (r, length) => { const obj: any = { nestedValue: '' } @@ -225,7 +225,7 @@ export namespace NestedMessage { } return obj - }, function * (r, length, prefix, opts = {}) { + }, function * (r, length, prefix) { const end = length == null ? r.len : r.pos + length if (prefix !== '.') { @@ -1118,7 +1118,7 @@ export namespace MessageWithPrimitiveMap { if (opts.lengthDelimited !== false) { w.ldelim() } - }, (r, length, opts = {}) => { + }, (r, length) => { const obj: any = { key: '', value: '' @@ -1146,7 +1146,7 @@ export namespace MessageWithPrimitiveMap { } return obj - }, function * (r, length, prefix, opts = {}) { + }, function * (r, length, prefix) { const end = length == null ? r.len : r.pos + length if (prefix !== '.') { diff --git a/packages/protons/test/fixtures/test.ts b/packages/protons/test/fixtures/test.ts index 61e4443..ab44201 100644 --- a/packages/protons/test/fixtures/test.ts +++ b/packages/protons/test/fixtures/test.ts @@ -44,7 +44,7 @@ export namespace SubMessage { if (opts.lengthDelimited !== false) { w.ldelim() } - }, (r, length, opts = {}) => { + }, (r, length) => { const obj: any = { foo: '' } @@ -67,7 +67,7 @@ export namespace SubMessage { } return obj - }, function * (r, length, prefix, opts = {}) { + }, function * (r, length, prefix) { const end = length == null ? r.len : r.pos + length if (prefix !== '.') { From e3bb5effcd8003205f7fea3fe51eaf86ebdadd7b Mon Sep 17 00:00:00 2001 From: achingbrain Date: Fri, 18 Sep 2026 10:38:09 +0300 Subject: [PATCH 2/2] chore: fix linting --- packages/protons/src/types/message.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/protons/src/types/message.ts b/packages/protons/src/types/message.ts index c4f0e60..c9e4e4c 100644 --- a/packages/protons/src/types/message.ts +++ b/packages/protons/src/types/message.ts @@ -300,7 +300,8 @@ export interface ${this.pbType}Input { // variable if we have repeated fields or a sub message field to pass // options to const decodeOpts = this.fields.some(field => field instanceof ArrayField || field instanceof MapField || field instanceof MessageField) - ? ', opts = {}' : '' + ? ', opts = {}' + : '' interfaceCodecDef = ` let _codec: Codec<${this.pbType}, ${this.pbType}Input>