Skip to content

Commit ec78e8b

Browse files
Merge pull request #689 from protofire/feature-use-natspec
Feature use natspec
2 parents 24a4235 + 14e4c88 commit ec78e8b

28 files changed

+1337
-143
lines changed

conf/rulesets/solhint-all.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,31 @@ module.exports = Object.freeze({
2424
maxLength: 32,
2525
},
2626
],
27+
'use-natspec': [
28+
'warn',
29+
{
30+
title: {
31+
enabled: true,
32+
ignore: {},
33+
},
34+
author: {
35+
enabled: true,
36+
ignore: {},
37+
},
38+
notice: {
39+
enabled: true,
40+
ignore: {},
41+
},
42+
param: {
43+
enabled: true,
44+
ignore: {},
45+
},
46+
return: {
47+
enabled: true,
48+
ignore: {},
49+
},
50+
},
51+
],
2752
'constructor-syntax': 'warn',
2853
'gas-calldata-parameters': 'warn',
2954
'gas-custom-errors': 'warn',

docs/rules.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ title: "Rule Index of Solhint"
2121
| [one-contract-per-file](./rules/best-practices/one-contract-per-file.md) | Enforces the use of ONE Contract per file see [here](https://docs.soliditylang.org/en/v0.8.21/style-guide.html#contract-and-library-names) | $~~~~~~~~$✔️ | |
2222
| [payable-fallback](./rules/best-practices/payable-fallback.md) | When fallback is not payable and there is no receive function you will not be able to receive currency. | $~~~~~~~~$✔️ | |
2323
| [reason-string](./rules/best-practices/reason-string.md) | Require or revert statement must have a reason string and check that each reason string is at most N characters long. | $~~~~~~~~$✔️ | |
24+
| [use-natspec](./rules/best-practices/use-natspec.md) | Enforces the presence and correctness of NatSpec tags. | | |
2425
| [constructor-syntax](./rules/best-practices/constructor-syntax.md) | Constructors should use the new constructor keyword. | | |
2526
2627

docs/rules/best-practices/code-complexity.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ This rule accepts an array of options:
2424
```json
2525
{
2626
"rules": {
27-
"code-complexity": ["warn",7]
27+
"code-complexity": [
28+
"warn",
29+
7
30+
]
2831
}
2932
}
3033
```

docs/rules/best-practices/explicit-types.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ This rule accepts an array of options:
2727
```json
2828
{
2929
"rules": {
30-
"explicit-types": ["warn","explicit"]
30+
"explicit-types": [
31+
"warn",
32+
"explicit"
33+
]
3134
}
3235
}
3336
```

docs/rules/best-practices/function-max-lines.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ This rule accepts an array of options:
2424
```json
2525
{
2626
"rules": {
27-
"function-max-lines": ["warn",50]
27+
"function-max-lines": [
28+
"warn",
29+
50
30+
]
2831
}
2932
}
3033
```

docs/rules/best-practices/max-line-length.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ This rule accepts an array of options:
2626
```json
2727
{
2828
"rules": {
29-
"max-line-length": ["error",120]
29+
"max-line-length": [
30+
"error",
31+
120
32+
]
3033
}
3134
}
3235
```

docs/rules/best-practices/max-states-count.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ This rule accepts an array of options:
2727
```json
2828
{
2929
"rules": {
30-
"max-states-count": ["warn",15]
30+
"max-states-count": [
31+
"warn",
32+
15
33+
]
3134
}
3235
}
3336
```

docs/rules/best-practices/reason-string.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@ This rule accepts an array of options:
2727
```json
2828
{
2929
"rules": {
30-
"reason-string": ["warn",{"maxLength":32}]
30+
"reason-string": [
31+
"warn",
32+
{
33+
"maxLength": 32
34+
}
35+
]
3136
}
3237
}
3338
```
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
---
2+
warning: "This is a dynamically generated file. Do not edit manually."
3+
layout: "default"
4+
title: "use-natspec | Solhint"
5+
---
6+
7+
# use-natspec
8+
![Category Badge](https://img.shields.io/badge/-Best%20Practices%20Rules-informational)
9+
![Default Severity Badge warn](https://img.shields.io/badge/Default%20Severity-warn-yellow)
10+
11+
## Description
12+
Enforces the presence and correctness of NatSpec tags.
13+
14+
## Options
15+
This rule accepts an array of options:
16+
17+
| Index | Description | Default Value |
18+
| ----- | ------------------------------------------------------------------------ | -------------------- |
19+
| 0 | Rule severity. Must be one of "error", "warn", "off". | warn |
20+
| 1 | A JSON object with natspec properties. See EXAMPLE CONFIG section below. | Check EXAMPLE CONFIG |
21+
22+
23+
### Example Config
24+
```json
25+
{
26+
"rules": {
27+
"use-natspec": [
28+
"warn",
29+
{
30+
"title": {
31+
"enabled": true,
32+
"ignore": {}
33+
},
34+
"author": {
35+
"enabled": true,
36+
"ignore": {}
37+
},
38+
"notice": {
39+
"enabled": true,
40+
"ignore": {}
41+
},
42+
"param": {
43+
"enabled": true,
44+
"ignore": {}
45+
},
46+
"return": {
47+
"enabled": true,
48+
"ignore": {}
49+
}
50+
}
51+
]
52+
}
53+
}
54+
```
55+
56+
### Notes
57+
- If a function or return value has unnamed parameters (e.g. `function foo(uint256)`), the rule only checks the number of `@param` or `@return` tags, not their names.
58+
- If a function or variable has `@inheritdoc`, the rule skips the validation.
59+
- The rule supports both `///` and `/** */` style NatSpec comments.
60+
- If a custom config is provided, it is merged with the default config. Only the overridden fields need to be specified.
61+
62+
## Examples
63+
### 👍 Examples of **correct** code for this rule
64+
65+
#### Contract with valid NatSpec
66+
67+
```solidity
68+
69+
/// @title Token contract
70+
/// @author Me
71+
/// @notice This contract handles tokens
72+
contract Token {
73+
/// @notice Transfers tokens
74+
/// @param to The recipient address
75+
/// @param amount The amount to transfer
76+
/// @return success Whether the transfer succeeded
77+
function transfer(address to, uint256 amount) public returns (bool success) {
78+
return true;
79+
}
80+
}
81+
82+
```
83+
84+
#### You can disable specific tags globally or by type/name using the `ignore` option in the config. For example:
85+
86+
```solidity
87+
{
88+
"use-natspec": [
89+
"warn",
90+
{
91+
"title": {
92+
"enabled": true,
93+
"ignore": {
94+
"contract": ["MyContract"],
95+
"*": ["LegacyContract"]
96+
}
97+
},
98+
"param": { "enabled": false }
99+
}
100+
]
101+
}
102+
```
103+
104+
#### The default configuration enables all checks with no ignore rules:
105+
106+
```solidity
107+
{
108+
"title": { "enabled": true, "ignore": {} },
109+
"author": { "enabled": true, "ignore": {} },
110+
"notice": { "enabled": true, "ignore": {} },
111+
"param": { "enabled": true, "ignore": {} },
112+
"return": { "enabled": true, "ignore": {} }
113+
}
114+
```
115+
116+
### 👎 Examples of **incorrect** code for this rule
117+
118+
#### Missing @notice and incorrect @param and @return tags
119+
120+
```solidity
121+
122+
/// @title Token contract
123+
contract Token {
124+
/// @param wrongParam Not matching actual parameter
125+
function transfer(address to) public returns (bool) {
126+
return true;
127+
}
128+
}
129+
130+
```
131+
132+
## Version
133+
This rule was introduced in the latest version.
134+
135+
## Resources
136+
- [Rule source](https://github.com/protofire/solhint/blob/master/lib/rules/best-practices/use-natspec.js)
137+
- [Document source](https://github.com/protofire/solhint/blob/master/docs/rules/best-practices/use-natspec.md)
138+
- [Test cases](https://github.com/protofire/solhint/blob/master/test/rules/best-practices/use-natspec.js)

docs/rules/miscellaneous/import-path-check.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@ This rule accepts an array of options:
2727
```json
2828
{
2929
"rules": {
30-
"import-path-check": ["warn",["[~dependenciesPath]"]]
30+
"import-path-check": [
31+
"warn",
32+
[
33+
"[~dependenciesPath]"
34+
]
35+
]
3136
}
3237
}
3338
```

0 commit comments

Comments
 (0)