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

autofix explicit-types rule #504

Merged
merged 1 commit into from
Oct 9, 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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions docs/rules/best-practises/explicit-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions docs/rules/security/avoid-sha3.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions docs/rules/security/avoid-throw.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
36 changes: 29 additions & 7 deletions lib/rules/best-practises/explicit-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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',
Expand All @@ -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) {
Expand Down Expand Up @@ -106,24 +120,32 @@ 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) {
const errorVars = varsToBeChecked.filter((type) => configType.includes(type))

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 = []

Expand Down
5 changes: 5 additions & 0 deletions lib/rules/security/avoid-sha3.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions lib/rules/security/avoid-throw.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
17 changes: 14 additions & 3 deletions solhint.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()

Expand All @@ -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)
}
}
Expand Down