Skip to content

Commit

Permalink
Merge pull request #194 from dedis/amine
Browse files Browse the repository at this point in the history
Refactoring runComment in mod.go
  • Loading branch information
nkcr authored Oct 25, 2022
2 parents c1917b5 + 35c6820 commit 4bcefbe
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 24 deletions.
70 changes: 46 additions & 24 deletions internal/mcheck/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,49 +47,71 @@ func main() {
)
}

// run parses all the comments in ast.File
// iterateOverLines iterates over the lines of a comment and
// checks if the line is too long or contains a prefix
// that should be ignored
func iterateOverLines(pass *analysis.Pass, lines []string, c *ast.Comment) {
for j := 0; j < len(lines); j++ {
line := lines[j]
if checkPrefixes(line) {
continue
}
ifTooLong(line, pass, c)
if strings.HasPrefix(line, NoLint) {
// Skip next comment for block comment.
j++
}
}
}

// checkPrefixes checks if the line starts with any of the following
// prefixes:
//
// `go:generate`
// `http://`
// `https://`
//
// If it does, it returns `true`. Otherwise, it returns `false`
func checkPrefixes(line string) bool {
return strings.HasPrefix(line, "//go:generate") ||
strings.HasPrefix(line, "// http://") ||
strings.HasPrefix(line, "// https://")
}

// ifTooLong reports a comment if it's too long
func ifTooLong(line string, pass *analysis.Pass, c *ast.Comment) {
if len(line) > MaxLen {
pass.Reportf( // `c` is a comment.
c.Pos(), "Comment too long: %s (%d)",
line, len(line))
}
}

// runComment loops over all the files in the package, and for each file it
// loops over all the comments in the file, and for each comment it loops
// over all the lines in the comment, and for each line it checks
// if the line is too long
func runComment(pass *analysis.Pass) (interface{}, error) {
fileLoop:
for _, file := range pass.Files {
isFirst := true
for _, cg := range file.Comments {
for i := 0; i < len(cg.List); i++ {
c := cg.List[i]

if isFirst && strings.HasPrefix(c.Text, "// Code generated") {
continue fileLoop
}
// in case of /* */ comment there might be multiple lines
lines := strings.Split(c.Text, "\n")
for j := 0; j < len(lines); j++ {
line := lines[j]

if strings.HasPrefix(line, "//go:generate") {
continue
}
if strings.HasPrefix(line, "// http://") || strings.HasPrefix(line, "// https://") {
continue
}
if len(line) > MaxLen {
pass.Reportf(c.Pos(), "Comment too long: %s (%d)",
line, len(line))
}
if strings.HasPrefix(line, NoLint) {
// Skip next comment for block comment.
j++
}
}

iterateOverLines(pass, lines, c)
isFirst = false

if strings.HasPrefix(c.Text, NoLint) {
// Skip next comment for one-line comment.
// Skip next comment for block comment.
i++
}
}
}
}

return nil, nil
}

Expand Down
1 change: 1 addition & 0 deletions services/dkg/pedersen/mod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ func TestPedersen_Setup(t *testing.T) {
context: serdecontext,
formFac: formFac,
status: &dkg.Status{},

}

// Wrong formID
Expand Down

0 comments on commit 4bcefbe

Please sign in to comment.