Skip to content
This repository was archived by the owner on Sep 27, 2022. It is now read-only.

Commit 83db412

Browse files
Brendan ChouAntonioJuliano
authored andcommitted
Add more contract linting rules (#355)
* lint function comments * check for aligned parameters
1 parent b4d6fee commit 83db412

File tree

2 files changed

+75
-11
lines changed

2 files changed

+75
-11
lines changed

contracts/margin/Margin.sol

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ contract Margin is
485485
/**
486486
* Cancel an amount of a loan offering. Only callable by the loan offering's payer or signer.
487487
*
488-
* @param addresses Array of addresses:
488+
* @param addresses Array of addresses:
489489
*
490490
* [0] = owedToken
491491
* [1] = heldToken
@@ -498,7 +498,7 @@ contract Margin is
498498
* [8] = loan lender fee token
499499
* [9] = loan taker fee token
500500
*
501-
* @param values256 Values corresponding to:
501+
* @param values256 Values corresponding to:
502502
*
503503
* [0] = loan maximum amount
504504
* [1] = loan minimum amount
@@ -508,15 +508,15 @@ contract Margin is
508508
* [5] = loan expiration timestamp (in seconds)
509509
* [6] = loan salt
510510
*
511-
* @param values32 Values corresponding to:
511+
* @param values32 Values corresponding to:
512512
*
513513
* [0] = loan call time limit (in seconds)
514514
* [1] = loan maxDuration (in seconds)
515515
* [2] = loan interest rate (annual nominal percentage times 10**6)
516516
* [3] = loan interest update period (in seconds)
517517
*
518-
* @param cancelAmount Amount to cancel
519-
* @return Amount that was canceled
518+
* @param cancelAmount Amount to cancel
519+
* @return Amount that was canceled
520520
*/
521521
function cancelLoanOffering(
522522
address[10] addresses,
@@ -565,7 +565,7 @@ contract Margin is
565565
* [5] = loan expiration timestamp (in seconds)
566566
* [6] = loan salt
567567
*
568-
* @param values32 Values corresponding to:
568+
* @param values32 Values corresponding to:
569569
*
570570
* [0] = loan call time limit (in seconds)
571571
* [1] = loan maxDuration (in seconds)
@@ -590,9 +590,9 @@ contract Margin is
590590
}
591591

592592
/**
593-
* Transfer ownership of a loan to a new address. This new address will be entitled
594-
* to all payouts for this loan. Only callable by the lender for a position. If the "who"
595-
* param is a contract, it must implement the LoanOwner interface.
593+
* Transfer ownership of a loan to a new address. This new address will be entitled to all
594+
* payouts for this loan. Only callable by the lender for a position. If "who" is a contract, it
595+
* must implement the LoanOwner interface.
596596
*
597597
* @param positionId Unique ID for the position
598598
* @param who New owner of the loan
@@ -612,8 +612,8 @@ contract Margin is
612612

613613
/**
614614
* Transfer ownership of a position to a new address. This new address will be entitled to all
615-
* payouts. Only callable by the owner of a position. If the "who" param is a contract, it must
616-
* implement the PositionOwner interface.
615+
* payouts. Only callable by the owner of a position. If "who" is a contract, it must implement
616+
* the PositionOwner interface.
617617
*
618618
* @param positionId Unique ID for the position
619619
* @param who New owner of the position

util/lintcontracts.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,69 @@ def lintCommentHeader(dir, filepath, solidityVersion):
9191
return everythingOkay
9292

9393

94+
def lintFunctionComments(dir, filepath):
95+
fileName = os.path.basename(filepath)
96+
everythingOkay = True
97+
inBlockComment = False
98+
seenBlank = False
99+
alreadyComplained = False
100+
argColumn = 0
101+
i = 1
102+
for line in open(filepath, 'r').readlines():
103+
words = line.split()
104+
errorSuffix = " (" + fileName + ":" + str(i) + ")"
105+
lstripped = line.lstrip()
106+
107+
# check for extra statements
108+
if ('param ' in lstripped and 'param ' not in lstripped):
109+
everythingOkay = False
110+
print "Param has only one space" + errorSuffix
111+
if ('param ' in lstripped):
112+
everythingOkay = False
113+
print "Param has more than two spaces" + errorSuffix
114+
115+
# start block comment
116+
if (not inBlockComment and lstripped.startswith('/**')):
117+
argColumn = 0
118+
inBlockComment = True
119+
seenBlank = False
120+
alreadyComplained = False
121+
122+
# check for aligned parameters
123+
if (inBlockComment):
124+
col = 0
125+
if ('param' in lstripped and len(words) >= 4):
126+
col = line.find(' '+words[3]) + 1
127+
if ('@returns' in lstripped):
128+
col = line.find(words[2])
129+
if (col > 0):
130+
if (argColumn == 0):
131+
argColumn = col
132+
else:
133+
if (col != argColumn):
134+
everythingOkay = False
135+
print "Params not aligned to column " + str(argColumn + 1) + errorSuffix
136+
137+
# blank comment line
138+
if (inBlockComment and lstripped.rstrip() == '*'):
139+
seenBlank = True
140+
141+
# make sure a blank comment line has been found before parameter list
142+
if (inBlockComment and not seenBlank):
143+
if ('* param' in lstripped or '* @param' in lstripped):
144+
if (not alreadyComplained):
145+
alreadyComplained = True
146+
everythingOkay = False
147+
print "No blank line before param list in function comment" + errorSuffix
148+
149+
# end block comment
150+
if (inBlockComment and line.rstrip().endswith('*/')):
151+
inBlockComment = False
152+
i += 1
153+
154+
return everythingOkay
155+
156+
94157
def main():
95158
files = []
96159
start_dir = os.getcwd()
@@ -109,6 +172,7 @@ def main():
109172

110173
everythingOkay = True
111174
for file in files:
175+
everythingOkay &= lintFunctionComments(dir_path, file)
112176
everythingOkay &= lintImports(dir_path, file)
113177
everythingOkay &= lintCommentHeader(dir_path, file, "0.4.24")
114178

0 commit comments

Comments
 (0)