Skip to content

Commit f9804a8

Browse files
Merge pull request #514 from cruzdanilo/interfaces
🐛 one-contract-per-file: ignore interfaces
2 parents cf11c92 + ffb4a16 commit f9804a8

File tree

3 files changed

+69
-2
lines changed

3 files changed

+69
-2
lines changed

lib/rules/best-practises/one-contract-per-file.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class OneContractPerFileChecker extends BaseChecker {
3333

3434
SourceUnit(node) {
3535
const contractDefinitionCount = node.children.reduce((count, child) => {
36-
if (child.type === 'ContractDefinition') {
36+
if (child.type === 'ContractDefinition' && child.kind !== 'interface') {
3737
return count + 1
3838
}
3939
return count

test/fixtures/best-practises/one-contract-per-file.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,40 @@ const THREE_CONTRACTS = `
3333
uint256 public constant TESTC = "testC";
3434
}
3535
`
36-
module.exports = { ONE_CONTRACT, TWO_CONTRACTS, THREE_CONTRACTS }
36+
37+
const TWO_LIBRARIES = `
38+
pragma solidity 0.8.0;
39+
40+
library A { }
41+
42+
library B { }
43+
`
44+
45+
const ONE_CONTRACT_WITH_INTERFACES = `
46+
pragma solidity 0.8.0;
47+
48+
contract A { }
49+
50+
interface B { }
51+
52+
interface C { }
53+
`
54+
55+
const ONE_LIBRARY_WITH_INTERFACES = `
56+
pragma solidity 0.8.0;
57+
58+
library A { }
59+
60+
interface B { }
61+
62+
interface C { }
63+
`
64+
65+
module.exports = {
66+
ONE_CONTRACT,
67+
TWO_CONTRACTS,
68+
THREE_CONTRACTS,
69+
TWO_LIBRARIES,
70+
ONE_CONTRACT_WITH_INTERFACES,
71+
ONE_LIBRARY_WITH_INTERFACES,
72+
}

test/rules/best-practises/one-contract-per-file.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,26 @@ describe('Linter - one-contract-per-file', () => {
1313
assertNoWarnings(report)
1414
})
1515

16+
it('should not raise error for ONE contract and multiple interfaces in the same file', () => {
17+
const code = contracts.ONE_CONTRACT_WITH_INTERFACES
18+
19+
const report = linter.processStr(code, {
20+
rules: { 'one-contract-per-file': 'error' },
21+
})
22+
23+
assertNoWarnings(report)
24+
})
25+
26+
it('should not raise error for ONE library and multiple interfaces in the same file', () => {
27+
const code = contracts.ONE_LIBRARY_WITH_INTERFACES
28+
29+
const report = linter.processStr(code, {
30+
rules: { 'one-contract-per-file': 'error' },
31+
})
32+
33+
assertNoWarnings(report)
34+
})
35+
1636
it('should raise error for TWO contracts in same file', () => {
1737
const code = contracts.TWO_CONTRACTS
1838

@@ -34,4 +54,15 @@ describe('Linter - one-contract-per-file', () => {
3454
assertErrorCount(report, 1)
3555
assertErrorMessage(report, 'Found more than One contract per file. 3 contracts found!')
3656
})
57+
58+
it('should raise error for TWO libraries in same file', () => {
59+
const code = contracts.TWO_LIBRARIES
60+
61+
const report = linter.processStr(code, {
62+
rules: { 'one-contract-per-file': 'error' },
63+
})
64+
65+
assertErrorCount(report, 1)
66+
assertErrorMessage(report, 'Found more than One contract per file. 2 contracts found!')
67+
})
3768
})

0 commit comments

Comments
 (0)