Skip to content

Commit

Permalink
base named-parameters rule
Browse files Browse the repository at this point in the history
  • Loading branch information
juanpcapurro committed Jul 3, 2023
1 parent b4064eb commit 162fb96
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/rules/naming/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const PrivateVarsLeadingUnderscore = require('./private-vars-leading-underscore'
const UseForbiddenNameChecker = require('./use-forbidden-name')
const VarNameMixedcaseChecker = require('./var-name-mixedcase')
const NamedParametersMapping = require('./named-parameters-mapping')
const NamedParametersFunction = require('./named-parameters-function')

module.exports = function checkers(reporter, config) {
return [
Expand All @@ -21,5 +22,6 @@ module.exports = function checkers(reporter, config) {
new UseForbiddenNameChecker(reporter),
new VarNameMixedcaseChecker(reporter),
new NamedParametersMapping(reporter),
new NamedParametersFunction(reporter, config),
]
}
87 changes: 87 additions & 0 deletions lib/rules/naming/named-parameters-function.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
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
)`,
},
],
},
},

isDefault: false,
recommended: true,
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
43 changes: 43 additions & 0 deletions test/rules/naming/named-parameters-function.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const linter = require('../../../lib/index')
const { assertNoWarnings, assertWarnsCount } = require('../../common/asserts')
const { contractWith, multiLine } = require('../../common/contract-builder')

describe('Linter - named-parameters-function', () => {
it('GIVEN a setting of 4, should NOT warn on calling with four positional arguments', () => {
const code = contractWith(
multiLine('function foo(uint a, uint b, uint c, uint d) {}', `function bar (){foo(1,2,3,4);}`)
)

const report = linter.processStr(code, {
rules: { 'named-parameters-function': ['warn', 4] },
})

assertNoWarnings(report)
})

it('GIVEN default settings, should warn on calling with four positional arguments', () => {
const code = contractWith(
multiLine('function foo(uint a, uint b, uint c, uint d) {}', `function bar (){foo(1,2,3,4);}`)
)

const report = linter.processStr(code, {
rules: { 'named-parameters-function': 'warn' },
})

assertWarnsCount(report, 1)
})

it('GIVEN default settings, should NOT warn on calling with four named arguments', () => {
const code = contractWith(
multiLine(
'function foo(uint a, uint b, uint c, uint d) {}',
`function bar (){foo({a: 1, b: 2, c:3, d:4});}`
)
)

const report = linter.processStr(code, {
rules: { 'named-parameters-function': 'warn' },
})
assertNoWarnings(report)
})
})

0 comments on commit 162fb96

Please sign in to comment.