Skip to content

Commit 02fe5cb

Browse files
Merge pull request #421 from protofire/fix/named-parameters-mapping-no-enforce-nested
Rule Modification: no enforcing parameters on nested mappings
2 parents b6240ac + cde1bc6 commit 02fe5cb

File tree

4 files changed

+84
-69
lines changed

4 files changed

+84
-69
lines changed

docs/rules/naming/named-parameters-mapping.md

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,28 +39,52 @@ mapping(string name => uint256 balance) public users;
3939
mapping(address owner => mapping(address token => uint256 balance)) public tokenBalances;
4040
```
4141

42+
#### Main key of mapping is enforced. On nested mappings other naming are not neccesary
43+
44+
```solidity
45+
mapping(address owner => mapping(address => uint256)) public tokenBalances;
46+
```
47+
48+
#### Main key of the parent mapping is enforced. No naming in nested mapping uint256
49+
50+
```solidity
51+
mapping(address owner => mapping(address token => uint256)) public tokenBalances;
52+
```
53+
54+
#### Main key of the parent mapping is enforced. No naming in nested mapping address
55+
56+
```solidity
57+
mapping(address owner => mapping(address => uint256 balance)) public tokenBalances;
58+
```
59+
4260
### 👎 Examples of **incorrect** code for this rule
4361

44-
#### No naming in regular mapping
62+
#### No naming at all in regular mapping
4563

4664
```solidity
4765
mapping(address => uint256)) public tokenBalances;
4866
```
4967

50-
#### No naming in nested mapping
68+
#### Missing any variable name in regular mapping uint256
69+
70+
```solidity
71+
mapping(address token => uint256)) public tokenBalances;
72+
```
73+
74+
#### Missing any variable name in regular mapping address
5175

5276
```solidity
53-
mapping(address => mapping(address => uint256)) public tokenBalances;
77+
mapping(address => uint256 balance)) public tokenBalances;
5478
```
5579

56-
#### No complete naming in nested mapping. Missing main key and value
80+
#### No MAIN KEY naming in nested mapping. Other naming are not enforced
5781

5882
```solidity
59-
mapping(address => mapping(address token => uint256)) public tokenBalances;
83+
mapping(address => mapping(address token => uint256 balance)) public tokenBalances;
6084
```
6185

6286
## Version
63-
This rule is introduced in the latest version.
87+
This rule was introduced in [Solhint 3.4.0](https://github.com/protofire/solhint/tree/v3.4.0)
6488

6589
## Resources
6690
- [Rule source](https://github.com/protofire/solhint/tree/master/lib/rules/naming/named-parameters-mapping.js)

lib/rules/naming/named-parameters-mapping.js

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,38 @@ const meta = {
1818
'To enter owner token balance, the main key "owner" enters another mapping which its key is "token" to get its "balance"',
1919
code: 'mapping(address owner => mapping(address token => uint256 balance)) public tokenBalances;',
2020
},
21+
{
22+
description:
23+
'Main key of mapping is enforced. On nested mappings other naming are not neccesary',
24+
code: 'mapping(address owner => mapping(address => uint256)) public tokenBalances;',
25+
},
26+
{
27+
description:
28+
'Main key of the parent mapping is enforced. No naming in nested mapping uint256',
29+
code: 'mapping(address owner => mapping(address token => uint256)) public tokenBalances;',
30+
},
31+
{
32+
description:
33+
'Main key of the parent mapping is enforced. No naming in nested mapping address',
34+
code: 'mapping(address owner => mapping(address => uint256 balance)) public tokenBalances;',
35+
},
2136
],
2237
bad: [
2338
{
24-
description: 'No naming in regular mapping ',
39+
description: 'No naming at all in regular mapping ',
2540
code: 'mapping(address => uint256)) public tokenBalances;',
2641
},
2742
{
28-
description: 'No naming in nested mapping ',
29-
code: 'mapping(address => mapping(address => uint256)) public tokenBalances;',
43+
description: 'Missing any variable name in regular mapping uint256',
44+
code: 'mapping(address token => uint256)) public tokenBalances;',
45+
},
46+
{
47+
description: 'Missing any variable name in regular mapping address',
48+
code: 'mapping(address => uint256 balance)) public tokenBalances;',
3049
},
3150
{
32-
description: 'No complete naming in nested mapping. Missing main key and value ',
33-
code: 'mapping(address => mapping(address token => uint256)) public tokenBalances;',
51+
description: 'No MAIN KEY naming in nested mapping. Other naming are not enforced',
52+
code: 'mapping(address => mapping(address token => uint256 balance)) public tokenBalances;',
3453
},
3554
],
3655
},
@@ -63,32 +82,23 @@ class NamedParametersMapping extends BaseChecker {
6382

6483
checkNameOnMapping(variable, isNested) {
6584
let mainKeyName
66-
let nestedKeyName
6785
let valueName
6886

6987
if (isNested) {
7088
mainKeyName = variable.typeName.keyName ? variable.typeName.keyName.name : null
71-
nestedKeyName = variable.typeName.valueType.keyName
72-
? variable.typeName.valueType.keyName.name
73-
: null
7489
valueName = variable.typeName.valueType.valueName
7590
? variable.typeName.valueType.valueName.name
7691
: null
7792
} else {
7893
mainKeyName = variable.typeName.keyName ? variable.typeName.keyName.name : null
79-
nestedKeyName = null
8094
valueName = variable.typeName.valueName ? variable.typeName.valueName.name : null
8195
}
8296

8397
if (!mainKeyName) {
8498
this.report(variable, 'Main key')
8599
}
86100

87-
if (!nestedKeyName && isNested) {
88-
this.report(variable, 'Nested key')
89-
}
90-
91-
if (!valueName) {
101+
if (!valueName && !isNested) {
92102
this.report(variable, 'Value')
93103
}
94104
}

test/fixtures/naming/named-parameters-mapping.js

Lines changed: 29 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,18 @@ const NO_NAMED_MAPPING_REGULAR = [
77
{
88
code: 'mapping(string => address owner) public ownerAddresses;',
99
error_mainKey: true,
10-
error_nestedKey: false,
1110
error_value: false,
1211
mapping_name: 'ownerAddresses',
1312
},
1413
{
1514
code: 'mapping(string ownerName => address) public ownerAddresses;',
1615
error_mainKey: false,
17-
error_nestedKey: false,
1816
error_value: true,
1917
mapping_name: 'ownerAddresses',
2018
},
2119
{
2220
code: 'mapping(string => address) public ownerAddresses;',
2321
error_mainKey: true,
24-
error_nestedKey: false,
2522
error_value: true,
2623
mapping_name: 'ownerAddresses',
2724
},
@@ -31,49 +28,24 @@ const NO_NAMED_MAPPING_NESTED = [
3128
{
3229
code: 'mapping(address => mapping(address => uint256)) public tokenBalances;',
3330
error_mainKey: true,
34-
error_nestedKey: true,
35-
error_value: true,
36-
mapping_name: 'tokenBalances',
37-
},
38-
{
39-
code: 'mapping(address owner => mapping(address => uint256)) public tokenBalances;',
40-
error_mainKey: false,
41-
error_nestedKey: true,
42-
error_value: true,
43-
mapping_name: 'tokenBalances',
44-
},
45-
{
46-
code: 'mapping(address owner => mapping(address token => uint256)) public tokenBalances;',
47-
error_mainKey: false,
48-
error_nestedKey: false,
49-
error_value: true,
31+
error_value: false,
5032
mapping_name: 'tokenBalances',
5133
},
5234
{
5335
code: 'mapping(address => mapping(address token => uint256)) public tokenBalances;',
5436
error_mainKey: true,
55-
error_nestedKey: false,
56-
error_value: true,
57-
mapping_name: 'tokenBalances',
58-
},
59-
{
60-
code: 'mapping(address => mapping(address token => uint256 balance)) public tokenBalances;',
61-
error_mainKey: true,
62-
error_nestedKey: false,
6337
error_value: false,
6438
mapping_name: 'tokenBalances',
6539
},
6640
{
6741
code: 'mapping(address => mapping(address => uint256 balance)) public tokenBalances;',
6842
error_mainKey: true,
69-
error_nestedKey: true,
7043
error_value: false,
7144
mapping_name: 'tokenBalances',
7245
},
7346
{
74-
code: 'mapping(address owner => mapping(address => uint256 balance)) public tokenBalances;',
75-
error_mainKey: false,
76-
error_nestedKey: true,
47+
code: 'mapping(address => mapping(address token => uint256 balance)) public tokenBalances;',
48+
error_mainKey: true,
7749
error_value: false,
7850
mapping_name: 'tokenBalances',
7951
},
@@ -88,7 +60,6 @@ const OTHER_WRONG_DECLARATIONS = [
8860
}
8961
mapping(address => ThisIsStruct) public ownerStuff;`,
9062
error_mainKey: true,
91-
error_nestedKey: false,
9263
error_value: true,
9364
mapping_name: 'ownerStuff',
9465
},
@@ -100,7 +71,6 @@ const OTHER_WRONG_DECLARATIONS = [
10071
}
10172
mapping(address owner => ThisIsStruct) public ownerStuff;`,
10273
error_mainKey: false,
103-
error_nestedKey: false,
10474
error_value: true,
10575
mapping_name: 'ownerStuff',
10676
},
@@ -110,10 +80,9 @@ const OTHER_WRONG_DECLARATIONS = [
11080
uint256 A;
11181
uint256 B;
11282
}
113-
mapping(address owner => mapping(address token => ThisIsStruct)) public ownerStuffPerToken;`,
114-
error_mainKey: false,
115-
error_nestedKey: false,
116-
error_value: true,
83+
mapping(address => mapping(address token => ThisIsStruct)) public ownerStuffPerToken;`,
84+
error_mainKey: true,
85+
error_value: false,
11786
mapping_name: 'ownerStuffPerToken',
11887
},
11988
{
@@ -125,8 +94,7 @@ const OTHER_WRONG_DECLARATIONS = [
12594
}
12695
mapping(address => mapping(address => ThisIsStruct)) public ownerStuffPerToken;`,
12796
error_mainKey: true,
128-
error_nestedKey: true,
129-
error_value: true,
97+
error_value: false,
13098
mapping_name: 'ownerStuffPerToken',
13199
},
132100
]
@@ -141,20 +109,17 @@ const OTHER_OK_DECLARATIONS = [
141109
mapping(address owner => ThisIsStruct structContent) public ownerStuff;
142110
uint256 public A;`,
143111
error_mainKey: false,
144-
error_nestedKey: false,
145112
error_value: false,
146113
mapping_name: 'ownerStuff',
147114
},
148115
{
149116
code: 'uint256 public A;',
150117
error_mainKey: false,
151-
error_nestedKey: false,
152118
error_value: false,
153119
},
154120
{
155121
code: 'uint256 constant public A = 100000;',
156122
error_mainKey: false,
157-
error_nestedKey: false,
158123
error_value: false,
159124
},
160125
{
@@ -164,7 +129,6 @@ const OTHER_OK_DECLARATIONS = [
164129
uint256 B;
165130
}`,
166131
error_mainKey: false,
167-
error_nestedKey: false,
168132
error_value: false,
169133
},
170134
{
@@ -175,7 +139,28 @@ const OTHER_OK_DECLARATIONS = [
175139
}
176140
mapping(address owner => mapping(address token => ThisIsStruct structContent)) public ownerStuffPerToken;`,
177141
error_mainKey: false,
178-
error_nestedKey: false,
142+
error_value: false,
143+
mapping_name: 'ownerStuffPerToken',
144+
},
145+
{
146+
code: `
147+
struct ThisIsStruct {
148+
uint256 A;
149+
uint256 B;
150+
}
151+
mapping(address owner => mapping(address => ThisIsStruct structContent)) public ownerStuffPerToken;`,
152+
error_mainKey: false,
153+
error_value: false,
154+
mapping_name: 'ownerStuffPerToken',
155+
},
156+
{
157+
code: `
158+
struct ThisIsStruct {
159+
uint256 A;
160+
uint256 B;
161+
}
162+
mapping(address owner => mapping(address => ThisIsStruct)) public ownerStuffPerToken;`,
163+
error_mainKey: false,
179164
error_value: false,
180165
mapping_name: 'ownerStuffPerToken',
181166
},

test/rules/naming/named-parameters-mapping.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,13 @@ const WRONG_DECLARATIONS = NO_NAMED_MAPPING_REGULAR.concat(
1616
OTHER_WRONG_DECLARATIONS
1717
)
1818
const MAIN_KEY_ERROR = 'Main key parameter in mapping XXXXX is not named'
19-
const NESTED_KEY_ERROR = 'Nested key parameter in mapping XXXXX is not named'
2019
const VALUE_ERROR = 'Value parameter in mapping XXXXX is not named'
2120

2221
const getPositionErrors = (objectCode) => {
2322
const errorArray = []
2423
if (objectCode.error_mainKey)
2524
errorArray.push(MAIN_KEY_ERROR.replace('XXXXX', objectCode.mapping_name))
2625

27-
if (objectCode.error_nestedKey)
28-
errorArray.push(NESTED_KEY_ERROR.replace('XXXXX', objectCode.mapping_name))
29-
3026
if (objectCode.error_value) errorArray.push(VALUE_ERROR.replace('XXXXX', objectCode.mapping_name))
3127
return errorArray
3228
}

0 commit comments

Comments
 (0)