Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I051 list current rules being used by .solhint.json #449

Merged
merged 2 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ Options:
Commands:

stdin [options] linting of source code data provided to STDIN
list-rules display covered rules of current .solhint.json
```
### Note
The `--fix` option currently works only on "avoid-throw" and "avoid-sha3" rules
Expand Down
11 changes: 8 additions & 3 deletions lib/rules/best-practises/reason-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand All @@ -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}`)
}
}

Expand Down
35 changes: 35 additions & 0 deletions solhint.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ function init() {
.description('create configuration file for solhint')
.action(writeSampleConfigFile)

program
.command('list-rules', null, { noHelp: false })
.description('display covered rules of current .solhint.json')
.action(listRules)

if (process.argv.length <= 2) {
program.help()
}
Expand All @@ -63,6 +68,7 @@ function execMainAction() {

const reportLists = program.args.filter(_.isString).map(processPath)
const reports = _.flatten(reportLists)

const warningsCount = reports.reduce((acc, i) => acc + i.warningCount, 0)
const warningsNumberExceeded =
program.opts().maxWarnings >= 0 && warningsCount > program.opts().maxWarnings
Expand Down Expand Up @@ -207,6 +213,35 @@ function getFormatter(formatter) {
}
}

function listRules() {
const configPath = '.solhint.json'
if (!fs.existsSync(configPath)) {
console.log('Error!! Configuration does not exists')
process.exit(0)
} else {
const config = readConfig()
console.log('\nConfiguration File: \n', config)

const reportLists = linter.processPath(configPath, readConfig())
const rulesObject = reportLists[0].config

console.log('\nRules: \n')
const orderedRules = Object.keys(rulesObject)
.sort()
.reduce((obj, key) => {
obj[key] = rulesObject[key]
return obj
}, {})

// eslint-disable-next-line func-names
Object.keys(orderedRules).forEach(function (key) {
console.log('- ', key, ': ', orderedRules[key])
})
}

process.exit(0)
}

function exitWithCode(reports) {
const errorsCount = reports.reduce((acc, i) => acc + i.errorCount, 0)

Expand Down
21 changes: 21 additions & 0 deletions test/rules/best-practises/reason-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`
)
})
})