-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: explicit returns solhint rule (#7)
- Loading branch information
Showing
4 changed files
with
73 additions
and
2 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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"name": "@chainlink/solhint-plugin-chainlink-solidity", | ||
"version": "1.0.1", | ||
"version": "1.1.1", | ||
"main": "index.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Rule: Returned values should always be explicit. | ||
// Using named return values and then returning with an empty return should be avoided. | ||
class ExplicitReturns { | ||
constructor(reporter, config) { | ||
this.ruleId = 'explicit-returns'; | ||
this.reporter = reporter; | ||
this.config = config; | ||
} | ||
|
||
FunctionDefinition(ctx) { | ||
const { returnParameters, body } = ctx; | ||
// collect all VariableDeclaration nodes with non-null "name" property | ||
// in the returnParameters array | ||
let namedReturns = []; | ||
for (let i = 0; i < returnParameters.length; i++) { | ||
const varDecl = returnParameters[i]; | ||
if (varDecl.name != null) { | ||
namedReturns.push(varDecl.name); | ||
} | ||
} | ||
// if there are no named returns, return | ||
if (namedReturns.length === 0) { | ||
return; | ||
} | ||
|
||
// check if body has a ReturnStatement with a non-null "expression" property | ||
let hasReturn = false; | ||
let hasReturnExpression = false; | ||
const { statements } = body; | ||
for (let i = 0; i < statements.length; i++) { | ||
const stmt = statements[i]; | ||
// Functions with named returns in solidity need not have a return statement | ||
// they can just assign the named returns a value and fall off the end of the function | ||
// we want to warn against that explicitly, hence the check for "ReturnStatement" | ||
// and for the expression being separate. | ||
if (stmt.type === 'ReturnStatement') { | ||
hasReturn = true; | ||
if (stmt.expression != null) { | ||
hasReturnExpression = true; | ||
break; | ||
} | ||
} | ||
} | ||
const returnExprGen = (namedRets) => { | ||
if (namedRets.length === 1) { | ||
return namedRets[0]; | ||
} | ||
return `(${namedRets.join(', ')})`; | ||
} | ||
if (!hasReturn) { | ||
this.reporter.error( | ||
ctx, | ||
this.ruleId, | ||
`Return statements must be written and must explicitly return something; consider "return ${returnExprGen(namedReturns)};"?`); | ||
return; | ||
} | ||
if (!hasReturnExpression) { | ||
this.reporter.error( | ||
ctx, | ||
this.ruleId, | ||
`Return statements must explicitly return something; consider "return ${returnExprGen(namedReturns)};"?`); | ||
return; | ||
} | ||
} | ||
} | ||
|
||
module.exports = ExplicitReturns; |