-
Notifications
You must be signed in to change notification settings - Fork 179
/
Copy pathnamed-parameters-mapping.js
112 lines (103 loc) · 3.77 KB
/
named-parameters-mapping.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
const BaseChecker = require('../base-checker')
const ruleId = 'named-parameters-mapping'
const meta = {
type: 'naming',
docs: {
description: `Solidity v0.8.18 introduced named parameters on the mappings definition.`,
category: 'Style Guide Rules',
examples: {
good: [
{
description:
'To enter "users" mapping the key called "name" is needed to get the "balance"',
code: 'mapping(string name => uint256 balance) public users;',
},
{
description:
'To enter owner token balance, the main key "owner" enters another mapping which its key is "token" to get its "balance"',
code: 'mapping(address owner => mapping(address token => uint256 balance)) public tokenBalances;',
},
{
description:
'Main key of mapping is enforced. On nested mappings other names are not necessary',
code: 'mapping(address owner => mapping(address => uint256)) public tokenBalances;',
},
{
description:
'Main key of the parent mapping is enforced. No naming in nested mapping uint256',
code: 'mapping(address owner => mapping(address token => uint256)) public tokenBalances;',
},
{
description:
'Main key of the parent mapping is enforced. No naming in nested mapping address',
code: 'mapping(address owner => mapping(address => uint256 balance)) public tokenBalances;',
},
],
bad: [
{
description: 'No naming at all in regular mapping ',
code: 'mapping(address => uint256)) public tokenBalances;',
},
{
description: 'Missing any variable name in regular mapping uint256',
code: 'mapping(address token => uint256)) public tokenBalances;',
},
{
description: 'Missing any variable name in regular mapping address',
code: 'mapping(address => uint256 balance)) public tokenBalances;',
},
{
description: 'No MAIN KEY naming in nested mapping. Other names are not enforced',
code: 'mapping(address => mapping(address token => uint256 balance)) public tokenBalances;',
},
],
},
},
isDefault: false,
recommended: false,
defaultSetup: 'warn',
schema: null,
}
class NamedParametersMappingChecker extends BaseChecker {
constructor(reporter) {
super(reporter, ruleId, meta)
}
StateVariableDeclaration(node) {
let isNested = false
const variables = node.variables
variables.forEach((variable) => {
// maybe the comparission to VariableDeclaration can be deleted
if (variable.type === 'VariableDeclaration' && variable.typeName.type === 'Mapping') {
if (variable.typeName.valueType.type === 'Mapping') {
// isNested mapping
isNested = true
}
this.checkNameOnMapping(variable, isNested)
}
})
}
checkNameOnMapping(variable, isNested) {
let mainKeyName
let valueName
if (isNested) {
mainKeyName = variable.typeName.keyName ? variable.typeName.keyName.name : null
valueName = variable.typeName.valueType.valueName
? variable.typeName.valueType.valueName.name
: null
} else {
mainKeyName = variable.typeName.keyName ? variable.typeName.keyName.name : null
valueName = variable.typeName.valueName ? variable.typeName.valueName.name : null
}
if (!mainKeyName) {
this.report(variable, 'Main key')
}
if (!valueName && !isNested) {
this.report(variable, 'Value')
}
}
report(variable, type) {
const message = `${type} parameter in mapping ${variable.name} is not named`
this.reporter.error(variable, this.ruleId, message)
}
}
module.exports = NamedParametersMappingChecker