-
Notifications
You must be signed in to change notification settings - Fork 181
/
Copy pathfunc-visibility.js
90 lines (78 loc) · 2.3 KB
/
func-visibility.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
const BaseChecker = require('../base-checker')
const { severityDescription } = require('../../doc/utils')
const DEFAULT_SEVERITY = 'warn'
const DEFAULT_IGNORE_CONSTRUCTORS = false
const DEFAULT_OPTION = { ignoreConstructors: DEFAULT_IGNORE_CONSTRUCTORS }
const ruleId = 'func-visibility'
const meta = {
type: 'security',
docs: {
description: `Explicitly mark visibility in function.`,
category: 'Security Rules',
options: [
{
description: severityDescription,
default: DEFAULT_SEVERITY,
},
{
description:
'A JSON object with a single property "ignoreConstructors" specifying if the rule should ignore constructors. (**Note: This is required to be true for Solidity >=0.7.0 and false for <0.7.0**)',
default: JSON.stringify(DEFAULT_OPTION),
},
],
examples: {
good: [
{
description: 'Functions explicitly marked with visibility',
code: require('../../../test/fixtures/security/functions-with-visibility').join('\n'),
},
],
bad: [
{
description: 'Functions without explicitly marked visibility',
code: require('../../../test/fixtures/security/functions-without-visibility').join('\n'),
},
],
},
},
isDefault: false,
recommended: true,
defaultSetup: [DEFAULT_SEVERITY, DEFAULT_OPTION],
schema: {
type: 'object',
properties: {
ignoreConstructors: {
type: 'boolean',
},
},
},
}
class FuncVisibilityChecker extends BaseChecker {
constructor(reporter, config) {
super(reporter, ruleId, meta)
this.ignoreConstructors =
config && config.getObjectPropertyBoolean(ruleId, 'ignoreConstructors', false)
}
ContractDefinition() {
this.isContract = true
}
'ContractDefinition:exit'() {
this.isContract = false
}
FunctionDefinition(node) {
if (node.isConstructor && this.ignoreConstructors) {
return
}
if (this.isContract) {
if (node.visibility === 'default') {
this.warn(
node,
node.isConstructor
? 'Explicitly mark visibility in function (Set ignoreConstructors to true if using solidity >=0.7.0)'
: 'Explicitly mark visibility in function'
)
}
}
}
}
module.exports = FuncVisibilityChecker