-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
202 lines (156 loc) · 5.06 KB
/
index.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
function isFallbackFunction(node) {
return isFunctionDefinition(node) && node.isFallback
}
function isReceiveFunction(node) {
return isFunctionDefinition(node) && node.isReceiveEther
}
function isFunctionDefinition(node) {
return node.type === 'FunctionDefinition'
}
class CustomOrderingChecker {
constructor(reporter, config) {
this.ruleId = 'custom-ordering'
this.reporter = reporter
this.functionsOrder = config
&& config.rules[`custom-functions-order/${this.ruleId}`]
&& config.rules[`custom-functions-order/${this.ruleId}`][1]
&& config.rules[`custom-functions-order/${this.ruleId}`][1].functionsOrder
}
SourceUnit(node) {
const children = node.children
this.checkOrder(children, sourceUnitPartOrder)
}
ContractDefinition(node) {
const children = node.subNodes
this.checkOrder(children, contractPartOrder)
}
checkOrder(children, orderFunction) {
if (children.length === 0) {
return
}
let maxChild = children[0]
let [maxComparisonValue, maxLabel] = orderFunction(children[0], this.functionsOrder)
for (let i = 1; i < children.length; i++) {
const [comparisonValue, label] = orderFunction(children[i], this.functionsOrder)
if (comparisonValue < maxComparisonValue) {
this.report(children[i], maxChild, label, maxLabel)
return
}
maxChild = children[i]
maxComparisonValue = comparisonValue
maxLabel = label
}
}
report(node, nodeBefore, label, labelBefore) {
const message = `Function order is incorrect, ${label} can not go after ${labelBefore} (line ${
nodeBefore.loc.start.line
})`
this.reporter.error(node, this.ruleId, message)
}
}
function getMutabilityWeight({ baseWeight, stateMutability }) {
switch (stateMutability) {
case 'constant':
case 'view':
return baseWeight + 2
case 'pure':
return baseWeight + 4
default:
return baseWeight
}
}
function isTypeDeclaration(node) {
return ['StructDefinition', 'EnumDefinition'].includes(node.type)
}
function sourceUnitPartOrder(node) {
if (node.type === 'PragmaDirective') {
return [0, 'pragma directive']
}
if (node.type === 'ImportDirective') {
return [10, 'import directive']
}
if (node.type === 'EnumDefinition' || node.type === 'StructDefinition') {
return [20, 'type definition']
}
if (node.type === 'ContractDefinition') {
if (node.kind === 'interface') {
return [30, 'interface']
}
if (node.kind === 'library') {
return [40, 'library definition']
}
if (node.kind === 'contract') {
return [50, 'contract definition']
}
}
throw new Error('Unrecognized source unit part, please report this issue')
}
function contractPartOrder(node, functionsOrder) {
if (node.type === 'UsingForDeclaration') {
return [0, 'using for declaration']
}
if (isTypeDeclaration(node)) {
let label
if (node.type === 'StructDefinition') {
label = 'struct definition'
} else {
label = 'enum definition'
}
return [10, label]
}
if (node.type === 'StateVariableDeclaration') {
return [20, 'state variable declaration']
}
if (node.type === 'EventDefinition') {
return [30, 'event definition']
}
if (node.type === 'ModifierDefinition') {
return [40, 'modifier definition']
}
if (node.isConstructor) {
return [50, 'constructor']
}
if (isReceiveFunction(node)) {
return [60, 'receive function']
}
if (isFallbackFunction(node)) {
return [70, 'fallback function']
}
if (node.type === 'FunctionDefinition') {
const { stateMutability, visibility } = node
if (!functionsOrder || functionsOrder.length === 0) {
return defaultOrdering(node)
}
const vs = stateMutability ? [visibility, stateMutability].join(' ') : visibility
const index = functionsOrder.findIndex(e => e === vs)
const label = [vs, 'function'].join(' ')
const weight = 70 + ((index + 1) * 10)
return [weight, label]
}
throw new Error('Unrecognized contract part, please report this issue')
}
function defaultOrdering(node) {
const { stateMutability, visibility } = node
if (visibility === 'external') {
const weight = getMutabilityWeight({ baseWeight: 80, stateMutability })
const label = [visibility, stateMutability, 'function'].join(' ')
return [weight, label]
}
if (visibility === 'public') {
const weight = getMutabilityWeight({ baseWeight: 90, stateMutability })
const label = [visibility, stateMutability, 'function'].join(' ')
return [weight, label]
}
if (visibility === 'internal') {
const weight = getMutabilityWeight({ baseWeight: 100, stateMutability })
const label = [visibility, stateMutability, 'function'].join(' ')
return [weight, label]
}
if (visibility === 'private') {
const weight = getMutabilityWeight({ baseWeight: 110, stateMutability })
const label = [visibility, stateMutability, 'function'].join(' ')
return [weight, label]
}
throw new Error('Unknown order for function, please report this issue')
}
module.exports = [CustomOrderingChecker]