From 827c88b89c787965dcc4e402f4e1e22be243cdca Mon Sep 17 00:00:00 2001 From: dbale-altoros Date: Mon, 10 Jul 2023 17:59:07 -0300 Subject: [PATCH] add: info on reason-string maxLength chars --- lib/rules/best-practises/reason-string.js | 11 ++++++++--- test/rules/best-practises/reason-string.js | 21 +++++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/lib/rules/best-practises/reason-string.js b/lib/rules/best-practises/reason-string.js index f535b909..33b44a2f 100644 --- a/lib/rules/best-practises/reason-string.js +++ b/lib/rules/best-practises/reason-string.js @@ -82,7 +82,12 @@ class ReasonStringChecker extends BaseChecker { // If has reason message and is too long, throw an error const reason = _.last(functionParameters).value if (reason.length > this.maxCharactersLong) { - this._errorMessageIsTooLong(node, functionName) + const infoMessage = reason.length + .toString() + .concat(' counted / ') + .concat(this.maxCharactersLong.toString()) + .concat(' allowed') + this._errorMessageIsTooLong(node, functionName, infoMessage) } } } @@ -103,8 +108,8 @@ class ReasonStringChecker extends BaseChecker { this.error(node, `Provide an error message for ${key}`) } - _errorMessageIsTooLong(node, key) { - this.error(node, `Error message for ${key} is too long`) + _errorMessageIsTooLong(node, key, infoMessage) { + this.error(node, `Error message for ${key} is too long: ${infoMessage}`) } } diff --git a/test/rules/best-practises/reason-string.js b/test/rules/best-practises/reason-string.js index c7f0cfd7..30546d7b 100644 --- a/test/rules/best-practises/reason-string.js +++ b/test/rules/best-practises/reason-string.js @@ -101,4 +101,25 @@ describe('Linter - reason-string', () => { assertNoWarnings(report) assertNoErrors(report) }) + + it('should raise reason string maxLength error with added data', () => { + const qtyChars = 'Roles: account already has role'.length + const maxLength = 5 + + const code = funcWith(`require(!has(role, account), "Roles: account already has role"); + role.bearer[account] = true;role.bearer[account] = true; + `) + + const report = linter.processStr(code, { + rules: { 'reason-string': ['warn', { maxLength: 5 }] }, + }) + + assert.ok(report.warningCount > 0) + assertWarnsCount(report, 1) + + assert.equal( + report.reports[0].message, + `Error message for require is too long: ${qtyChars} counted / ${maxLength} allowed` + ) + }) })