From e3ad28ce470176469e01394f9c54701c7c716db8 Mon Sep 17 00:00:00 2001 From: RenanSouza2 Date: Fri, 25 Oct 2024 16:59:08 -0300 Subject: [PATCH 1/2] improve code readability --- packages/hardhat-chai-matchers/src/utils.ts | 53 +++++++++++++-------- 1 file changed, 32 insertions(+), 21 deletions(-) diff --git a/packages/hardhat-chai-matchers/src/utils.ts b/packages/hardhat-chai-matchers/src/utils.ts index b5a1d508f0..86b0a7f474 100644 --- a/packages/hardhat-chai-matchers/src/utils.ts +++ b/packages/hardhat-chai-matchers/src/utils.ts @@ -19,36 +19,47 @@ export type Ssfi = (...args: any[]) => any; * the best way to use this value, so this needs some trial-and-error. Use the * existing matchers for a reference of something that works well enough. */ -export function buildAssert(negated: boolean, ssfi: Ssfi) { + +type Message = string | (() => string); + +function evalMessage(message?: Message): string { + if (message === undefined) { + throw new Error( + "Assertion doesn't have an error message. Please open an issue to report this." + ); + } + + return typeof message === "function" ? message() : message; +} + +function buildNegated(ssfi: Ssfi) { return function ( condition: boolean, - messageFalse?: string | (() => string), - messageTrue?: string | (() => string) + _messageFalse?: Message, + messageTrue?: Message ) { - if (!negated && !condition) { - if (messageFalse === undefined) { - throw new Error( - "Assertion doesn't have an error message. Please open an issue to report this." - ); - } - - const message = - typeof messageFalse === "function" ? messageFalse() : messageFalse; + if (condition) { + const message = evalMessage(messageTrue); throw new AssertionError(message, undefined, ssfi); } + }; +} - if (negated && condition) { - if (messageTrue === undefined) { - throw new Error( - "Assertion doesn't have an error message. Please open an issue to report this." - ); - } - - const message = - typeof messageTrue === "function" ? messageTrue() : messageTrue; +function buildNormal(ssfi: Ssfi) { + return function ( + condition: boolean, + messageFalse?: Message, + _messageTrue?: Message + ) { + if (!condition) { + const message = evalMessage(messageFalse); throw new AssertionError(message, undefined, ssfi); } }; } +export function buildAssert(negated: boolean, ssfi: Ssfi) { + return negated ? buildNegated(ssfi) : buildNormal(ssfi); +} + export type AssertWithSsfi = ReturnType; From 17e8a2eace06acfa0ddd5c4d81911791fdd85565 Mon Sep 17 00:00:00 2001 From: Luis Schaab Date: Mon, 30 Dec 2024 17:03:55 +0000 Subject: [PATCH 2/2] doc: move jsdoc above the buildAssert fn --- packages/hardhat-chai-matchers/src/utils.ts | 33 ++++++++++----------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/packages/hardhat-chai-matchers/src/utils.ts b/packages/hardhat-chai-matchers/src/utils.ts index 86b0a7f474..a349b5eebd 100644 --- a/packages/hardhat-chai-matchers/src/utils.ts +++ b/packages/hardhat-chai-matchers/src/utils.ts @@ -3,23 +3,6 @@ import { AssertionError } from "chai"; // just a generic function type to avoid errors from the ban-types eslint rule export type Ssfi = (...args: any[]) => any; -/** - * This function is used by the matchers to obtain an `assert` function, which - * should be used instead of `this.assert`. - * - * The first parameter is the value of the `negated` flag. Keep in mind that - * this value should be captured at the beginning of the matcher's - * implementation, before any async code is executed. Otherwise things like - * `.to.emit().and.not.to.emit()` won't work, because by the time the async part - * of the first emit is executd, the `.not` (executed synchronously) has already - * modified the flag. - * - * The second parameter is what Chai calls the "start stack function indicator", - * a function that is used to build the stack trace. It's unclear to us what's - * the best way to use this value, so this needs some trial-and-error. Use the - * existing matchers for a reference of something that works well enough. - */ - type Message = string | (() => string); function evalMessage(message?: Message): string { @@ -58,6 +41,22 @@ function buildNormal(ssfi: Ssfi) { }; } +/** + * This function is used by the matchers to obtain an `assert` function, which + * should be used instead of `this.assert`. + * + * The first parameter is the value of the `negated` flag. Keep in mind that + * this value should be captured at the beginning of the matcher's + * implementation, before any async code is executed. Otherwise things like + * `.to.emit().and.not.to.emit()` won't work, because by the time the async part + * of the first emit is executd, the `.not` (executed synchronously) has already + * modified the flag. + * + * The second parameter is what Chai calls the "start stack function indicator", + * a function that is used to build the stack trace. It's unclear to us what's + * the best way to use this value, so this needs some trial-and-error. Use the + * existing matchers for a reference of something that works well enough. + */ export function buildAssert(negated: boolean, ssfi: Ssfi) { return negated ? buildNegated(ssfi) : buildNormal(ssfi); }