Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 one-contract-per-file: ignore interfaces #514

Merged
merged 3 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/rules/best-practises/one-contract-per-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class OneContractPerFileChecker extends BaseChecker {

SourceUnit(node) {
const contractDefinitionCount = node.children.reduce((count, child) => {
if (child.type === 'ContractDefinition') {
if (child.type === 'ContractDefinition' && child.kind !== 'interface') {
return count + 1
}
return count
Expand Down
38 changes: 37 additions & 1 deletion test/fixtures/best-practises/one-contract-per-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,40 @@ const THREE_CONTRACTS = `
uint256 public constant TESTC = "testC";
}
`
module.exports = { ONE_CONTRACT, TWO_CONTRACTS, THREE_CONTRACTS }

const TWO_LIBRARIES = `
pragma solidity 0.8.0;

library A { }

library B { }
`

const ONE_CONTRACT_WITH_INTERFACES = `
pragma solidity 0.8.0;

contract A { }

interface B { }

interface C { }
`

const ONE_LIBRARY_WITH_INTERFACES = `
pragma solidity 0.8.0;

library A { }

interface B { }

interface C { }
`

module.exports = {
ONE_CONTRACT,
TWO_CONTRACTS,
THREE_CONTRACTS,
TWO_LIBRARIES,
ONE_CONTRACT_WITH_INTERFACES,
ONE_LIBRARY_WITH_INTERFACES,
}
31 changes: 31 additions & 0 deletions test/rules/best-practises/one-contract-per-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,26 @@ describe('Linter - one-contract-per-file', () => {
assertNoWarnings(report)
})

it('should not raise error for ONE contract and multiple interfaces in the same file', () => {
const code = contracts.ONE_CONTRACT_WITH_INTERFACES

const report = linter.processStr(code, {
rules: { 'one-contract-per-file': 'error' },
})

assertNoWarnings(report)
})

it('should not raise error for ONE library and multiple interfaces in the same file', () => {
const code = contracts.ONE_LIBRARY_WITH_INTERFACES

const report = linter.processStr(code, {
rules: { 'one-contract-per-file': 'error' },
})

assertNoWarnings(report)
})

it('should raise error for TWO contracts in same file', () => {
const code = contracts.TWO_CONTRACTS

Expand All @@ -34,4 +54,15 @@ describe('Linter - one-contract-per-file', () => {
assertErrorCount(report, 1)
assertErrorMessage(report, 'Found more than One contract per file. 3 contracts found!')
})

it('should raise error for TWO libraries in same file', () => {
const code = contracts.TWO_LIBRARIES

const report = linter.processStr(code, {
rules: { 'one-contract-per-file': 'error' },
})

assertErrorCount(report, 1)
assertErrorMessage(report, 'Found more than One contract per file. 2 contracts found!')
})
})
Loading