-
Notifications
You must be signed in to change notification settings - Fork 179
/
Copy pathfunc-visibility.js
98 lines (78 loc) · 3.17 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
91
92
93
94
95
96
97
98
const assert = require('assert')
const linter = require('../../../lib/index')
const contractWith = require('../../common/contract-builder').contractWith
const CONTRACTS = require('../../fixtures/security/contracts-with-free-functions')
describe('Linter - func-visibility with free functions', () => {
it('should return two warnings and skip free functions', () => {
const code = CONTRACTS.CONTRACTS_FREE_FUNCTIONS_ERRORS_2
const report = linter.processStr(code, {
rules: { 'func-visibility': ['warn', { ignoreConstructors: true }] },
})
assert.equal(report.warningCount, 2)
assert.ok(report.reports[0].message.includes('visibility'))
assert.ok(report.reports[1].message.includes('visibility'))
})
it('should return one warning and skip free functions', () => {
const code = CONTRACTS.CONTRACT_FREE_FUNCTIONS_ERRORS_1
const report = linter.processStr(code, {
rules: { 'func-visibility': ['warn', { ignoreConstructors: true }] },
})
assert.equal(report.warningCount, 1)
assert.ok(report.reports[0].message.includes('visibility'))
})
it('should not return any warning for a free function only', () => {
const code = CONTRACTS.NOCONTRACT_FREE_FUNCTION_ERRORS_0
const report = linter.processStr(code, {
rules: { 'func-visibility': ['warn', { ignoreConstructors: true }] },
})
assert.equal(report.warningCount, 0)
})
it('should not return any warning for a correct contract with a free function', () => {
const code = CONTRACTS.CONTRACT_FREE_FUNCTIONS_ERRORS_0
const report = linter.processStr(code, {
rules: { 'func-visibility': ['warn', { ignoreConstructors: true }] },
})
assert.equal(report.warningCount, 0)
})
})
describe('Linter - func-visibility', () => {
it('should return required visibility error', () => {
require('../../fixtures/security/functions-without-visibility').forEach((func) => {
const code = contractWith(func)
const report = linter.processStr(code, {
rules: { 'func-visibility': 'warn' },
})
assert.equal(report.warningCount, 1)
assert.ok(report.reports[0].message.includes('visibility'))
})
})
it('should not return required visibility error', () => {
require('../../fixtures/security/functions-with-visibility').forEach((func) => {
const code = contractWith(func)
const report = linter.processStr(code, {
rules: { 'func-visibility': 'warn' },
})
assert.equal(report.warningCount, 0)
})
})
describe("when 'ignoreConstructors' is enabled", () => {
it('should ignore constructors without visibility', () => {
const code = contractWith('constructor () {}')
const report = linter.processStr(code, {
rules: {
'func-visibility': ['warn', { ignoreConstructors: true }],
},
})
assert.equal(report.warningCount, 0)
})
it('should still report functions without visibility', () => {
const code = contractWith('function foo() {}')
const report = linter.processStr(code, {
rules: {
'func-visibility': ['warn', { ignoreConstructors: true }],
},
})
assert.equal(report.warningCount, 1)
})
})
})