Skip to content

Commit

Permalink
Merge pull request #396 from protofire/fix/disable-func-visibility-fo…
Browse files Browse the repository at this point in the history
…r-free-functions

fix: skip free functions on func-visibility
  • Loading branch information
dbale-altoros committed Feb 8, 2023
2 parents b81c3fc + 242dd06 commit d4c0a46
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 7 deletions.
24 changes: 17 additions & 7 deletions lib/rules/security/func-visibility.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,28 @@ class FuncVisibilityChecker extends BaseChecker {
config && config.getObjectPropertyBoolean(ruleId, 'ignoreConstructors', false)
}

ContractDefinition() {
this.isContract = true
}

'ContractDefinition:exit'() {
this.isContract = false
}

FunctionDefinition(node) {
if (node.isConstructor && this.ignoreConstructors) {
return
}

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'
)
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'
)
}
}
}
}
Expand Down
69 changes: 69 additions & 0 deletions test/fixtures/security/contracts-with-free-functions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const CONTRACTS_FREE_FUNCTIONS_ERRORS_2 = `
function freeAa() returns(bool) {
return true;
}
contract A {
// error here
function functionPublicViewA() returns (uint256) {
return 1;
}
}
function freeBb() returns(bool) {
return true;
}
contract B {
// error here
function functionPublicViewA() returns (uint256) {
return 1;
}
function functionPublicPureB() internal pure returns (bool) {
return true;
}
}
`

const CONTRACT_FREE_FUNCTIONS_ERRORS_1 = `
contract A {
constructor() {}
// error here
function functionPublicViewA() returns (uint256) {
return 1;
}
}
function freeBb() returns(bool) {
return true;
}
`

const NOCONTRACT_FREE_FUNCTION_ERRORS_0 = `
// NO error here
function functionPublicViewA() returns (uint256) {
return 1;
}
`

const CONTRACT_FREE_FUNCTIONS_ERRORS_0 = `
function freeBb() returns(bool) {
return true;
}
contract A {
constructor() {}
// NO error here
function functionPublicViewA() external returns (uint256) {
return 1;
}
}
`

module.exports = {
CONTRACTS_FREE_FUNCTIONS_ERRORS_2,
CONTRACT_FREE_FUNCTIONS_ERRORS_1,
NOCONTRACT_FREE_FUNCTION_ERRORS_0,
CONTRACT_FREE_FUNCTIONS_ERRORS_0,
}
42 changes: 42 additions & 0 deletions test/rules/security/func-visibility.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,48 @@
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', () => {
Expand Down

0 comments on commit d4c0a46

Please sign in to comment.