From f6e373fa31829b3cd559980cfca780ace26fd28c Mon Sep 17 00:00:00 2001 From: ncomerci Date: Thu, 18 Dec 2025 15:07:15 -0300 Subject: [PATCH 1/4] lib: ensure early returns in favor of else blocks --- packages/lib-ts/src/environment.ts | 16 +++++++--------- packages/lib-ts/src/helpers/strings.ts | 8 +++++--- packages/lib-ts/src/log.ts | 9 +++------ packages/lib-ts/src/types/BigInt.ts | 12 ++++++------ 4 files changed, 21 insertions(+), 24 deletions(-) diff --git a/packages/lib-ts/src/environment.ts b/packages/lib-ts/src/environment.ts index 7d41e56a..a1bfc832 100644 --- a/packages/lib-ts/src/environment.ts +++ b/packages/lib-ts/src/environment.ts @@ -90,7 +90,7 @@ export namespace environment { */ export function rawTokenPriceQuery(token: Token, timestamp: Date | null = null): USD[] { if (token.isUSD()) return [USD.fromI32(1)] - else if (!(token instanceof BlockchainToken)) throw new Error('Price query not supported for token ' + token.toString()) + if (!(token instanceof BlockchainToken)) throw new Error('Price query not supported for token ' + token.toString()) const prices = _tokenPriceQuery(JSON.stringify(TokenPriceQuery.fromToken(token as BlockchainToken, timestamp))) return JSON.parse(prices).map((price) => USD.fromBigInt(BigInt.fromString(price))) } @@ -108,14 +108,12 @@ export namespace environment { const sortedPrices = prices.sort((a: USD, b: USD) => a.compare(b)) const length = sortedPrices.length - if (length % 2 === 1) { - return sortedPrices[length / 2] - } else { - const left = sortedPrices[length / 2 - 1] - const right = sortedPrices[length / 2] - const sum = left.plus(right) - return sum.div(BigInt.fromI32(2)) - } + if (length % 2 === 1) return sortedPrices[length / 2] + + const left = sortedPrices[length / 2 - 1] + const right = sortedPrices[length / 2] + const sum = left.plus(right) + return sum.div(BigInt.fromI32(2)) } /** diff --git a/packages/lib-ts/src/helpers/strings.ts b/packages/lib-ts/src/helpers/strings.ts index 7fe752ba..540816ef 100644 --- a/packages/lib-ts/src/helpers/strings.ts +++ b/packages/lib-ts/src/helpers/strings.ts @@ -94,12 +94,14 @@ export function normalizeScientificNotation(input: string): string { if (newDecimalPos <= 0) { const zeros = '0'.repeat(-newDecimalPos) return sign + '0.' + zeros + fullDigits - } else if (newDecimalPos >= fullDigits.length) { + } + + if (newDecimalPos >= fullDigits.length) { const zeros = '0'.repeat(newDecimalPos - fullDigits.length) return sign + fullDigits + zeros - } else { - return sign + fullDigits.substring(0, newDecimalPos) + '.' + fullDigits.substring(newDecimalPos) } + + return sign + fullDigits.substring(0, newDecimalPos) + '.' + fullDigits.substring(newDecimalPos) } export function isHex(str: string, strict: boolean = false): boolean { diff --git a/packages/lib-ts/src/log.ts b/packages/lib-ts/src/log.ts index 33480ce1..e080b1c3 100644 --- a/packages/lib-ts/src/log.ts +++ b/packages/lib-ts/src/log.ts @@ -72,12 +72,9 @@ function format(fmt: string, args: Array): string { const argsStr = args.map(a => a.toString()) for (let i: i32 = 0, len: i32 = fmt.length; i < len; i++) { if (i < len - 1 && fmt.charCodeAt(i) == 0x7b /* '{' */ && fmt.charCodeAt(i + 1) == 0x7d /* '}' */) { - if (argIndex >= argsStr.length) { - throw new Error('Too few arguments for format string: ' + fmt) - } else { - out += argsStr[argIndex++] - i++ - } + if (argIndex >= argsStr.length) throw new Error('Too few arguments for format string: ' + fmt) + out += argsStr[argIndex++] + i++ } else { out += fmt.charAt(i) } diff --git a/packages/lib-ts/src/types/BigInt.ts b/packages/lib-ts/src/types/BigInt.ts index c73ea829..b731c6c6 100644 --- a/packages/lib-ts/src/types/BigInt.ts +++ b/packages/lib-ts/src/types/BigInt.ts @@ -404,15 +404,15 @@ export class BigInt extends Uint8Array { return aIsNeg ? resultAbs.neg() : resultAbs } const cmp = BigInt.compare(this.abs(), other.abs()) - if (cmp === 0) { - return BigInt.zero() - } else if (cmp > 0) { + if (cmp === 0) return BigInt.zero() + + if (cmp > 0) { const resultAbs = BigInt.subUnsigned(this.abs(), other.abs()) return this.isNegative() ? resultAbs.neg() : resultAbs - } else { - const resultAbs = BigInt.subUnsigned(other.abs(), this.abs()) - return other.isNegative() ? resultAbs.neg() : resultAbs } + + const resultAbs = BigInt.subUnsigned(other.abs(), this.abs()) + return other.isNegative() ? resultAbs.neg() : resultAbs } /** From d34040b6e1b79916fbaf764cff9da3b7740f16f9 Mon Sep 17 00:00:00 2001 From: ncomerci Date: Thu, 18 Dec 2025 15:13:06 -0300 Subject: [PATCH 2/4] test: ensure early returns in favor of else blocks --- packages/test-ts/src/RunnerMock.ts | 30 +++++++++++------------------- packages/test-ts/src/run-task.ts | 6 +++--- 2 files changed, 14 insertions(+), 22 deletions(-) diff --git a/packages/test-ts/src/RunnerMock.ts b/packages/test-ts/src/RunnerMock.ts index e3d91a26..2b08546f 100644 --- a/packages/test-ts/src/RunnerMock.ts +++ b/packages/test-ts/src/RunnerMock.ts @@ -99,12 +99,11 @@ export default class RunnerMock { run(fnName = 'main'): void { try { const fn = this.instance.exports[fnName] - if (typeof fn === 'function') { - fn() - } else { + if (typeof fn !== 'function') { const availableExports = Object.keys(this.instance.exports).join(', ') throw Error(`No "${fnName}" found in exports. Available exports: ${availableExports}`) } + fn() } catch (error) { throw Error(`Task Execution Error - ${error}`) } @@ -199,13 +198,9 @@ export default class RunnerMock { } private createMockFunction(functionName: string, mockValue: MockResponseValue): CallableFunction { - if (mockValue === 'log') { - return this.createLogFn(functionName) - } else if (this.isParameterizedResponse(mockValue)) { - return this.createParameterizedFunction(functionName, mockValue) - } else { - return this.createConstantFunction(String(mockValue)) - } + if (mockValue === 'log') return this.createLogFn(functionName) + if (this.isParameterizedResponse(mockValue)) return this.createParameterizedFunction(functionName, mockValue) + return this.createConstantFunction(String(mockValue)) } private isParameterizedResponse(value: unknown): value is ParameterizedResponse { @@ -220,21 +215,18 @@ export default class RunnerMock { private createParameterizedFunction(functionName: string, config: ParameterizedResponse): CallableFunction { return (ptr: number) => { const param = this.getStringFromMemory(ptr) - let result: number if (config.paramResponse && param in config.paramResponse) { - result = this.writeStringToMemory(config.paramResponse[param]) - } else if ('default' in config && config.default !== undefined) { - result = this.writeStringToMemory(config.default) - } else { - throw new Error(`No response defined for parameter "${param}" in function "${functionName}".`) + if (config.log === true) this.logToFile(functionName, param) + return this.writeStringToMemory(config.paramResponse[param]) } - if (config.log === true) { - this.logToFile(functionName, param) + if ('default' in config && config.default !== undefined) { + if (config.log === true) this.logToFile(functionName, param) + return this.writeStringToMemory(config.default) } - return result + throw new Error(`No response defined for parameter "${param}" in function "${functionName}".`) } } diff --git a/packages/test-ts/src/run-task.ts b/packages/test-ts/src/run-task.ts index d404da46..a3223795 100644 --- a/packages/test-ts/src/run-task.ts +++ b/packages/test-ts/src/run-task.ts @@ -122,9 +122,9 @@ function toIntents(intentsJson: string) { if (intent.op == OpType.Swap) { const { sourceChain, destinationChain } = intent as Swap return { ...intent, sourceChain: Number(sourceChain), destinationChain: Number(destinationChain) } - } else { - const { chainId } = intent as Transfer | Call - return { ...intent, chainId: Number(chainId) } } + + const { chainId } = intent as Transfer | Call + return { ...intent, chainId: Number(chainId) } }) } From 543b1a5538ff4e1346e81774ae4eb3220852954c Mon Sep 17 00:00:00 2001 From: ncomerci Date: Thu, 18 Dec 2025 15:17:37 -0300 Subject: [PATCH 3/4] chore: add changeset --- .changeset/solid-zebras-dream.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .changeset/solid-zebras-dream.md diff --git a/.changeset/solid-zebras-dream.md b/.changeset/solid-zebras-dream.md new file mode 100644 index 00000000..21e11864 --- /dev/null +++ b/.changeset/solid-zebras-dream.md @@ -0,0 +1,7 @@ +--- +"@mimicprotocol/test-ts": patch +"@mimicprotocol/lib-ts": patch +"@mimicprotocol/cli": patch +--- + +Ensure early returns in favor of else blocks From e9c77fc44f682b2ea5dee407068e51fcebde9ca6 Mon Sep 17 00:00:00 2001 From: ncomerci Date: Mon, 22 Dec 2025 16:26:33 -0300 Subject: [PATCH 4/4] chore: revert RunnerMock change --- packages/test-ts/src/RunnerMock.ts | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/packages/test-ts/src/RunnerMock.ts b/packages/test-ts/src/RunnerMock.ts index 2b08546f..3fbb9a5c 100644 --- a/packages/test-ts/src/RunnerMock.ts +++ b/packages/test-ts/src/RunnerMock.ts @@ -215,18 +215,21 @@ export default class RunnerMock { private createParameterizedFunction(functionName: string, config: ParameterizedResponse): CallableFunction { return (ptr: number) => { const param = this.getStringFromMemory(ptr) + let result: number if (config.paramResponse && param in config.paramResponse) { - if (config.log === true) this.logToFile(functionName, param) - return this.writeStringToMemory(config.paramResponse[param]) + result = this.writeStringToMemory(config.paramResponse[param]) + } else if ('default' in config && config.default !== undefined) { + result = this.writeStringToMemory(config.default) + } else { + throw new Error(`No response defined for parameter "${param}" in function "${functionName}".`) } - if ('default' in config && config.default !== undefined) { - if (config.log === true) this.logToFile(functionName, param) - return this.writeStringToMemory(config.default) + if (config.log === true) { + this.logToFile(functionName, param) } - throw new Error(`No response defined for parameter "${param}" in function "${functionName}".`) + return result } }