-
Notifications
You must be signed in to change notification settings - Fork 169
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 #478 from protofire/i340-name-return-values
New Rule: named-return-values
- Loading branch information
Showing
10 changed files
with
233 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
--- | ||
warning: "This is a dynamically generated file. Do not edit manually." | ||
layout: "default" | ||
title: "named-return-values | Solhint" | ||
--- | ||
|
||
# named-return-values | ||
![Category Badge](https://img.shields.io/badge/-Style%20Guide%20Rules-informational) | ||
![Default Severity Badge warn](https://img.shields.io/badge/Default%20Severity-warn-yellow) | ||
|
||
## Description | ||
Enforce the return values of a function to be named | ||
|
||
## Options | ||
This rule accepts a string option of rule severity. Must be one of "error", "warn", "off". Default to warn. | ||
|
||
### Example Config | ||
```json | ||
{ | ||
"rules": { | ||
"named-return-values": "warn" | ||
} | ||
} | ||
``` | ||
|
||
|
||
## Examples | ||
### 👍 Examples of **correct** code for this rule | ||
|
||
#### Function definition with named return values | ||
|
||
```solidity | ||
function checkBalance(address wallet) external view returns(uint256 retBalance) {} | ||
``` | ||
|
||
### 👎 Examples of **incorrect** code for this rule | ||
|
||
#### Function definition with UNNAMED return values | ||
|
||
```solidity | ||
function checkBalance(address wallet) external view returns(uint256) {} | ||
``` | ||
|
||
## Version | ||
This rule is introduced in the latest version. | ||
|
||
## Resources | ||
- [Rule source](https://github.com/protofire/solhint/tree/master/lib/rules/naming/named-return-values.js) | ||
- [Document source](https://github.com/protofire/solhint/tree/master/docs/rules/naming/named-return-values.md) | ||
- [Test cases](https://github.com/protofire/solhint/tree/master/test/rules/naming/named-return-values.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,60 @@ | ||
const BaseChecker = require('../base-checker') | ||
const { severityDescription } = require('../../doc/utils') | ||
|
||
const DEFAULT_SEVERITY = 'warn' | ||
|
||
const ruleId = 'named-return-values' | ||
const meta = { | ||
type: 'naming', | ||
|
||
docs: { | ||
description: `Enforce the return values of a function to be named`, | ||
category: 'Style Guide Rules', | ||
options: [ | ||
{ | ||
description: severityDescription, | ||
default: DEFAULT_SEVERITY, | ||
}, | ||
], | ||
examples: { | ||
good: [ | ||
{ | ||
description: 'Function definition with named return values', | ||
code: 'function checkBalance(address wallet) external view returns(uint256 retBalance) {}', | ||
}, | ||
], | ||
bad: [ | ||
{ | ||
description: 'Function definition with UNNAMED return values', | ||
code: 'function checkBalance(address wallet) external view returns(uint256) {}', | ||
}, | ||
], | ||
}, | ||
}, | ||
|
||
isDefault: false, | ||
recommended: false, | ||
defaultSetup: DEFAULT_SEVERITY, | ||
|
||
schema: null, | ||
} | ||
|
||
class NamedReturnValuesChecker extends BaseChecker { | ||
constructor(reporter) { | ||
super(reporter, ruleId, meta) | ||
} | ||
|
||
FunctionDefinition(node) { | ||
if (node.returnParameters) { | ||
let index = 0 | ||
for (const returnValue of node.returnParameters) { | ||
if (!returnValue.name) { | ||
this.error(node, `Named return value is missing - Index ${index}`) | ||
} | ||
index++ | ||
} | ||
} | ||
} | ||
} | ||
|
||
module.exports = NamedReturnValuesChecker |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,92 @@ | ||
const assert = require('assert') | ||
const linter = require('../../../lib/index') | ||
const contractWith = require('../../common/contract-builder').contractWith | ||
const { assertErrorCount, assertNoErrors, assertWarnsCount } = require('../../common/asserts') | ||
|
||
describe('Linter - named-return-values', () => { | ||
it('should NOT raise error for named return values', () => { | ||
const code = contractWith( | ||
`function getBalanceFromTokens(address wallet) public returns(address token1, address token2, uint256 balance1, uint256 balance2) { balance = 1; }` | ||
) | ||
const report = linter.processStr(code, { | ||
rules: { 'named-return-values': 'error' }, | ||
}) | ||
assertNoErrors(report) | ||
}) | ||
|
||
it('should raise error for unnamed return values', () => { | ||
const code = contractWith( | ||
`function getBalanceFromTokens(address wallet) public returns(address, address, uint256, uint256) { balance = 1; }` | ||
) | ||
const report = linter.processStr(code, { | ||
rules: { 'named-return-values': 'error' }, | ||
}) | ||
|
||
assertErrorCount(report, 4) | ||
for (let index = 0; index < report.reports.length; index++) { | ||
assert.equal(report.reports[index].message, `Named return value is missing - Index ${index}`) | ||
} | ||
}) | ||
|
||
it('should NOT raise error for functions without return values', () => { | ||
const code = contractWith(`function writeOnStorage(address wallet) public { balance = 1; }`) | ||
const report = linter.processStr(code, { | ||
rules: { 'named-return-values': 'error' }, | ||
}) | ||
assertNoErrors(report) | ||
}) | ||
|
||
it('should raise error for 2 unnamed return values', () => { | ||
const code = contractWith( | ||
`function getBalanceFromTokens(address wallet) public returns(address user, address, uint256 amount, uint256) { balance = 1; }` | ||
) | ||
const report = linter.processStr(code, { | ||
rules: { 'named-return-values': 'error' }, | ||
}) | ||
|
||
assertErrorCount(report, 2) | ||
assert.equal(report.reports[0].message, `Named return value is missing - Index 1`) | ||
assert.equal(report.reports[1].message, `Named return value is missing - Index 3`) | ||
}) | ||
|
||
it('should NOT raise error for solhint:recommended setup', () => { | ||
const code = contractWith( | ||
`function getBalanceFromTokens(address wallet) public returns(address, address, uint256, uint256) { balance = 1; }` | ||
) | ||
|
||
const report = linter.processStr(code, { | ||
extends: 'solhint:recommended', | ||
rules: { 'compiler-version': 'off' }, | ||
}) | ||
|
||
assertNoErrors(report) | ||
}) | ||
|
||
it('should NOT raise error for solhint:default setup', () => { | ||
const code = contractWith( | ||
`function getBalance(address wallet) public returns(uint256) { balance = 1; }` | ||
) | ||
|
||
const report = linter.processStr(code, { | ||
extends: 'solhint:default', | ||
}) | ||
|
||
assertNoErrors(report) | ||
}) | ||
|
||
it('should raise error for solhint:all setup', () => { | ||
const code = contractWith( | ||
`function getBalance(uint256 wallet) public override returns(uint256, address) { wallet = 1; }` | ||
) | ||
|
||
const report = linter.processStr(code, { | ||
extends: 'solhint:all', | ||
rules: { 'compiler-version': 'off' }, | ||
}) | ||
|
||
assertWarnsCount(report, 2) | ||
for (let index = 0; index < report.reports.length; index++) { | ||
assert.equal(report.reports[index].message, `Named return value is missing - Index ${index}`) | ||
} | ||
}) | ||
}) |
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