protofire / solhint

Solhint is an open-source project to provide a linting utility for Solidity code.
https://protofire.github.io/solhint/
MIT License
1.03k stars 160 forks source link

Add `explicit-types` rule #387

Closed fvictorio closed 1 year ago

fvictorio commented 1 year ago

Summary

Add a new explicit-types rule to forbid or enforce explicit types (like uint256) that have an alias (like uint). This rule should implement the auto-fix mechanism.

Motivation

Prettier Solidity used to have an option to do this, but it was removed in v1.0.0 because we felt that that kind of thing belongs to a linter rather than a formatter.

Details

The rule should accept an options object { default: "explicit" | "implicit" }, where "explicit" means that uint256, int256, etc. should be used and "implicit" means that uint, int, etc. should be used. The default should be "explicit".

The types that should be affected are:

I'm intentionally not including bytes1 and byte. The reason is that, since we don't have version information, we can't know if an instance of bytes1 (with the implicit default selected) is an error or not. If there's demand for this, we might consider adding a new option to the rule.

dbale-altoros commented 1 year ago

@fvictorio I understand the rule... One thing I'm missing As I see in the code... The rules are configured like this:

{
  "rules": {
    "max-line-length": ["error",120]
  }
}

So when you say it should accept an object { default: "explicit" | "implicit" } I did not follow that... maybe something like this

{
  "rules": {
    "explicit-types": ["error","true"]
  }
}

so explicit-types is enforced when true and explicit-types is not enforce when false

Or you meant another thing ?

fvictorio commented 1 year ago
{
  "rules": {
    "explicit-types": ["error", { default: "implicit" }]
  }
}
shirotech commented 1 year ago

In the meantime, I have been maintaining a direct fork with that feature backported, if anyone is interested https://www.npmjs.com/package/prettier-plugin-solidity-explicit-types

dbale-altoros commented 1 year ago

Added in this PR https://github.com/protofire/solhint/pull/467