-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
160 lines (133 loc) · 4.21 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
const merge = require('deepmerge')
const minimatch = require('minimatch')
class EnsureModifiers {
constructor(reporter, config, inputSrc, fileName) {
this.ruleId = 'ensure-modifiers'
this.reporter = reporter
this.config = config
this.inputSrc = inputSrc
this.fileName = fileName
this.context = []
let defaultOpts = {
ignoreVisibility: ["internal"],
ignoreStateMutability: ["view", "pure"],
ignoreContractKind: ["abstract", "interface"],
verbose: false,
required: {},
override: {}
}
let userOpts = config.rules['modifiers/ensure-modifiers'][1] || {}
this.opts = merge(defaultOpts, userOpts)
}
ContractDefinition(ctx) {
this.context.pop()
this.context.push(ctx)
}
matchRules(rules, thing) {
let modifiers = new Set()
let nested = {}
let matched = false
if (!rules) {
return {modifiers, nested, matched}
}
Object.keys(rules).forEach((rule) => {
if (minimatch(thing, rule, {matchBase: true})) {
if (Array.isArray(rules[rule])) {
matched = true
rules[rule].forEach((r) => modifiers.add(r))
} else {
nested = {...nested, ...rules[rule]}
}
}
})
return {modifiers, nested, matched}
}
// Traverse `rules` by `paths`. If a rule matches a given path component,
// call `reduceFn` with the matching modifiers and an accumulation of the
// modifiers. Recurses into `rules` to find a list of matching modifiers.
// `reduceFn` should return an accumulation.
collectModifiers(rules, paths, reduceFn) {
let modifiers = new Set()
let matched = false
paths.forEach((path) => {
let r = rules
path = path.slice()
let pathComponent = path.shift()
while (pathComponent && r) {
if (this.opts.verbose) console.log("path", path, "pathComponent", pathComponent, "r", r)
let result = this.matchRules(r, pathComponent)
if (result.matched) {
if (this.opts.verbose) console.log("match", result)
matched = matched || result.matched
modifiers = reduceFn(modifiers, result.modifiers)
}
pathComponent = path.shift()
r = result.nested
}
})
return {modifiers, matched}
}
matchedModifiers(contractName, functionName) {
let fileName = this.fileName
let qualifiedContract = `${fileName}:${contractName}`
let qualifiedContractMethod = `${qualifiedContract}.${functionName}`
let contractMethod = `${contractName}.${functionName}`
let paths = [
[qualifiedContractMethod],
[qualifiedContract, functionName],
[fileName, contractMethod],
[fileName, contractName, functionName]
]
// Look at override first
let matched = false
let result = this.collectModifiers(this.opts.override, paths, (acc, modifiers) => {
// (Effectively) short circuit rather than collecting
if (matched) {
return acc
}
matched = true
return modifiers
})
if (result.matched) {
return result.modifiers
}
// Then look at required
result = this.collectModifiers(this.opts.required, paths, (acc, modifiers) => {
modifiers.forEach((m) => acc.add(m))
return acc
})
return result.modifiers
}
FunctionDefinition(ctx) {
let contract = this.context[this.context.length - 1]
if (this.opts.ignoreContractKind.includes(contract.kind) ||
this.opts.ignoreVisibility.includes(ctx.visibility) ||
this.opts.ignoreStateMutability.includes(ctx.stateMutability)) {
return
}
let contractName = contract.name
let functionName = ctx.name
let requiredModifiers
try {
requiredModifiers = this.matchedModifiers(contractName, functionName)
} catch(err) {
if (this.opts.verbose) {
console.log(err)
}
throw err
}
ctx.modifiers.forEach((m) => {
requiredModifiers.delete(m.name)
})
if (requiredModifiers.size > 0) {
requiredModifiers.forEach((m) => {
this.reporter.error(
ctx,
this.ruleId,
`Function \`${contractName}.${functionName}\` does not have required modifier \`${m}\`.`
)
})
}
}
}
module.exports = [EnsureModifiers]