diff --git a/README.md b/README.md index 0f010f69..c1d347a8 100644 --- a/README.md +++ b/README.md @@ -60,7 +60,8 @@ Options: -c, --config [file_name] file to use as your .solhint.json -q, --quiet report errors only - default: false --ignore-path [file_name] file to use as your .solhintignore - --fix automatically fix problems + --fix automatically fix problems. Skip report + --fixShow automatically fix problems. Show report --init create configuration file for solhint -h, --help output usage information diff --git a/docs/rules/best-practises/explicit-types.md b/docs/rules/best-practises/explicit-types.md index cd698b24..18964a4e 100644 --- a/docs/rules/best-practises/explicit-types.md +++ b/docs/rules/best-practises/explicit-types.md @@ -32,6 +32,8 @@ This rule accepts an array of options: } ``` +### Notes +- Solhint allows this rule to automatically fix the code with `--fix` or `--fixShow` option ## Examples ### 👍 Examples of **correct** code for this rule diff --git a/docs/rules/security/avoid-sha3.md b/docs/rules/security/avoid-sha3.md index 1d6f9637..6b009388 100644 --- a/docs/rules/security/avoid-sha3.md +++ b/docs/rules/security/avoid-sha3.md @@ -26,6 +26,8 @@ This rule accepts a string option of rule severity. Must be one of "error", "war } ``` +### Notes +- Solhint allows this rule to automatically fix the code with `--fix` or `--fixShow` option ## Examples This rule does not have examples. diff --git a/docs/rules/security/avoid-throw.md b/docs/rules/security/avoid-throw.md index a9d90cc8..b5a3022d 100644 --- a/docs/rules/security/avoid-throw.md +++ b/docs/rules/security/avoid-throw.md @@ -26,6 +26,8 @@ This rule accepts a string option of rule severity. Must be one of "error", "war } ``` +### Notes +- Solhint allows this rule to automatically fix the code with `--fix` or `--fixShow` option ## Examples This rule does not have examples. diff --git a/lib/rules/best-practises/explicit-types.js b/lib/rules/best-practises/explicit-types.js index 010db857..266a38dd 100644 --- a/lib/rules/best-practises/explicit-types.js +++ b/lib/rules/best-practises/explicit-types.js @@ -7,6 +7,7 @@ const ALL_TYPES = EXPLICIT_TYPES.concat(IMPLICIT_TYPES) const VALID_CONFIGURATION_OPTIONS = ['explicit', 'implicit'] const DEFAULT_OPTION = 'explicit' const DEFAULT_SEVERITY = 'warn' +let typesToSearch const ruleId = 'explicit-types' const meta = { @@ -55,11 +56,17 @@ const meta = { }, ], }, + notes: [ + { + note: 'Solhint allows this rule to automatically fix the code with `--fix` or `--fixShow` option', + }, + ], }, isDefault: false, recommended: true, defaultSetup: [DEFAULT_SEVERITY, DEFAULT_OPTION], + fixable: true, schema: { type: 'string', @@ -73,6 +80,13 @@ class ExplicitTypesChecker extends BaseChecker { this.configOption = (config && config.getStringFromArray(ruleId, DEFAULT_OPTION)) || DEFAULT_OPTION this.isExplicit = this.configOption === 'explicit' + + // if explicit, it will search for implicit and viceversa + if (this.isExplicit) { + typesToSearch = IMPLICIT_TYPES + } else { + typesToSearch = EXPLICIT_TYPES + } } VariableDeclaration(node) { @@ -106,12 +120,7 @@ class ExplicitTypesChecker extends BaseChecker { // if defined variables are not to be checked (example address type), return if (varsToBeChecked && varsToBeChecked.length === 0) return - // if explicit, it will search for implicit and viceversa - if (this.isExplicit) { - this.validateVariables(IMPLICIT_TYPES, node, varsToBeChecked) - } else { - this.validateVariables(EXPLICIT_TYPES, node, varsToBeChecked) - } + this.validateVariables(typesToSearch, node, varsToBeChecked) } validateVariables(configType, node, varsToBeChecked) { @@ -119,11 +128,24 @@ class ExplicitTypesChecker extends BaseChecker { if (errorVars && errorVars.length > 0) { for (const errorVar of errorVars) { - this.error(node, `Rule is set with ${this.configOption} type [var/s: ${errorVar}]`) + this.error( + node, + `Rule is set with ${this.configOption} type [var/s: ${errorVar}]`, + this.fixStatement(node, errorVar) + ) } } } + fixStatement(typeNameNode, errorVar) { + const configFileIndex = typesToSearch.findIndex((arg) => arg === errorVar) + return (fixer) => + fixer.replaceTextRange( + typeNameNode.typeName.range, + this.isExplicit ? EXPLICIT_TYPES[configFileIndex] : IMPLICIT_TYPES[configFileIndex] + ) + } + findNamesOfElementaryTypeName(jsonObject, typeToFind) { const names = [] diff --git a/lib/rules/security/avoid-sha3.js b/lib/rules/security/avoid-sha3.js index 048679a8..aa0127c5 100644 --- a/lib/rules/security/avoid-sha3.js +++ b/lib/rules/security/avoid-sha3.js @@ -7,6 +7,11 @@ const meta = { docs: { description: `Use "keccak256" instead of deprecated "sha3".`, category: 'Security Rules', + notes: [ + { + note: 'Solhint allows this rule to automatically fix the code with `--fix` or `--fixShow` option', + }, + ], }, isDefault: false, diff --git a/lib/rules/security/avoid-throw.js b/lib/rules/security/avoid-throw.js index 30a942a0..279233ff 100644 --- a/lib/rules/security/avoid-throw.js +++ b/lib/rules/security/avoid-throw.js @@ -7,6 +7,11 @@ const meta = { docs: { description: `"throw" is deprecated, avoid to use it.`, category: 'Security Rules', + notes: [ + { + note: 'Solhint allows this rule to automatically fix the code with `--fix` or `--fixShow` option', + }, + ], }, isDefault: false, diff --git a/solhint.js b/solhint.js index 6fb12aac..ad68394a 100644 --- a/solhint.js +++ b/solhint.js @@ -27,7 +27,8 @@ function init() { .option('-c, --config [file_name]', 'file to use as your .solhint.json') .option('-q, --quiet', 'report errors only - default: false') .option('--ignore-path [file_name]', 'file to use as your .solhintignore') - .option('--fix', 'automatically fix problems') + .option('--fix', 'automatically fix problems. Skips fixes in report') + .option('--fixShow', 'automatically fix problems. Show fixes in report') .option('--init', 'create configuration file for solhint') .description('Linter for Solidity programming language') .action(execMainAction) @@ -73,7 +74,7 @@ function execMainAction() { const reportLists = program.args.filter(_.isString).map(processPath) const reports = _.flatten(reportLists) - if (program.opts().fix) { + if (program.opts().fix || program.opts().fixShow) { for (const report of reports) { const inputSrc = fs.readFileSync(report.filePath).toString() @@ -85,7 +86,17 @@ function execMainAction() { const { fixed, output } = applyFixes(fixes, inputSrc) if (fixed) { - report.reports = report.reports.filter((x) => !x.fix) + // skip or not the report when fixed + if (program.opts().fix) { + report.reports = report.reports.filter((x) => !x.fix) + } else { + // console.log('report.reports :>> ', report.reports) + report.reports.forEach((report) => { + if (report.fix !== null) { + report.message = `[FIXED] - ${report.message}` + } + }) + } fs.writeFileSync(report.filePath, output) } }