-
Notifications
You must be signed in to change notification settings - Fork 1
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 #25 from solhint-community/named-parameters-rule
Named parameters rule
- Loading branch information
Showing
10 changed files
with
368 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
--- | ||
warning: "This is a dynamically generated file. Do not edit manually." | ||
layout: "default" | ||
title: "named-parameters-function | Solhint" | ||
--- | ||
|
||
# named-parameters-function | ||
![Category Badge](https://img.shields.io/badge/-Best%20Practise%20Rules-informational) | ||
![Default Severity Badge warn](https://img.shields.io/badge/Default%20Severity-warn-yellow) | ||
|
||
## Description | ||
Enforce using named parameters when invoking a function with more than N arguments | ||
|
||
## Options | ||
This rule accepts an array of options: | ||
|
||
| Index | Description | Default Value | | ||
| ----- | -------------------------------------------------------------------------------------------------------------- | ------------- | | ||
| 0 | Rule severity. Must be one of "error", "warn", "off". | warn | | ||
| 1 | A Number specifying the max amount of arguments a function can have while still allowing positional arguments. | 3 | | ||
|
||
|
||
### Example Config | ||
```json | ||
{ | ||
"rules": { | ||
"named-parameters-function": ["warn",3] | ||
} | ||
} | ||
``` | ||
|
||
|
||
## Examples | ||
### 👍 Examples of **correct** code for this rule | ||
|
||
#### Calling a function with few positional arguments | ||
|
||
```solidity | ||
foo(10,200) | ||
``` | ||
|
||
#### Calling a function with few named arguments | ||
|
||
```solidity | ||
foo({amount: 10, price: 200}) | ||
``` | ||
|
||
#### Calling a function with many named arguments | ||
|
||
```solidity | ||
foo({ | ||
amount: 10, | ||
price: 200, | ||
recipient: 0x690B9A9E9aa1C9dB991C7721a92d351Db4FaC990, | ||
token: 0xdac17f958d2ee523a2206206994597c13d831ec7 | ||
}) | ||
``` | ||
|
||
### 👎 Examples of **incorrect** code for this rule | ||
|
||
#### Calling a function with many positional arguments | ||
|
||
```solidity | ||
foo( | ||
10, | ||
200, | ||
0x690B9A9E9aa1C9dB991C7721a92d351Db4FaC990, | ||
0xdac17f958d2ee523a2206206994597c13d831ec7 | ||
) | ||
``` | ||
|
||
#### With a config value of 0, using positional arguments in _any_ capacity | ||
|
||
```solidity | ||
foo(10) | ||
``` | ||
|
||
## Version | ||
This rule is introduced in the latest version. | ||
|
||
## Resources | ||
- [Rule source](https://github.com/solhint-community/solhint-community/tree/master/lib/rules/naming/named-parameters-function.js) | ||
- [Document source](https://github.com/solhint-community/solhint-community/tree/master/docs/rules/naming/named-parameters-function.md) | ||
- [Test cases](https://github.com/solhint-community/solhint-community/tree/master/test/rules/naming/named-parameters-function.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
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,93 @@ | ||
const BaseChecker = require('../base-checker') | ||
const { severityDescription } = require('../../doc/utils') | ||
|
||
const DEFAULT_SEVERITY = 'warn' | ||
const DEFAULT_MAX_POSITIONAL_ARGUMENTS = 3 | ||
|
||
const ruleId = 'named-parameters-function' | ||
const meta = { | ||
type: 'naming', | ||
|
||
docs: { | ||
description: | ||
'Enforce using named parameters when invoking a function with more than N arguments', | ||
category: 'Best Practise Rules', | ||
options: [ | ||
{ | ||
description: severityDescription, | ||
default: DEFAULT_SEVERITY, | ||
}, | ||
{ | ||
description: | ||
'A Number specifying the max amount of arguments a function can have while still allowing positional arguments.', | ||
default: JSON.stringify(DEFAULT_MAX_POSITIONAL_ARGUMENTS), | ||
}, | ||
], | ||
examples: { | ||
good: [ | ||
{ | ||
description: 'Calling a function with few positional arguments', | ||
code: 'foo(10,200)', | ||
}, | ||
{ | ||
description: 'Calling a function with few named arguments', | ||
code: 'foo({amount: 10, price: 200})', | ||
}, | ||
{ | ||
description: 'Calling a function with many named arguments', | ||
code: ` | ||
foo({ | ||
amount: 10, | ||
price: 200, | ||
recipient: 0x690B9A9E9aa1C9dB991C7721a92d351Db4FaC990, | ||
token: 0xdac17f958d2ee523a2206206994597c13d831ec7 | ||
})`, | ||
}, | ||
], | ||
bad: [ | ||
{ | ||
description: 'Calling a function with many positional arguments', | ||
code: ` | ||
foo( | ||
10, | ||
200, | ||
0x690B9A9E9aa1C9dB991C7721a92d351Db4FaC990, | ||
0xdac17f958d2ee523a2206206994597c13d831ec7 | ||
)`, | ||
}, | ||
{ | ||
description: 'With a config value of 0, using positional arguments in _any_ capacity', | ||
code: `foo(10)`, | ||
}, | ||
], | ||
}, | ||
}, | ||
|
||
isDefault: false, | ||
recommended: false, | ||
defaultSetup: [DEFAULT_SEVERITY, DEFAULT_MAX_POSITIONAL_ARGUMENTS], | ||
|
||
schema: { type: 'integer' }, | ||
} | ||
|
||
class FunctionNamedParametersChecker extends BaseChecker { | ||
constructor(reporter, config) { | ||
super(reporter, ruleId, meta) | ||
this.maxPositionalArguments = config | ||
? config.getNumber(ruleId, DEFAULT_MAX_POSITIONAL_ARGUMENTS) | ||
: DEFAULT_MAX_POSITIONAL_ARGUMENTS | ||
} | ||
|
||
FunctionCall(node) { | ||
if (node.names.length === 0) { | ||
if (node.arguments.length > this.maxPositionalArguments) { | ||
this.error( | ||
node, | ||
`Call to function with arity > ${this.maxPositionalArguments} is using positional arguments. Use named arguments instead.` | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
module.exports = FunctionNamedParametersChecker |
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
Oops, something went wrong.