-
Notifications
You must be signed in to change notification settings - Fork 165
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #487 from protofire/i154-one-contract-per-file
New Rule one contract per file
- Loading branch information
Showing
8 changed files
with
184 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
--- | ||
warning: "This is a dynamically generated file. Do not edit manually." | ||
layout: "default" | ||
title: "one-contract-per-file | Solhint" | ||
--- | ||
|
||
# one-contract-per-file | ||
![Recommended Badge](https://img.shields.io/badge/-Recommended-brightgreen) | ||
![Category Badge](https://img.shields.io/badge/-Best%20Practise%20Rules-informational) | ||
![Default Severity Badge warn](https://img.shields.io/badge/Default%20Severity-warn-yellow) | ||
> The {"extends": "solhint:recommended"} property in a configuration file enables this rule. | ||
|
||
## Description | ||
Enforces the use of ONE Contract per file see [here](https://docs.soliditylang.org/en/v0.8.21/style-guide.html#contract-and-library-names) | ||
|
||
## Options | ||
This rule accepts a string option of rule severity. Must be one of "error", "warn", "off". Default to warn. | ||
|
||
### Example Config | ||
```json | ||
{ | ||
"rules": { | ||
"one-contract-per-file": "warn" | ||
} | ||
} | ||
``` | ||
|
||
|
||
## Examples | ||
This rule does not have examples. | ||
|
||
## Version | ||
This rule is introduced in the latest version. | ||
|
||
## Resources | ||
- [Rule source](https://github.com/protofire/solhint/tree/master/lib/rules/best-practises/one-contract-per-file.js) | ||
- [Document source](https://github.com/protofire/solhint/tree/master/docs/rules/best-practises/one-contract-per-file.md) | ||
- [Test cases](https://github.com/protofire/solhint/tree/master/test/rules/best-practises/one-contract-per-file.js) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
const BaseChecker = require('../base-checker') | ||
const { severityDescription } = require('../../doc/utils') | ||
|
||
const DEFAULT_SEVERITY = 'warn' | ||
|
||
const ruleId = 'one-contract-per-file' | ||
const meta = { | ||
type: 'best-practises', | ||
|
||
docs: { | ||
description: | ||
'Enforces the use of ONE Contract per file see [here](https://docs.soliditylang.org/en/v0.8.21/style-guide.html#contract-and-library-names)', | ||
category: 'Best Practise Rules', | ||
options: [ | ||
{ | ||
description: severityDescription, | ||
default: DEFAULT_SEVERITY, | ||
}, | ||
], | ||
}, | ||
|
||
isDefault: false, | ||
recommended: true, | ||
defaultSetup: DEFAULT_SEVERITY, | ||
|
||
schema: null, | ||
} | ||
|
||
class OneContractPerFileChecker extends BaseChecker { | ||
constructor(reporter) { | ||
super(reporter, ruleId, meta) | ||
} | ||
|
||
SourceUnit(node) { | ||
const contractDefinitionCount = node.children.reduce((count, child) => { | ||
if (child.type === 'ContractDefinition') { | ||
return count + 1 | ||
} | ||
return count | ||
}, 0) | ||
|
||
if (contractDefinitionCount > 1) { | ||
this.error( | ||
node, | ||
`Found more than One contract per file. ${contractDefinitionCount} contracts found!` | ||
) | ||
} | ||
} | ||
} | ||
|
||
module.exports = OneContractPerFileChecker |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
const ONE_CONTRACT = ` | ||
pragma solidity 0.8.0; | ||
contract A { | ||
uint256 public constant TESTA = "testA"; | ||
} | ||
` | ||
|
||
const TWO_CONTRACTS = ` | ||
pragma solidity 0.8.0; | ||
contract A { | ||
uint256 public constant TESTA = "testA"; | ||
} | ||
contract B { | ||
uint256 public constant TESTB = "testB"; | ||
} | ||
` | ||
|
||
const THREE_CONTRACTS = ` | ||
pragma solidity 0.8.0; | ||
contract A { | ||
uint256 public constant TESTA = "testA"; | ||
} | ||
contract B { | ||
uint256 public constant TESTB = "testB"; | ||
} | ||
contract C { | ||
uint256 public constant TESTC = "testC"; | ||
} | ||
` | ||
module.exports = { ONE_CONTRACT, TWO_CONTRACTS, THREE_CONTRACTS } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
const { assertNoWarnings, assertErrorMessage, assertErrorCount } = require('../../common/asserts') | ||
const linter = require('../../../lib/index') | ||
const contracts = require('../../fixtures/best-practises/one-contract-per-file') | ||
|
||
describe('Linter - one-contract-per-file', () => { | ||
it('should not raise error for ONE contract only', () => { | ||
const code = contracts.ONE_CONTRACT | ||
|
||
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 | ||
|
||
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!') | ||
}) | ||
|
||
it('should raise error for THREE contracts in same file', () => { | ||
const code = contracts.THREE_CONTRACTS | ||
|
||
const report = linter.processStr(code, { | ||
rules: { 'one-contract-per-file': 'error' }, | ||
}) | ||
|
||
assertErrorCount(report, 1) | ||
assertErrorMessage(report, 'Found more than One contract per file. 3 contracts found!') | ||
}) | ||
}) |